我有一個形狀為 numpy 的數組(500, 500, 3)。我希望它轉換成圖像并使用 PyQt5 顯示它。
1 回答

慕工程0101907
TA貢獻1887條經驗 獲得超5個贊
我假設該數組的類型為 uint8 并代表紅色、綠色和藍色 您可以Pillow為此使用,例如
from PyQt5 import QtWidgets, QtGui
from PIL import Image, ImageQt
import numpy as np
# generate data
table = np.zeros((256,256,3), dtype=np.uint8)
for i in range(256):
table[:,i,0] = i
table[i,:,1] = i
table[:,:,2] = (2*255 - table[:,:,0] - table[:,:,1]) // 2
# convert data to QImage using PIL
img = Image.fromarray(table, mode='RGB')
qt_img = ImageQt.ImageQt(img)
app = QtWidgets.QApplication([])
w = QtWidgets.QLabel()
w.setPixmap(QtGui.QPixmap.fromImage(qt_img))
w.show()
app.exec()
添加回答
舉報
0/150
提交
取消