来自 Numpy 数组的 PyQt5 QImage [英] PyQt5 QImage from Numpy Array

查看:42
本文介绍了来自 Numpy 数组的 PyQt5 QImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码

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()  

我收到以下错误

类型错误:参数不匹配任何重载调用:QImage():参数太多 QImage(QSize, QImage.Format): 参数 1 有意外类型 'numpy.ndarray' QImage(int, int, QImage.Format):参数 1 具有意外类型 'numpy.ndarray' QImage(bytes, int,int, QImage.Format): 参数 1 具有意外类型 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): 参数 1 有意外类型 'numpy.ndarray' QImage(bytes, int, int, int,QImage.Format):参数 1 具有意外类型numpy.ndarray"
QImage(sip.voidptr, int, int, int, QImage.Format): 参数 1 有意外类型 'numpy.ndarray' QImage(List[str]): 参数 1 有意外类型 'numpy.ndarray' QImage(str, format: str = None):参数 1 具有意外类型 'numpy.ndarray' QImage(QImage):参数 1 具有意外类型 'numpy.ndarray' QImage(Any): 太多论据

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

根据这篇文章可能是由 numpy 创建视图引起的.修改行

According to this post this can be caused by numpy creating a view. Modifying the lines

 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)     

产生同样的错误.为了测试我没有通过视图,我打印了测试结果

Produces the same error. To test that I am not passing a view I print the result of the test

im_np_cpy.base is im_np

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

and it's False. The image is visualised correctly with cv2. I am clearly missing something, any idea what?

干杯!

推荐答案

我在转置后添加了一个副本是这样的:

I added a copy after the transpose like this:

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

这对我有用.

这篇关于来自 Numpy 数组的 PyQt5 QImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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