-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add JPEG XL Open/Read support via libjxl #7848
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
Open
olokelo
wants to merge
49
commits into
python-pillow:main
Choose a base branch
from
olokelo:jxl-support2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 32 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
8e0c5db
add tests
olokelo a57ebea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 23fb57d
minor fixes, linting corrections
olokelo 2eb5987
Added type hints
radarhere 37b58f3
Removed feature
radarhere eeaecb4
Merge pull request #1 from radarhere/jxl-support2
olokelo 24b63ad
fix goto labels for clang
olokelo 0b50410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f403672
Merge branch 'main' into jxl-support2
hugovk f6086d4
modify leak test
olokelo 5320450
fix _jxl_decoder_count_frames
olokelo 1b049ab
minor plugin code tweaks
olokelo 6048520
add type hints
olokelo 8fa280f
rename jxl -> jpegxl
olokelo 58c37bf
add test case for seeking to the same frame
olokelo 48bbc2e
flip cases in metadata test
olokelo 443a352
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 62c58c2
fix some type hinting mistakes
olokelo 8cab1c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e5003ff
change Optional to python 3.10+ syntax
olokelo fa5bfac
add more metadata test cases
olokelo 0b71605
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1f00fb8
add 16-bits grayscale support for jpeg xl images
olokelo 08270a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9313587
Merge branch 'main' into jxl-support2
radarhere 4256b2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8a1c03e
Merge branch 'main' into jxl-support2
radarhere 13944d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bb06057
Merge branch 'main' into jxl-support2
radarhere bc4a794
Merge branch 'main' into jxl-support2
radarhere ff269ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 661c0d8
Lint fixes
radarhere ade1db0
Replace slice and comparison with startswith
radarhere ceec3f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5e0457a
Removed getxmp()
radarhere 9266318
Do not set info["exif"] to None
radarhere 29e4b55
Do not add _getexif to new plugin
radarhere 3cd3848
Added type hint
radarhere aa6510f
Removed self.rawmode
radarhere 36640de
tile is already empty list
radarhere 7125fe4
Fixed type hints
radarhere 05aee33
Use monkeypatch
radarhere 6c3f0b5
Use member names to initialize module
radarhere 80e9963
Use member names to initialize PyTypeObject
radarhere 29c1e4c
Removed C method unused by Python
radarhere c8409e0
Simplified code
radarhere 61ce5c2
is_animated should be a bool
radarhere bf0cdb2
Test on Linux
radarhere 79f941d
Added argument that was removed in 0.9.0
radarhere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
|
||
import pytest | ||
|
||
from PIL import Image, JpegXlImagePlugin, features | ||
|
||
from .helper import ( | ||
assert_image_similar_tofile, | ||
skip_unless_feature, | ||
) | ||
|
||
try: | ||
from PIL import _jpegxl | ||
|
||
HAVE_JPEGXL = True | ||
except ImportError: | ||
HAVE_JPEGXL = False | ||
|
||
# cjxl v0.9.2 41b8cdab | ||
# hopper.jxl: cjxl hopper.png hopper.jxl -q 75 -e 8 | ||
# 16_bit_binary.jxl: cjxl 16_bit_binary.pgm 16_bit_binary.jxl -q 100 -e 9 | ||
|
||
|
||
class TestUnsupportedJpegXl: | ||
def test_unsupported(self) -> None: | ||
if HAVE_JPEGXL: | ||
JpegXlImagePlugin.SUPPORTED = False | ||
|
||
file_path = "Tests/images/hopper.jxl" | ||
with pytest.raises(OSError): | ||
with Image.open(file_path): | ||
pass | ||
|
||
if HAVE_JPEGXL: | ||
JpegXlImagePlugin.SUPPORTED = True | ||
|
||
|
||
@skip_unless_feature("jpegxl") | ||
class TestFileJpegXl: | ||
def setup_method(self) -> None: | ||
self.rgb_mode = "RGB" | ||
self.i16_mode = "I;16" | ||
|
||
def test_version(self) -> None: | ||
_jpegxl.JpegXlDecoderVersion() | ||
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("jpegxl")) | ||
|
||
def test_read_rgb(self) -> None: | ||
""" | ||
Can we read a RGB mode Jpeg XL file without error? | ||
Does it have the bits we expect? | ||
""" | ||
|
||
with Image.open("Tests/images/hopper.jxl") as image: | ||
assert image.mode == self.rgb_mode | ||
assert image.size == (128, 128) | ||
assert image.format == "JPEG XL" | ||
image.load() | ||
image.getdata() | ||
|
||
# generated with: | ||
# djxl hopper.jxl hopper_jxl_bits.ppm | ||
assert_image_similar_tofile(image, "Tests/images/hopper_jxl_bits.ppm", 1.0) | ||
|
||
def test_read_i16(self) -> None: | ||
""" | ||
Can we read 16-bit Grayscale Jpeg XL image? | ||
""" | ||
|
||
with Image.open("Tests/images/jxl/16bit_subcutaneous.cropped.jxl") as image: | ||
assert image.mode == self.i16_mode | ||
assert image.size == (128, 64) | ||
assert image.format == "JPEG XL" | ||
image.load() | ||
image.getdata() | ||
|
||
assert_image_similar_tofile( | ||
image, "Tests/images/jxl/16bit_subcutaneous.cropped.png", 1.0 | ||
) | ||
|
||
def test_JpegXlDecode_with_invalid_args(self) -> None: | ||
""" | ||
Calling decoder functions with no arguments should result in an error. | ||
""" | ||
|
||
with pytest.raises(TypeError): | ||
_jpegxl.PILJpegXlDecoder() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from PIL import Image | ||
|
||
from .helper import assert_image_similar_tofile | ||
|
||
_jpegxl = pytest.importorskip("PIL._jpegxl", reason="JPEG XL support not installed") | ||
|
||
|
||
def test_read_rgba() -> None: | ||
""" | ||
Can we read an RGBA mode file without error? | ||
Does it have the bits we expect? | ||
""" | ||
|
||
# Generated with `cjxl transparent.png transparent.jxl -q 100 -e 8` | ||
file_path = "Tests/images/transparent.jxl" | ||
with Image.open(file_path) as image: | ||
assert image.mode == "RGBA" | ||
assert image.size == (200, 150) | ||
assert image.format == "JPEG XL" | ||
image.load() | ||
image.getdata() | ||
|
||
image.tobytes() | ||
|
||
assert_image_similar_tofile(image, "Tests/images/transparent.png", 1.0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from PIL import Image | ||
|
||
from .helper import ( | ||
assert_image_equal, | ||
skip_unless_feature, | ||
) | ||
|
||
pytestmark = [ | ||
skip_unless_feature("jpegxl"), | ||
] | ||
|
||
|
||
def test_n_frames() -> None: | ||
"""Ensure that jxl format sets n_frames and is_animated attributes correctly.""" | ||
|
||
with Image.open("Tests/images/hopper.jxl") as im: | ||
assert im.n_frames == 1 | ||
assert not im.is_animated | ||
|
||
with Image.open("Tests/images/iss634.jxl") as im: | ||
assert im.n_frames == 41 | ||
assert im.is_animated | ||
|
||
|
||
def test_float_duration() -> None: | ||
|
||
with Image.open("Tests/images/iss634.jxl") as im: | ||
im.load() | ||
assert im.info["duration"] == 70 | ||
|
||
|
||
def test_seeking() -> None: | ||
""" | ||
Open an animated jxl file, and then try seeking through frames in reverse-order, | ||
verifying the durations are correct. | ||
""" | ||
|
||
with Image.open("Tests/images/jxl/traffic_light.jxl") as im1: | ||
with Image.open("Tests/images/jxl/traffic_light.gif") as im2: | ||
assert im1.n_frames == im2.n_frames | ||
assert im1.is_animated | ||
|
||
# Traverse frames in reverse, checking timestamps and durations | ||
total_dur = 0 | ||
for frame in reversed(range(im1.n_frames)): | ||
im1.seek(frame) | ||
im1.load() | ||
im2.seek(frame) | ||
im2.load() | ||
|
||
assert_image_equal(im1.convert("RGB"), im2.convert("RGB")) | ||
|
||
total_dur += im1.info["duration"] | ||
assert im1.info["duration"] == im2.info["duration"] | ||
assert im1.info["timestamp"] == im1.info["timestamp"] | ||
assert total_dur == 8000 | ||
|
||
assert im1.tell() == 0 | ||
assert im2.tell() == 0 | ||
|
||
im1.seek(0) | ||
im1.load() | ||
im2.seek(0) | ||
im2.load() | ||
|
||
|
||
def test_seek_errors() -> None: | ||
with Image.open("Tests/images/iss634.jxl") as im: | ||
with pytest.raises(EOFError): | ||
im.seek(-1) | ||
|
||
with pytest.raises(EOFError): | ||
im.seek(47) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from __future__ import annotations | ||
|
||
from types import ModuleType | ||
|
||
import pytest | ||
|
||
from PIL import Image | ||
|
||
from .helper import skip_unless_feature | ||
|
||
pytestmark = [ | ||
skip_unless_feature("jpegxl"), | ||
] | ||
|
||
ElementTree: ModuleType | None | ||
try: | ||
from defusedxml import ElementTree | ||
except ImportError: | ||
ElementTree = None | ||
|
||
|
||
# cjxl flower.jpg flower.jxl --lossless_jpeg=0 -q 75 -e 8 | ||
|
||
# >>> from PIL import Image | ||
# >>> with Image.open('Tests/images/flower2.webp') as im: | ||
# >>> with open('/tmp/xmp.xml', 'wb') as f: | ||
# >>> f.write(im.info['xmp']) | ||
# cjxl flower2.jpg flower2.jxl --lossless_jpeg=0 -q 75 -e 8 -x xmp=/tmp/xmp.xml | ||
|
||
|
||
def test_read_exif_metadata() -> None: | ||
file_path = "Tests/images/flower.jxl" | ||
with Image.open(file_path) as image: | ||
assert image.format == "JPEG XL" | ||
exif_data = image.info.get("exif", None) | ||
assert exif_data | ||
|
||
exif = image._getexif() | ||
|
||
# Camera make | ||
assert exif[271] == "Canon" | ||
|
||
with Image.open("Tests/images/flower.jpg") as jpeg_image: | ||
expected_exif = jpeg_image.info["exif"] | ||
|
||
# jpeg xl always returns exif without 'Exif\0\0' prefix | ||
assert exif_data == expected_exif[6:] | ||
|
||
|
||
def test_read_exif_metadata_without_prefix() -> None: | ||
with Image.open("Tests/images/flower2.jxl") as im: | ||
# Assert prefix is not present | ||
assert im.info["exif"][:6] != b"Exif\x00\x00" | ||
|
||
exif = im.getexif() | ||
assert exif[305] == "Adobe Photoshop CS6 (Macintosh)" | ||
|
||
|
||
def test_read_icc_profile() -> None: | ||
file_path = "Tests/images/flower2.jxl" | ||
with Image.open(file_path) as image: | ||
assert image.format == "JPEG XL" | ||
assert image.info.get("icc_profile", None) | ||
|
||
icc = image.info["icc_profile"] | ||
|
||
with Image.open("Tests/images/flower2.jxl") as jpeg_image: | ||
expected_icc = jpeg_image.info["icc_profile"] | ||
|
||
assert icc == expected_icc | ||
|
||
|
||
def test_getxmp() -> None: | ||
with Image.open("Tests/images/flower.jxl") as im: | ||
assert "xmp" not in im.info | ||
assert im.getxmp() == {} | ||
|
||
with Image.open("Tests/images/flower2.jxl") as im: | ||
if ElementTree: | ||
assert ( | ||
im.getxmp()["xmpmeta"]["xmptk"] | ||
== "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 " | ||
) | ||
else: | ||
with pytest.warns( | ||
UserWarning, | ||
match="XMP data cannot be read without defusedxml dependency", | ||
): | ||
assert im.getxmp() == {} | ||
|
||
|
||
def test_fix_exif_fail() -> None: | ||
with Image.open("Tests/images/flower2.jxl") as image: | ||
assert image._fix_exif(b"\0\0\0\0") is None | ||
|
||
|
||
def test_read_exif_metadata_empty() -> None: | ||
with Image.open("Tests/images/hopper.jxl") as image: | ||
assert image._getexif() is None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
from io import BytesIO | ||
|
||
from PIL import Image | ||
|
||
from .helper import PillowLeakTestCase, skip_unless_feature | ||
|
||
TEST_FILE = "Tests/images/hopper.jxl" | ||
|
||
|
||
@skip_unless_feature("jpegxl") | ||
class TestJpegXlLeaks(PillowLeakTestCase): | ||
mem_limit = 6 * 1024 # kb | ||
iterations = 1000 | ||
|
||
def test_leak_load(self) -> None: | ||
with open(TEST_FILE, "rb") as f: | ||
im_data = f.read() | ||
|
||
def core() -> None: | ||
with Image.open(BytesIO(im_data)) as im: | ||
im.load() | ||
|
||
self._test_leak(core) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this purely added to copy test_webp_leaks.py? It's not a scenario that is tested for most formats, so I'm not sure it's necessary.