Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageQt does not work as expected in PyQt5 #5266

Closed
driscollis opened this issue Feb 13, 2021 · 5 comments · Fixed by #5268
Closed

ImageQt does not work as expected in PyQt5 #5266

driscollis opened this issue Feb 13, 2021 · 5 comments · Fixed by #5268
Labels
Qt Qt for Python, PyQt, PySide

Comments

@driscollis
Copy link
Contributor

What did you do?

Attempted to use ImageQt to load a Pillow image in a PyQt5 application

What did you expect to happen?

I expected the image to load correctly. There are no errors, but it does not load the image in PyQt5 correctly.

What actually happened?

The image loaded as a mostly white image with kind of ghost image of the actual photo (see screenshot). I have attached the PyQt5 and PySide6 code, including screenshots from when I ran both files.

Note: The same code works in PySide6, but not in PyQt5.

What are your OS, Python and Pillow versions?

  • OS: MacOS Mojave and Windows 10
  • Python: Python 3.9
  • Pillow: 8.0.0
    pink_flower
import sys

from PIL import Image, ImageQt
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QWidget, QLabel
from PyQt5.QtWidgets import QVBoxLayout, QApplication


class ImageViewer(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.setWindowTitle("PyQt Image Viewer")

        # Open up image in Pillow
        image = Image.open("pink_flower.jpg")
        qt_image = ImageQt.ImageQt(image)
        pixmap = QPixmap.fromImage(qt_image)

        self.image_label = QLabel('')
        self.image_label.setPixmap(pixmap)

        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.image_label)
        self.setLayout(self.main_layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    viewer = ImageViewer()
    viewer.show()
    app.exec_()

pyqt_pillow_issue.zip

@radarhere radarhere added the Qt Qt for Python, PyQt, PySide label Feb 15, 2021
@radarhere radarhere changed the title ImageQt Does Not Work as Expected in PyQt5 ImageQt does not work as expected in PyQt5 Feb 15, 2021
@radarhere
Copy link
Member

Ok, got it. You're hitting this bug

Pillow/src/PIL/ImageQt.py

Lines 203 to 205 in fdd8b68

# Fix some strange bug that causes
if im.mode == "RGB":
im = im.convert("RGBA")

If I change your code to

        # Open up image in Pillow
        image = Image.open("pink_flower.jpg")
        pixmap = ImageQt.toqpixmap(image)

then it works.

@driscollis
Copy link
Contributor Author

Huh. That's interesting. I know I tried that, but I didn't remove the pixmap = QPixmap.fromImage(qt_image) line, which raises an error if left in there.

This definitely works if you remove that line too.

@driscollis
Copy link
Contributor Author

Thanks for looking into this and finding the error so quickly!

@radarhere
Copy link
Member

I've created #5268 to change Pillow so that your original code works.

@driscollis
Copy link
Contributor Author

Thanks for looking into this. I appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Qt Qt for Python, PyQt, PySide
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants