我正在尝试使用以下测试应用程序显示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文件?
发布于 2012-07-25 01:19:25
QImage支持读取PGM文件。检查过img加载是否正确吗?
也许你有一个坏的文件,或者名称中的拼写错误,或者错误的权限
发布于 2012-07-25 16:17:29
看起来您正在使用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)使用
pixmap = QPixmap('test.pgm')
self.scene.addPixmap(pixmap)对你来说应该很管用。
https://stackoverflow.com/questions/11634358
复制相似问题