Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
8e0c5db
add tests
olokelo Mar 1, 2024
a57ebea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 2, 2024
23fb57d
minor fixes, linting corrections
olokelo Mar 2, 2024
2eb5987
Added type hints
radarhere Mar 6, 2024
37b58f3
Removed feature
radarhere Mar 6, 2024
eeaecb4
Merge pull request #1 from radarhere/jxl-support2
olokelo Mar 11, 2024
24b63ad
fix goto labels for clang
olokelo Mar 11, 2024
0b50410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
f403672
Merge branch 'main' into jxl-support2
hugovk Mar 19, 2024
f6086d4
modify leak test
olokelo Mar 19, 2024
5320450
fix _jxl_decoder_count_frames
olokelo Mar 19, 2024
1b049ab
minor plugin code tweaks
olokelo Mar 19, 2024
6048520
add type hints
olokelo Mar 19, 2024
8fa280f
rename jxl -> jpegxl
olokelo Mar 19, 2024
58c37bf
add test case for seeking to the same frame
olokelo Mar 19, 2024
48bbc2e
flip cases in metadata test
olokelo Mar 19, 2024
443a352
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 19, 2024
62c58c2
fix some type hinting mistakes
olokelo Mar 19, 2024
8cab1c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 19, 2024
e5003ff
change Optional to python 3.10+ syntax
olokelo Mar 19, 2024
fa5bfac
add more metadata test cases
olokelo Mar 20, 2024
0b71605
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 20, 2024
1f00fb8
add 16-bits grayscale support for jpeg xl images
olokelo May 18, 2024
08270a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 18, 2024
9313587
Merge branch 'main' into jxl-support2
radarhere May 22, 2024
4256b2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2024
8a1c03e
Merge branch 'main' into jxl-support2
radarhere Aug 14, 2024
13944d5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2024
bb06057
Merge branch 'main' into jxl-support2
radarhere Sep 11, 2024
bc4a794
Merge branch 'main' into jxl-support2
radarhere Apr 26, 2025
ff269ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 26, 2025
661c0d8
Lint fixes
radarhere Apr 26, 2025
ade1db0
Replace slice and comparison with startswith
radarhere May 7, 2025
ceec3f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
5e0457a
Removed getxmp()
radarhere May 7, 2025
9266318
Do not set info["exif"] to None
radarhere May 7, 2025
29e4b55
Do not add _getexif to new plugin
radarhere May 7, 2025
3cd3848
Added type hint
radarhere May 7, 2025
aa6510f
Removed self.rawmode
radarhere May 7, 2025
36640de
tile is already empty list
radarhere May 7, 2025
7125fe4
Fixed type hints
radarhere May 7, 2025
05aee33
Use monkeypatch
radarhere May 7, 2025
6c3f0b5
Use member names to initialize module
radarhere May 9, 2025
80e9963
Use member names to initialize PyTypeObject
radarhere May 9, 2025
29c1e4c
Removed C method unused by Python
radarhere May 9, 2025
c8409e0
Simplified code
radarhere May 9, 2025
61ce5c2
is_animated should be a bool
radarhere May 10, 2025
bf0cdb2
Test on Linux
radarhere May 12, 2025
79f941d
Added argument that was removed in 0.9.0
radarhere May 12, 2025
b5d64e8
Removed specific leak check
radarhere Jun 15, 2025
ccca015
Use multi-phase initialization
radarhere Jun 25, 2025
ece4065
Merge branch 'main' into jxl-support2
radarhere Jun 25, 2025
e99989f
Merge branch 'main' into jxl-support2
radarhere Aug 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tests/images/flower.jxl
Binary file not shown.
Binary file added Tests/images/flower2.jxl
Binary file not shown.
Binary file added Tests/images/hopper.jxl
Binary file not shown.
Binary file added Tests/images/hopper_jxl_bits.ppm
Binary file not shown.
Binary file added Tests/images/iss634.jxl
Binary file not shown.
Binary file added Tests/images/jxl/traffic_light.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/jxl/traffic_light.jxl
Binary file not shown.
Binary file added Tests/images/transparent.jxl
Binary file not shown.
72 changes: 72 additions & 0 deletions Tests/test_file_jxl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from __future__ import annotations

import re

import pytest

from PIL import Image, JxlImagePlugin, features

from .helper import (
assert_image_similar_tofile,
skip_unless_feature,
)

try:
from PIL import _jxl

HAVE_JXL = True
except ImportError:
HAVE_JXL = False

