簡體   English   中英

Pillow (PIL) 到 QImage 的轉換 -> python.exe 已停止工作

[英]Pillow (PIL) to QImage conversion -> python.exe has stopped working

我想用 PIL 加載圖像,應用一些過濾,然后在 GUI 上顯示圖像。

我寫了一個小示例應用程序:

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 時崩潰:

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 完全相同的源。

對於所有 3 個版本,我都得到了相同的結果。

有沒有人遇到過類似的問題或知道解決方案? 我什至無法調試代碼,因為它運行到“結束”然后崩潰:(

基於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))

PyQt5和 Python 3.8 中,這不再有效。 我會按如下方式編寫pil2pixmap函數。

import io
from PyQt5.QtGui import QImage, QPixmap
from PIL import Image


def pil2pixmap(image):
    bytes_img = io.BytesIO()
    image.save(bytes_img, format='JPEG')

    qimg = QImage()
    qimg.loadFromData(bytes_img.getvalue())

    return QPixmap.fromImage(qimg)

其中參數image是例如image = Image.open('path_to_a_picture')

注意:要使用 QGuiApplication 必須運行的功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM