Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何在QGraphicsView中显示PGM文件?_Qt_Pyqt_Qgraphicsview_Qgraphicsscene_Pgm - Fatal编程技术网

Qt 如何在QGraphicsView中显示PGM文件?

Qt 如何在QGraphicsView中显示PGM文件?,qt,pyqt,qgraphicsview,qgraphicsscene,pgm,Qt,Pyqt,Qgraphicsview,Qgraphicsscene,Pgm,我正在尝试使用以下测试应用程序显示PGM文件。该代码适用于PNG文件,但不适用于PGM: import sys from PyQt4.QtGui import * from PyQt4.QtCore import * import Image import ImageQt class MyView(QGraphicsView): def __init__(self): QGraphicsView.__init__(self) self.scene =

我正在尝试使用以下测试应用程序显示PGM文件。该代码适用于PNG文件,但不适用于PGM:

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import Image
import ImageQt

class MyView(QGraphicsView):
    def __init__(self):
        QGraphicsView.__init__(self)

        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)

        img = Image.open('test.pgm') # works with 'test.png' 
        self.imgQ = ImageQt.ImageQt(img)
        pixmap = QPixmap.fromImage(self.imgQ)
        self.scene.addPixmap(pixmap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = MyView()
    view.show()
    sys.exit(app.exec_())

如何在QGraphicsView中显示PGM文件?

QImage支持读取PGM文件。您是否检查过img是否正确加载


可能您有abad文件、名称输入错误或权限错误

看起来您正在使用PIL打开图像,将其转换为QImage,然后将其转换为QPixmap

有什么原因不能直接将PGM加载到QPixmap并避免PIL

替换

img = Image.open('test.pgm') # works with 'test.png' 
self.imgQ = ImageQt.ImageQt(img)
pixmap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixmap)


应该对你有用。

完全有效。实际上,我发现的一个解决方法是首先使用QIMage加载文件:
qimg=QIMage(“test.pgm”)pixmap=QPixmap.frommage(qimg)self.scene.addPixmap(pixmap)
。但是你的方法更简单。谢谢。是的,我已经检查过img是否正确加载。OP代码是否适用于PGM文件?
pixmap = QPixmap('test.pgm')
self.scene.addPixmap(pixmap)