From 037354e8ef9cdd6aa237763d604c742e00985377 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 14 Dec 2021 10:13:09 +1100 Subject: [PATCH] Correct image by scaling pixels --- Tests/test_imageqt.py | 14 ++++++++++---- src/PIL/ImageQt.py | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index 08cab9976b2..589cb5a210a 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -2,7 +2,7 @@ from PIL import ImageQt -from .helper import hopper +from .helper import assert_image_similar, hopper pytestmark = pytest.mark.skipif( not ImageQt.qt_is_installed, reason="Qt bindings are not installed" @@ -42,11 +42,17 @@ def checkrgb(r, g, b): def test_image(): - for mode in ("1", "RGB", "RGBA", "L", "P"): - ImageQt.ImageQt(hopper(mode)) + modes = ["1", "RGB", "RGBA", "L", "P"] qt_format = ImageQt.QImage.Format if ImageQt.qt_version == "6" else ImageQt.QImage if hasattr(qt_format, "Format_Grayscale16"): # Qt 5.13+ - ImageQt.ImageQt(hopper("I;16")) + modes.append("I;16") + + for mode in modes: + im = hopper(mode) + roundtripped_im = ImageQt.fromqimage(ImageQt.ImageQt(im)) + if mode not in ("RGB", "RGBA"): + im = im.convert("RGB") + assert_image_similar(roundtripped_im, im, 1) def test_closed_file(): diff --git a/src/PIL/ImageQt.py b/src/PIL/ImageQt.py index 8970314d9ae..db8fa0fa9f9 100644 --- a/src/PIL/ImageQt.py +++ b/src/PIL/ImageQt.py @@ -168,6 +168,8 @@ def _toqclass_helper(im): data = im.tobytes("raw", "BGRA") format = qt_format.Format_ARGB32 elif im.mode == "I;16" and hasattr(qt_format, "Format_Grayscale16"): # Qt 5.13+ + im = im.point(lambda i: i * 256) + format = qt_format.Format_Grayscale16 else: if exclusive_fp: