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

Fixed a bug with channel 4 population in ImageQt #8507

Closed
wants to merge 2 commits into from

Conversation

codev8services
Copy link

Changes proposed in this pull request:

  1. Fixed a bug with channel 4 population and format inconsistencies: lines 164, 167.
  2. Added PyQt5 support.

The changes have been tested.

1. Fixed a bug with channel 4 population and format inconsistencies: lines 164, 167.
2. Added PyQt5 support.

The changes have been tested.
@radarhere
Copy link
Member

radarhere commented Oct 26, 2024

Hi. We deliberately deprecated support for PyQt5 in Pillow 9.2.0 - https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#pyqt5-and-pyside2

Qt 5 reached end-of-life on 2020-12-08 for open-source users (and will reach EOL on 2023-12-08 for commercial licence holders).

Support for PyQt5 and PySide2 has been deprecated from ImageQt and will be removed in Pillow 10 (2023-07-01). Upgrade to PyQt6 or PySide6 instead.

Support was removed on schedule in Pillow 10.

As for your 'bug with channel 4 population and format inconsistencies', when I attempt to show an RGB image in PyQt6 on my macOS machine, the result looks fine.

import sys
from PIL import Image, ImageQt
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        label = QLabel()

        with Image.open('Tests/images/hopper.png') as im:
            assert im.mode == "RGB"
            pixmap = ImageQt.toqpixmap(im)
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(im.width, im.height)

    def button_clicked(self, s):
        print("click", s)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

Screenshot 2024-10-26 at 9 38 47 pm

Is it a problem that only happens with PyQt5? Could you provide a short code snippet to demonstrate?

@radarhere radarhere changed the title Fixed a bug with channel 4 population in ImageQt.py Fixed a bug with channel 4 population in ImageQt Oct 26, 2024
@codev8services
Copy link
Author

codev8services commented Oct 26, 2024

No, I have managed to reproduce this issue in both PyQt5 and PyQt6.

The corrections I made to the code resolved the issue. Although PyQt5 was not necessary for the original problem, its support was straightforward to implement. I used it to replicate the issue repeatedly.

from PyQt6.QtWidgets import QPushButton, QListWidget, QWidget, QApplication, QLabel, QVBoxLayout
from PyQt6.QtCore import Qt

#* The problem is also reproduced in the example using PyQt5.
# from PyQt5.QtWidgets import QPushButton, QListWidget, QWidget, QApplication, QLabel, QVBoxLayout
# from PyQt5.QtCore import Qt

from PIL import Image, ImageQt


def load_img():

    # The problem only occurs with RGB images, 
    # as indicated in the changes that I suggested.

    img = Image.open("cat.jpg")
    
    # The RGB image causes the program to crash with no obvious error in the console, 
    # or it loads in an incorrect format (I have attached a screenshot).
    pixmap = ImageQt.toqpixmap(img)

    picture_view.setPixmap(pixmap)


app = QApplication([])
window = QWidget()
window.resize(700, 600)

btn = QPushButton('load image to pixmap')
picture_view = QLabel('Image View')
picture_view.setStyleSheet("background: magenta")

layout = QVBoxLayout(window)
layout.addWidget(btn)
layout.addWidget(picture_view)

btn.clicked.connect(load_img)

window.show()
app.exec()

ERROR
error

CORRECT
correct

@radarhere
Copy link
Member

Could you provide a copy of cat.jpg?

What operating system are you using? What version of PyQt6?

>>> from PyQt6.QtCore import QT_VERSION_STR, PYQT_VERSION_STR
>>> print("Qt: v", QT_VERSION_STR, "\tPyQt: v", PYQT_VERSION_STR)
Qt: v 6.7.1 	PyQt: v 6.7.0

@codev8services
Copy link
Author

Qt: v 6.7.1 PyQt: v 6.7.1

Microsoft Windows [Version 10.0.17763.1697]
x64-based PC

I have tried to reproduce the error with other RGB images. In total, I have checked 4 images in the jpg format and there have been problems with all of them. However, there are no issues with the png format.

cat.jpg
cat

The picture was downloaded from the Freepik website.

@radarhere
Copy link
Member

Thanks. I'm unable to replicate your problem on my macOS machine - here's a screenshot using your image.

@codev8services
Copy link
Author

I also tested Pillow versions 10.0.0 and 9.5.0.

The error was discovered during classes with my student (also Windows). It appeared on his PC, I was able to reproduce it on mine.

So, the issue has been present in multiple versions of Pillow and occurs on Windows machines.

@hugovk hugovk added the Windows label Oct 27, 2024
@radarhere
Copy link
Member

I put together a commit to try and replicate this on GitHub Actions - it is indeed a problem on Windows

but not on macOS

This PR does indeed fix the problem

src/PIL/ImageQt.py Outdated Show resolved Hide resolved
src/PIL/ImageQt.py Outdated Show resolved Hide resolved
src/PIL/ImageQt.py Outdated Show resolved Hide resolved
src/PIL/ImageQt.py Outdated Show resolved Hide resolved
src/PIL/ImageQt.py Outdated Show resolved Hide resolved
Co-authored-by: Hugo van Kemenade <[email protected]>
@radarhere
Copy link
Member

@codev8services you may have noticed that our test suite is failing here, because it doesn't expect RGB images to be turned into RGBA. I've come up with an alternative suggestion that lets us continue to use RGB. Would you mind testing #8509?

@hugovk hugovk added the Qt Qt for Python, PyQt, PySide label Oct 28, 2024
@radarhere
Copy link
Member

#8509 has now been merged instead.

@radarhere radarhere closed this Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Tests Qt Qt for Python, PyQt, PySide Windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants