将numpy数组转换为PySide QPixmap [英] Convert numpy array to PySide QPixmap

查看:137
本文介绍了将numpy数组转换为PySide QPixmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像转换为NumPy数组并转换为PySide QPixmap,因此可以显示它(在PySide UI中编辑).我已经找到了此工具: qimage2ndarray ,但仅适用于PyQt4.我尝试对其进行更改以使其能够与PySide一起使用,但是我将不得不更改该工具的C部分,并且我没有使用C的经验.我该怎么做或有其他选择吗?

I want to convert an image into a NumPy array to a PySide QPixmap, so I can display it ( in my PySide UI). I already found this tool: qimage2ndarray, but it only works for PyQt4. I tried to change it to get it working with PySide, but I would have to change the C part of the tool and I have no experience with C. How can I do this or are there any alternatives?

推荐答案

一种替代方法是只使用PIL库.

One alternative is to just use PIL library.

>>> import numpy as np
>>> import Image
>>> im = Image.fromarray(np.random.randint(0,256,size=(100,100,3)).astype(np.uint8))
>>> im.show()

您可以在 http://www.pyside.org/docs上查看QPixmap构造函数/pyside/PySide/QtGui/QImage.html .

看起来您应该能够直接在构造函数中使用numpy数组:

It looks like you should be able to use a numpy array directly in the constructor:

PySide.QtGui.QImage类(数据,宽度,高度,格式)

class PySide.QtGui.QImage(data, width, height, format)

其中format参数是以下之一:

where the format argument is one of these: http://www.pyside.org/docs/pyside/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.Format.

例如,您可以执行以下操作:

So, for example you could do something like:

>>> a = np.random.randint(0,256,size=(100,100,3)).astype(np.uint32)
>>> b = (255 << 24 | a[:,:,0] << 16 | a[:,:,1] << 8 | a[:,:,2]).flatten() # pack RGB values
>>> im = PySide.QtGui.QImage(b, 100, 100, PySide.QtGui.QImage.Format_RGB32)

我没有安装PySide,所以我没有进行测试.可能无法按原样工作,但它可能会引导您朝着正确的方向前进.

I don't have PySide installed so I haven't tested this. Chances are it won't work as is, but it might guide you in the right direction.

这篇关于将numpy数组转换为PySide QPixmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