gpt4 book ai didi

python - 来自 Numpy 数组的 PyQt5 QImage

转载 作者:行者123 更新时间:2023-12-02 03:35:45 24 4
gpt4 key购买 nike

考虑以下代码

from PyQt5.QtWidgets import QMainWindow, QLabel, QSizePolicy, QApplication 
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import numpy as np
import sys



class Test(QMainWindow):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(10,10,640, 400)

pixmap_label = QLabel()
pixmap_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
pixmap_label.resize(640,400)
pixmap_label.setAlignment(Qt.AlignCenter)

im_np = np.ones((1800,2880,3),dtype=uint8)
im_np = np.transpose(im_np, (1,0,2))
qimage = QImage(im_np, im_np.shape[1], im_np.shape[0],
QImage.Format_RGB888)
pixmap = QPixmap(qimage)
pixmap = pixmap.scaled(640,400, Qt.KeepAspectRatio)
pixmap_label.setPixmap(pixmap)

self.setCentralWidget(pixmap_label)
self.show()



def main():
app = QApplication(sys.argv)
win = Test()
sys.exit(app.exec_())



if __name__=="__main__":
main()

我收到以下错误

TypeError: arguments did not match any overloaded call: QImage(): too many arguments QImage(QSize, QImage.Format): argument 1 has unexpected type 'numpy.ndarray' QImage(int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray' QImage(bytes, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray' QImage(bytes, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray' QImage(List[str]): argument 1 has unexpected type 'numpy.ndarray' QImage(str, format: str = None): argument 1 has unexpected type 'numpy.ndarray' QImage(QImage): argument 1 has unexpected type 'numpy.ndarray' QImage(Any): too many arguments

根据this post这可能是由 numpy 创建 View 引起的。修改行

 im_np = np.array(img)                                                                                                                                                                                  
im_np = np.transpose(im_np, (1,0,2))

im_np = np.array(img)                                                                                                                                                                                  
im_np = np.transpose(im_np, (1,0,2))
im_np_cpy = np.copy(im_np)

产生相同的错误。为了测试我没有通过 View ,我打印了测试结果

im_np_cpy.base is im_np

这是错误的。使用 cv2 可以正确显示图像。我显然错过了一些东西,知道什么吗?

干杯!

最佳答案

我在转置后添加了一个副本,如下所示:

im_np = np.transpose(im_np,(1,0,2)).copy()

这对我有用。

关于python - 来自 Numpy 数组的 PyQt5 QImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48639185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com