为了账号安全,请及时绑定邮箱和手机立即绑定

从 numpy 数组在 PyQt5 中显示图像

从 numpy 数组在 PyQt5 中显示图像

函数式编程 2023-06-13 11:04:58
我有一个形状为 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()


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 168 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信