python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.32k stars 2.23k forks source link

Fixed a bug with channel 4 population in ImageQt #8507

Closed codev8services closed 3 weeks ago

codev8services commented 3 weeks ago

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.

radarhere commented 3 weeks ago

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?

codev8services commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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

codev8services commented 3 weeks ago

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.

radarhere commented 3 weeks ago

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

radarhere commented 3 weeks ago

@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?

radarhere commented 3 weeks ago

8509 has now been merged instead.