我读取了一个大小为:高度*宽度*3 (3=RGB)的字节数组,它表示一个图像。这是我从USB摄像头收到的原始数据。
我可以使用PIL on this thread来显示和保存它。现在,我正尝试将其显示在PyQt5窗口中。
我尝试过使用QLabel.setPixmap(),但似乎无法创建有效的像素贴图。
尝试读取字节数组失败:
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QByteArray
from PyQt5.QtWidgets import QLabel
self.camLabel = QLabel()
pixmap = QPixmap()
loaded = pixmap.loadFromData(QByteArray(img)) # img is a byte array of size: h*w*3
self.imgLabel.setPixmap(pixmap)在本例中,loaded返回False,因此我知道imgLabel.setPixmap将无法工作,但我不知道如何进一步调试以找出加载失败的原因。
第二次尝试使用PIL库失败:
import PIL.Image
import PIL.ImageQt
pImage = PIL.Image.fromarray(RGB) # RGB is a numpy array of the data in img
qtImage = PIL.ImageQt.ImageQt(pImage)
pixmap = QPixmap.fromImage(qtImage)
self.imgLabel.setPixmap(pixmap)在这个例子中,当我运行:self.imgLabel.setPixmap(pixmap)时,应用程序崩溃了,所以再一次,我不确定如何进一步调试。
任何帮助都将不胜感激!
发布于 2020-01-28 21:22:45
要从numpy数组中获取QPixmap,可以先创建一个QImage,然后使用它来创建QPixmap。例如:
from PyQt5 import QtCore, QtWidgets, QtGui
import numpy as np
# generate np array of (r, g, b) triplets with dtype uint8
height = width = 255
RGBarray = np.array([[r % 256, c % 256, -c % 256] for r in range(height) for c in range(width)], dtype=np.uint8)
app = QtWidgets.QApplication([])
label = QtWidgets.QLabel()
# create QImage from numpy array
image = QtGui.QImage(bytes(RGBarray), width, height, 3*width, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(image)
label.setPixmap(pixmap)
label.show()
app.exec()

https://stackoverflow.com/questions/59944947
复制相似问题