<small id='Ss0Hs'></small><noframes id='Ss0Hs'>

  • <legend id='Ss0Hs'><style id='Ss0Hs'><dir id='Ss0Hs'><q id='Ss0Hs'></q></dir></style></legend>

      <tfoot id='Ss0Hs'></tfoot>
          <bdo id='Ss0Hs'></bdo><ul id='Ss0Hs'></ul>

      1. <i id='Ss0Hs'><tr id='Ss0Hs'><dt id='Ss0Hs'><q id='Ss0Hs'><span id='Ss0Hs'><b id='Ss0Hs'><form id='Ss0Hs'><ins id='Ss0Hs'></ins><ul id='Ss0Hs'></ul><sub id='Ss0Hs'></sub></form><legend id='Ss0Hs'></legend><bdo id='Ss0Hs'><pre id='Ss0Hs'><center id='Ss0Hs'></center></pre></bdo></b><th id='Ss0Hs'></th></span></q></dt></tr></i><div id='Ss0Hs'><tfoot id='Ss0Hs'></tfoot><dl id='Ss0Hs'><fieldset id='Ss0Hs'></fieldset></dl></div>

        `QImage` 构造函数有未知关键字 `data`

        时间:2023-08-07

          1. <small id='yx24E'></small><noframes id='yx24E'>

            <tfoot id='yx24E'></tfoot>

            • <bdo id='yx24E'></bdo><ul id='yx24E'></ul>

              <i id='yx24E'><tr id='yx24E'><dt id='yx24E'><q id='yx24E'><span id='yx24E'><b id='yx24E'><form id='yx24E'><ins id='yx24E'></ins><ul id='yx24E'></ul><sub id='yx24E'></sub></form><legend id='yx24E'></legend><bdo id='yx24E'><pre id='yx24E'><center id='yx24E'></center></pre></bdo></b><th id='yx24E'></th></span></q></dt></tr></i><div id='yx24E'><tfoot id='yx24E'></tfoot><dl id='yx24E'><fieldset id='yx24E'></fieldset></dl></div>

                <legend id='yx24E'><style id='yx24E'><dir id='yx24E'><q id='yx24E'></q></dir></style></legend>
                    <tbody id='yx24E'></tbody>
                • 本文介绍了`QImage` 构造函数有未知关键字 `data`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我正在使用 opencv 从网络摄像头拍摄图像.

                  Suppose I am taking an image from the webcam using opencv.

                  _, img = self.cap.read()  # numpy.ndarray (480, 640, 3)
                  

                  然后我使用 img 创建一个 QImage qimg:

                  Then I create a QImage qimg using img:

                  qimg = QImage(
                      data=img,
                      width=img.shape[1],
                      height=img.shape[0],
                      bytesPerLine=img.strides[0],
                      format=QImage.Format_Indexed8)
                  

                  但它给出了一个错误提示:

                  But it gives an error saying that:

                  TypeError: 'data' 是一个未知的关键字参数

                  TypeError: 'data' is an unknown keyword argument

                  但是在 this 文档中说,构造函数应该有一个名为数据.

                  But said in this documentation, the constructor should have an argument named data.

                  我正在使用 anaconda 环境来运行这个项目.

                  I am using anaconda environment to run this project.

                  opencv 版本 = 3.1.4

                  opencv version = 3.1.4

                  pyqt 版本 = 5.9.2

                  pyqt version = 5.9.2

                  numpy 版本 = 1.15.0

                  numpy version = 1.15.0

                  推荐答案

                  他们的意思是需要data作为参数,而不是关键字叫data,下面的方法做了一个numpy/opencv的转换图像到 QImage:

                  What they are indicating is that the data is required as a parameter, not that the keyword is called data, the following method makes the conversion of a numpy/opencv image to QImage:

                  from PyQt5.QtGui import QImage, qRgb
                  import numpy as np
                  import cv2
                  
                  gray_color_table = [qRgb(i, i, i) for i in range(256)]
                  
                  def NumpyToQImage(im):
                      qim = QImage()
                      if im is None:
                          return qim
                      if im.dtype == np.uint8:
                          if len(im.shape) == 2:
                              qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8)
                              qim.setColorTable(gray_color_table)
                          elif len(im.shape) == 3:
                              if im.shape[2] == 3:
                                  qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888)
                              elif im.shape[2] == 4:
                                  qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32)
                      return qim
                  
                  img = cv2.imread('/path/of/image')
                  qimg = NumpyToQImage(img)
                  assert(not qimg.isNull())
                  

                  或者您可以使用 qimage2ndarray 库

                  当使用索引裁剪图片时只修改shape而不修改data,解决方法是复制一份

                  When using the indexes to crop the image is only modifying the shape but not the data, the solution is to make a copy

                  img = cv2.imread('/path/of/image')
                  img = np.copy(img[200:500, 300:500, :]) # copy image
                  qimg = NumpyToQImage(img)
                  assert(not qimg.isNull())
                  

                  这篇关于`QImage` 构造函数有未知关键字 `data`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 x 轴刻度更改为自定义字符串 下一篇:如何将 yaxis 刻度标签设置在固定位置,以便当我

                  相关文章

                  最新文章

                • <small id='UphZ6'></small><noframes id='UphZ6'>

                • <tfoot id='UphZ6'></tfoot>
                  • <bdo id='UphZ6'></bdo><ul id='UphZ6'></ul>

                  <legend id='UphZ6'><style id='UphZ6'><dir id='UphZ6'><q id='UphZ6'></q></dir></style></legend>

                      <i id='UphZ6'><tr id='UphZ6'><dt id='UphZ6'><q id='UphZ6'><span id='UphZ6'><b id='UphZ6'><form id='UphZ6'><ins id='UphZ6'></ins><ul id='UphZ6'></ul><sub id='UphZ6'></sub></form><legend id='UphZ6'></legend><bdo id='UphZ6'><pre id='UphZ6'><center id='UphZ6'></center></pre></bdo></b><th id='UphZ6'></th></span></q></dt></tr></i><div id='UphZ6'><tfoot id='UphZ6'></tfoot><dl id='UphZ6'><fieldset id='UphZ6'></fieldset></dl></div>