假设我使用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.stri
          _, 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环境来运行这个项目.
他们所指示的是数据是作为参数所必需的,而不是关键字被称为数据,以下方法将numpy / opencv图像转换为QImage:opencv version = 3.1.4
pyqt version = 5.9.2
numpy version = 1.15.0
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库
使用索引裁剪图像时只修改形状而不修改数据,解决方法是复制
img = cv2.imread('/path/of/image')
img = np.copy(img[200:500, 300:500, :]) # copy image
qimg = NumpyToQImage(img)
assert(not qimg.isNull())
        
             