本文介绍了枕头(PIL)到QImage的转换-> python.exe停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PIL加载图像,进行一些过滤,然后在GUI上显示图像.

I want to load an Image with PIL, apply some filtering and then display the image on a GUI.

我已经编写了一个示例应用程序:

I have written a little sample application:

from PyQt4 import QtCore, QtGui
from PIL import Image, ImageQt

class TwoDToThreeD(QtGui.QWidget):

    def __init__(self):

        QtGui.QWidget.__init__(self)
        layout = QtGui.QGridLayout()

        self.btnOpen = self.createButton("Open File", self.open)

        layout.addWidget(self.btnOpen, 4, 0)

        self.imageLabel = QtGui.QLabel()
        self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
        self.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
        self.imageLabel.setScaledContents(True)

        layout.addWidget(self.imageLabel, 0, 2, 4, 1)
        layout.setColumnStretch(1, 10)
        layout.setColumnStretch(2, 20)

        self.setLayout(layout)

    def createButton(self, text, member):
        button = QtGui.QPushButton(text)
        button.clicked.connect(member)
        return button


    def open(self):
        fileName = (QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath()))
        if fileName:
            print (fileName)
            self.imgPil = Image.open(str(fileName))

            # print (PIL.VERSION)

            print (self.imgPil.format, self.imgPil.size, self.imgPil.mode)
            # imgPil.show()
            img_tmp = ImageQt.ImageQt(self.imgPil)

            image = QtGui.QImage(img_tmp)

            if image.isNull():
                QtGui.QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
                return

            self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(image))



if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    dialog = TwoDToThreeD()
    dialog.show()
    sys.exit(app.exec_())

加载* .png效果很好.但是,当我尝试加载* .jpg python时,它崩溃了:

Loading a *.png works fine. But when I try to load a *.jpg python just crashes:

python.exe has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is
available.

我在Windows 8.1 64bit上,并尝试使用python 2.7 64bit,python 2.7 32bit和python 3.4 64bit相同的源代码.

I am on Windows 8.1 64Bit and tried the very same source with python 2.7 64bit, python 2.7 32bit and python 3.4 64bit.

使用所有3个版本,我得到的结果都是相同的.

With all 3 versions I get the same result.

有人遇到类似问题或知道解决方案吗?我什至无法调试代码,因为它一直运行到结束",然后崩溃:(

Has anybody experienced similar problems or knows a solution?I can't even debug the code, since it runs til the "end" and then crashes :(

推荐答案

基于 github

def pil2pixmap(self,im):
        if im.mode == "RGB":
            pass
        elif im.mode == "L":
            im = im.convert("RGBA")
        data = im.convert("RGBA").tostring("raw", "RGBA")
        qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_ARGB32)
        pixmap = QtGui.QPixmap.fromImage(qim)
        return pixmap
    def open(self):
        fileName = (QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath()))
        if fileName:
            imgPil = Image.open(str(fileName))
            # do your work then convert
            self.imageLabel.setPixmap(self.pil2pixmap(imgPil))

这篇关于枕头(PIL)到QImage的转换-> python.exe停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:56