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

Support 16-bit grayscale ImageQt conversion #5856

Merged
merged 4 commits into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions Tests/test_imageqt.py
Expand Up @@ -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"
Expand Down Expand Up @@ -42,8 +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+
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():
Expand Down
6 changes: 5 additions & 1 deletion src/PIL/ImageQt.py
Expand Up @@ -108,7 +108,7 @@ def align8to32(bytes, width, mode):
converts each scanline of data from 8 bit to 32 bit aligned
"""

bits_per_pixel = {"1": 1, "L": 8, "P": 8}[mode]
bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]

# calculate bytes per line and the extra padding if needed
bits_per_line = bits_per_pixel * width
Expand Down Expand Up @@ -167,6 +167,10 @@ def _toqclass_helper(im):
elif im.mode == "RGBA":
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:
im.close()
Expand Down