如何使用PyQt5在QWidget上设置numpy数组图像 [英] How to set an numpy array image on a QWidget using PyQt5
问题描述
我正在从相机中读取一个图像作为一个numpy阵列。我的目标是将它放在pyqt5的Qwidget中并在我的mainwindow gui程序上打印,但是我收到以下错误:
I'm reading an image from my camera as a numpy array. My aim is to put it inside a Qwidget from pyqt5 and print on my mainwindow gui program, but i'm getting the following error:
TypeError: QPixmap(): argument 1 has unexpected type 'numpy.ndarray'
这是代码:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from epics import PV
import numpy as np
class PanoramicGUI:
def __init__(self):
self.MainWindow = uic.loadUi('panoramicGUI.ui')
self.MainWindow.SavePositionButton. clicked.connect(self.save_image)
def save_image(self):
detectorData = PV("CAMERA:DATA")
self.data = detectorData.get()
self.data = np.array(self.data).reshape(2048,2048).astype(np.int32)
print(self.data)
img = PrintImage(QPixmap(self.data))
self.MainWindow.WidgetHV1X1.setLayout(QtWidgets.QVBoxLayout())
self.MainWindow.WidgetHV1X1.layout().addWidget(img)
class PrintImage(QWidget):
def __init__(self, pixmap, parent=None):
QWidget.__init__(self, parent=parent)
self.pixmap = pixmap
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.pixmap)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
panoramic = PanoramicGUI()
panoramic.MainWindow.show()
app.exec_()
有人可以帮助我吗?
问候,
Gabriel。
推荐答案
有多种方法可以解决这个问题。
There are multiple ways to go about this.
一种选择是加载通过提供文件路径直接从磁盘映像。所以你会有 img = PrintImage(QPixmap(FILE_PATH))
其中 FILE_PATH
是一些字符串而不是numpy数组。有关更完整的示例,请参阅以下链接: https://www.tutorialspoint.com/pyqt/ pyqt_qpixmap_class.htm
One option is to load the image straight from disk by providing a file path. So you would have img = PrintImage(QPixmap(FILE_PATH))
where FILE_PATH
is some string instead of a numpy array. For a more complete example see the following link: https://www.tutorialspoint.com/pyqt/pyqt_qpixmap_class.htm
如果你真的想用numpy数组处理它,那么你需要创建一个 QtGui.QImage( )
首先将对象传递到 QtGui.QPixmap()
对象而不是直接传递给numpy数组。根据 QtGui.QImage()
的文档,如果数据的格式不是 QtGui的可识别格式,则需要设置数据的格式。 QImage的()
。所以以下内容应该有效:
If you really want to handle it with a numpy array, then you need to create a QtGui.QImage()
object first and pass that into your QtGui.QPixmap()
object instead of a numpy array directly. Per the documentation of QtGui.QImage()
, you need to set the format of the data if it is not already in a recognized format by QtGui.QImage()
. So the following should work:
#Initialze QtGui.QImage() with arguments data, height, width, and QImage.Format
self.data = np.array(self.data).reshape(2048,2048).astype(np.int32)
qimage = QtGui.QImage(self.data, self.data.shape[0],self.data.shape[1],QtGui.QImage.Format_RGB32)
img = PrintImage(QPixmap(qimage))
QtGui.QImage()
的最后一个参数可以从这里的文档中的列表中更改为您想要的任何内容 http://srinikom.github.io /pyside-docs/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.Format
The last argument for QtGui.QImage()
can be changed to whatever you desire among the list from the documentation here http://srinikom.github.io/pyside-docs/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.Format
最后的链接非常好一般的所有事情 QtGui
相关。
That final link is really good in general for all things QtGui
related.
这篇关于如何使用PyQt5在QWidget上设置numpy数组图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!