`QImage` 构造函数有未知的关键字 `data`
`QImage` constructor has unknown keyword `data`
假设我正在使用 opencv 从网络摄像头拍摄图像。
_, img = self.cap.read() # numpy.ndarray (480, 640, 3)
然后我使用 img
创建一个 QImage
qimg:
qimg = QImage(
data=img,
width=img.shape[1],
height=img.shape[0],
bytesPerLine=img.strides[0],
format=QImage.Format_Indexed8)
但它给出了一个错误:
TypeError: 'data' is an unknown keyword argument
但是在 this 文档中说,构造函数应该有一个名为 data
的参数。
我正在使用 anaconda 环境来 运行 这个项目。
opencv version = 3.1.4
pyqt version = 5.9.2
numpy version = 1.15.0
他们说的是需要data作为参数,并不是说关键字叫data,下面的方法将numpy/opencv图片转成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
,解决办法是复制一份
img = cv2.imread('/path/of/image')
img = np.copy(img[200:500, 300:500, :]) # copy image
qimg = NumpyToQImage(img)
assert(not qimg.isNull())
我怀疑 TypeError: 'data' is an unknown keyword argument
出错了,因为这是它遇到的第一个参数。
链接的 class 参考是针对 PyQt4 的,对于 PyQt5,它链接到 https://doc.qt.io/qt-5/qimage.html 的 C++
文档,但相似之处很明显。
PyQt4:
QImage.__init__ (self, bytes data, int width, int height, int bytesPerLine, Format format)
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
PyQt5 (C++):
QImage(const uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
根据 https://www.programcreek.com/python/example/106694/PyQt5.QtGui.QImage 中的示例,您可以尝试
qimg = QImage(img, img.shape[1], img.shape[0], img.strides[0], QImage.Format_Indexed8)
(没有 data=、width= 等)
假设我正在使用 opencv 从网络摄像头拍摄图像。
_, img = self.cap.read() # numpy.ndarray (480, 640, 3)
然后我使用 img
创建一个 QImage
qimg:
qimg = QImage(
data=img,
width=img.shape[1],
height=img.shape[0],
bytesPerLine=img.strides[0],
format=QImage.Format_Indexed8)
但它给出了一个错误:
TypeError: 'data' is an unknown keyword argument
但是在 this 文档中说,构造函数应该有一个名为 data
的参数。
我正在使用 anaconda 环境来 运行 这个项目。
opencv version = 3.1.4
pyqt version = 5.9.2
numpy version = 1.15.0
他们说的是需要data作为参数,并不是说关键字叫data,下面的方法将numpy/opencv图片转成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
,解决办法是复制一份
img = cv2.imread('/path/of/image')
img = np.copy(img[200:500, 300:500, :]) # copy image
qimg = NumpyToQImage(img)
assert(not qimg.isNull())
我怀疑 TypeError: 'data' is an unknown keyword argument
出错了,因为这是它遇到的第一个参数。
链接的 class 参考是针对 PyQt4 的,对于 PyQt5,它链接到 https://doc.qt.io/qt-5/qimage.html 的 C++
文档,但相似之处很明显。
PyQt4:
QImage.__init__ (self, bytes data, int width, int height, int bytesPerLine, Format format)
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
PyQt5 (C++):
QImage(const uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
根据 https://www.programcreek.com/python/example/106694/PyQt5.QtGui.QImage 中的示例,您可以尝试
qimg = QImage(img, img.shape[1], img.shape[0], img.strides[0], QImage.Format_Indexed8)
(没有 data=、width= 等)