# cjxl v0.9.2 41b8cdab
# hopper.jxl: cjxl hopper.png hopper.jxl -q 75 -e 8


class TestUnsupportedJxl:
def test_unsupported(self) -> None:
if HAVE_JXL:
JxlImagePlugin.SUPPORTED = False

file_path = "Tests/images/hopper.jxl"
with pytest.warns(UserWarning):
with pytest.raises(OSError):
with Image.open(file_path):
pass

if HAVE_JXL:
JxlImagePlugin.SUPPORTED = True


@skip_unless_feature("jxl")
class TestFileJxl:
def setup_method(self) -> None:
self.rgb_mode = "RGB"

def test_version(self) -> None:
_jxl.JxlDecoderVersion()
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("jxl"))

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_JxlDecode_with_invalid_args(self) -> None:
"""
Calling decoder functions with no arguments should result in an error.
"""

with pytest.raises(TypeError):
_jxl.PILJxlDecoder()
29 changes: 29 additions & 0 deletions Tests/test_file_jxl_alpha.py
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

_webp = pytest.importorskip("PIL._jxl", reason="JXL 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)
69 changes: 69 additions & 0 deletions Tests/test_file_jxl_animated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from __future__ import annotations

import pytest

from PIL import Image

from .helper import (
assert_image_equal,
skip_unless_feature,
)

pytestmark = [
skip_unless_feature("jxl"),
]


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


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)
89 changes: 89 additions & 0 deletions Tests/test_file_jxl_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from __future__ import annotations

from types import ModuleType

import pytest

from PIL import Image

from .helper import skip_unless_feature

pytestmark = [
skip_unless_feature("jxl"),
]

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 is None:
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
assert (
im.getxmp()["xmpmeta"]["xmptk"]
== "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 "
)
27 changes: 27 additions & 0 deletions Tests/test_jxl_leaks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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("jxl")
class TestJxlLeaks(PillowLeakTestCase):
# TODO: lower the limit, I'm not sure what is correct limit
# since I have libjxl debug system-wide
mem_limit = 16 * 1024 # kb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we find out the correct limit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on release version of libjxl and I've set it to 6 megabytes which seem to work well, that's also similar to memory usage of djxl for decoding hopper.jxl.
I think libjxl memory usage could depend also on number of decoding threads used. By default we use all available cores.

iterations = 100

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)
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class feature:
features = [
"zlib",
"jpeg",
"jxl",
"tiff",
"freetype",
"raqm",
Expand Down Expand Up @@ -691,6 +692,14 @@ def build_extensions(self):
feature.jpeg2000 = "openjp2"
feature.openjpeg_version = ".".join(str(x) for x in best_version)

if feature.want("jxl"):
_dbg("Looking for jxl")
if _find_include_file(self, "jxl/encode.h") and _find_include_file(
self, "jxl/decode.h"
):
if _find_library_file(self, "jxl"):
feature.jxl = "jxl jxl_threads"

if feature.want("imagequant"):
_dbg("Looking for imagequant")
if _find_include_file(self, "libimagequant.h"):
Expand Down Expand Up @@ -774,6 +783,15 @@ def build_extensions(self):
# alternate Windows name.
feature.lcms = "lcms2_static"

if feature.jxl:
# jxl and jxl_threads are required
libs = feature.jxl.split()
defs = []

self._update_extension("PIL._jxl", libs, defs)
else:
self._remove_extension("PIL._jxl")

if feature.want("webp"):
_dbg("Looking for webp")
if _find_include_file(self, "webp/encode.h") and _find_include_file(
Expand Down Expand Up @@ -934,6 +952,7 @@ def summary_report(self, feature):
(feature.freetype, "FREETYPE2"),
(feature.raqm, "RAQM (Text shaping)", raqm_extra_info),
(feature.lcms, "LITTLECMS2"),
(feature.jxl, "JXL"),
(feature.webp, "WEBP"),
(feature.webpmux, "WEBPMUX"),
(feature.xcb, "XCB (X protocol)"),
Expand Down Expand Up @@ -978,6 +997,7 @@ def debug_build():
Extension("PIL._imaging", files),
Extension("PIL._imagingft", ["src/_imagingft.c"]),
Extension("PIL._imagingcms", ["src/_imagingcms.c"]),
Extension("PIL._jxl", ["src/_jxl.c"]),
Extension("PIL._webp", ["src/_webp.c"]),
Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]),
Extension("PIL._imagingmath", ["src/_imagingmath.c"]),
Expand Down
Loading