From b9701b0d52aeedaba40517703bb00fac43ae5a0d Mon Sep 17 00:00:00 2001 From: codev8 <163837343+codev8services@users.noreply.github.com> Date: Sat, 26 Oct 2024 14:01:32 +0500 Subject: [PATCH 1/2] Fixed a bug with channel 4 population in ImageQt.py 1. Fixed a bug with channel 4 population and format inconsistencies: lines 164, 167. 2. Added PyQt5 support. The changes have been tested. --- src/PIL/ImageQt.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index a3d647138df..b19f8da4591 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -26,6 +26,7 @@ if TYPE_CHECKING: import PyQt6 + import PyQt5 import PySide6 from . import ImageFile @@ -39,6 +40,7 @@ qt_version: str | None qt_versions = [ ["6", "PyQt6"], + ["5", "PyQt5"], ["side6", "PySide6"], ] @@ -50,6 +52,9 @@ if qt_module == "PyQt6": from PyQt6.QtCore import QBuffer, QIODevice from PyQt6.QtGui import QImage, QPixmap, qRgba + elif qt_module == "PyQt5": + from PyQt5.QtCore import QBuffer, QIODevice + from PyQt5.QtGui import QImage, QPixmap, qRgba elif qt_module == "PySide6": from PySide6.QtCore import QBuffer, QIODevice from PySide6.QtGui import QImage, QPixmap, qRgba @@ -155,11 +160,11 @@ def _toqclass_helper(im: Image.Image | str | QByteArray) -> dict[str, Any]: assert palette is not None colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)] elif im.mode == "RGB": - # Populate the 4th channel with 255 + # Populate the 4th channel with 255 #! im = im.convert("RGBA") data = im.tobytes("raw", "BGRA") - format = getattr(qt_format, "Format_RGB32") + format = getattr(qt_format, "Format_ARGB32") # 4th channel was populated in line 164 elif im.mode == "RGBA": data = im.tobytes("raw", "BGRA") format = getattr(qt_format, "Format_ARGB32") From f22b0c66e83f6900c9b8d7c7753167f554d934f9 Mon Sep 17 00:00:00 2001 From: codev8 <163837343+codev8services@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:28:35 +0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- src/PIL/ImageQt.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index b19f8da4591..c0b03a8812d 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -26,7 +26,6 @@ if TYPE_CHECKING: import PyQt6 - import PyQt5 import PySide6 from . import ImageFile @@ -40,7 +39,6 @@ qt_version: str | None qt_versions = [ ["6", "PyQt6"], - ["5", "PyQt5"], ["side6", "PySide6"], ] @@ -52,9 +50,6 @@ if qt_module == "PyQt6": from PyQt6.QtCore import QBuffer, QIODevice from PyQt6.QtGui import QImage, QPixmap, qRgba - elif qt_module == "PyQt5": - from PyQt5.QtCore import QBuffer, QIODevice - from PyQt5.QtGui import QImage, QPixmap, qRgba elif qt_module == "PySide6": from PySide6.QtCore import QBuffer, QIODevice from PySide6.QtGui import QImage, QPixmap, qRgba @@ -160,11 +155,11 @@ def _toqclass_helper(im: Image.Image | str | QByteArray) -> dict[str, Any]: assert palette is not None colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)] elif im.mode == "RGB": - # Populate the 4th channel with 255 #! + # Populate the 4th channel with 255 im = im.convert("RGBA") data = im.tobytes("raw", "BGRA") - format = getattr(qt_format, "Format_ARGB32") # 4th channel was populated in line 164 + format = getattr(qt_format, "Format_ARGB32") elif im.mode == "RGBA": data = im.tobytes("raw", "BGRA") format = getattr(qt_format, "Format_ARGB32")