我正在将相机中的图像作为 numpy 数组读取。我的目标是将其放入 pyqt5 的 Qwidget 中并在我的 mainwindow gui 程序上打印,但我收到以下错误:
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_()
有人能帮我吗?
Regards,
Gabriel.
有多种方法可以解决这个问题。
一种选择是通过提供文件路径直接从磁盘加载图像。所以你会有img = PrintImage(QPixmap(FILE_PATH))
where FILE_PATH
是一些字符串而不是 numpy 数组。有关更完整的示例,请参阅以下链接:https://www.tutorialspoint.com/pyqt/pyqt_qpixmap_class.htm https://www.tutorialspoint.com/pyqt/pyqt_qpixmap_class.htm
如果你真的想用 numpy 数组来处理它,那么你需要创建一个QtGui.QImage()
首先对象并将其传递到您的QtGui.QPixmap()
对象而不是直接使用 numpy 数组。根据文档QtGui.QImage()
,如果数据尚未采用可识别的格式,则需要设置数据的格式QtGui.QImage()
。所以以下应该有效:
#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 http://srinikom.github.io/pyside-docs/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.Format
最后的链接总体来说对于所有事情来说都非常好QtGui
有关的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)