它们所指示的是需要将数据作为参数,而不是将关键字称为data,以下方法将numpy / opencv图像转换为QImage:
from PyQt5.QtGui import QImage, qRgbimport numpy as npimport cv2gray_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 qimimg = 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 imageqimg = NumpyToQImage(img)assert(not qimg.isNull())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)