Skip to content

Commit 41f2921

Browse files
committed
Use monkeypatch
1 parent 7125fe4 commit 41f2921

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

Tests/test_file_jxl.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,29 @@
66

77
from PIL import Image, JpegXlImagePlugin, features
88

9-
from .helper import (
10-
assert_image_similar_tofile,
11-
skip_unless_feature,
12-
)
9+
from .helper import assert_image_similar_tofile, skip_unless_feature
1310

1411
try:
1512
from PIL import _jpegxl
16-
17-
HAVE_JPEGXL = True
1813
except ImportError:
19-
HAVE_JPEGXL = False
14+
pass
2015

2116
# cjxl v0.9.2 41b8cdab
2217
# hopper.jxl: cjxl hopper.png hopper.jxl -q 75 -e 8
2318
# 16_bit_binary.jxl: cjxl 16_bit_binary.pgm 16_bit_binary.jxl -q 100 -e 9
2419

2520

2621
class TestUnsupportedJpegXl:
27-
def test_unsupported(self) -> None:
28-
if HAVE_JPEGXL:
29-
JpegXlImagePlugin.SUPPORTED = False
22+
def test_unsupported(self, monkeypatch: pytest.MonkeyPatch) -> None:
23+
monkeypatch.setattr(JpegXlImagePlugin, "SUPPORTED", False)
3024

31-
file_path = "Tests/images/hopper.jxl"
3225
with pytest.raises(OSError):
33-
with Image.open(file_path):
26+
with Image.open("Tests/images/hopper.jxl"):
3427
pass
3528

36-
if HAVE_JPEGXL:
37-
JpegXlImagePlugin.SUPPORTED = True
38-
3929

4030
@skip_unless_feature("jpegxl")
4131
class TestFileJpegXl:
42-
def setup_method(self) -> None:
43-
self.rgb_mode = "RGB"
44-
self.i16_mode = "I;16"
45-
4632
def test_version(self) -> None:
4733
_jpegxl.JpegXlDecoderVersion()
4834
version = features.version_module("jpegxl")
@@ -56,30 +42,30 @@ def test_read_rgb(self) -> None:
5642
"""
5743

5844
with Image.open("Tests/images/hopper.jxl") as image:
59-
assert image.mode == self.rgb_mode
45+
assert image.mode == "RGB"
6046
assert image.size == (128, 128)
6147
assert image.format == "JPEG XL"
6248
image.load()
6349
image.getdata()
6450

6551
# generated with:
6652
# djxl hopper.jxl hopper_jxl_bits.ppm
67-
assert_image_similar_tofile(image, "Tests/images/hopper_jxl_bits.ppm", 1.0)
53+
assert_image_similar_tofile(image, "Tests/images/hopper_jxl_bits.ppm", 1)
6854

6955
def test_read_i16(self) -> None:
7056
"""
7157
Can we read 16-bit Grayscale Jpeg XL image?
7258
"""
7359

7460
with Image.open("Tests/images/jxl/16bit_subcutaneous.cropped.jxl") as image:
75-
assert image.mode == self.i16_mode
61+
assert image.mode == "I;16"
7662
assert image.size == (128, 64)
7763
assert image.format == "JPEG XL"
7864
image.load()
7965
image.getdata()
8066

8167
assert_image_similar_tofile(
82-
image, "Tests/images/jxl/16bit_subcutaneous.cropped.png", 1.0
68+
image, "Tests/images/jxl/16bit_subcutaneous.cropped.png", 1
8369
)
8470

8571
def test_JpegXlDecode_with_invalid_args(self) -> None:

Tests/test_file_jxl_alpha.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

3-
import pytest
4-
53
from PIL import Image
64

7-
from .helper import assert_image_similar_tofile
5+
from .helper import assert_image_similar_tofile, skip_unless_feature
86

9-
_jpegxl = pytest.importorskip("PIL._jpegxl", reason="JPEG XL support not installed")
7+
pytestmark = [skip_unless_feature("jpegxl")]
108

119

1210
def test_read_rgba() -> None:
@@ -16,14 +14,13 @@ def test_read_rgba() -> None:
1614
"""
1715

1816
# Generated with `cjxl transparent.png transparent.jxl -q 100 -e 8`
19-
file_path = "Tests/images/transparent.jxl"
20-
with Image.open(file_path) as image:
21-
assert image.mode == "RGBA"
22-
assert image.size == (200, 150)
23-
assert image.format == "JPEG XL"
24-
image.load()
25-
image.getdata()
17+
with Image.open("Tests/images/transparent.jxl") as im:
18+
assert im.mode == "RGBA"
19+
assert im.size == (200, 150)
20+
assert im.format == "JPEG XL"
21+
im.load()
22+
im.getdata()
2623

27-
image.tobytes()
24+
im.tobytes()
2825

29-
assert_image_similar_tofile(image, "Tests/images/transparent.png", 1.0)
26+
assert_image_similar_tofile(im, "Tests/images/transparent.png", 1)

Tests/test_file_jxl_animated.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
from PIL import Image
66

7-
from .helper import (
8-
assert_image_equal,
9-
skip_unless_feature,
10-
)
7+
from .helper import assert_image_equal, skip_unless_feature
118

12-
pytestmark = [
13-
skip_unless_feature("jpegxl"),
14-
]
9+
pytestmark = [skip_unless_feature("jpegxl")]
1510

1611

1712
def test_n_frames() -> None:
@@ -27,7 +22,6 @@ def test_n_frames() -> None:
2722

2823

2924
def test_float_duration() -> None:
30-
3125
with Image.open("Tests/images/iss634.jxl") as im:
3226
im.load()
3327
assert im.info["duration"] == 70

Tests/test_file_jxl_metadata.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
from .helper import skip_unless_feature
1010

11-
pytestmark = [
12-
skip_unless_feature("jpegxl"),
13-
]
11+
pytestmark = [skip_unless_feature("jpegxl")]
1412

1513
ElementTree: ModuleType | None
1614
try:

src/PIL/JpegXlImagePlugin.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,28 @@ class JpegXlImageFile(ImageFile.ImageFile):
4141
def _open(self) -> None:
4242
self._decoder = _jpegxl.PILJpegXlDecoder(self.fp.read())
4343

44-
width, height, mode, has_anim, tps_num, tps_denom, n_loops, n_frames = (
45-
self._decoder.get_info()
46-
)
44+
(
45+
width,
46+
height,
47+
self._mode,
48+
self.is_animated,
49+
tps_num,
50+
tps_denom,
51+
n_loops,
52+
n_frames,
53+
) = self._decoder.get_info()
4754
self._size = width, height
4855
self.info["loop"] = n_loops
49-
self.is_animated = has_anim
5056

5157
self._tps_dur_secs = 1
52-
self.n_frames: int | None = 1
58+
self.n_frames = 1
5359
if self.is_animated:
54-
self.n_frames = None
55-
if n_frames > 0:
56-
self.n_frames = n_frames
57-
self._tps_dur_secs = tps_num / tps_denom
60+
self.n_frames = n_frames
61+
self._tps_dur_secs = tps_num / tps_denom
5862

5963
# TODO: handle libjxl time codes
6064
self.__timestamp = 0
6165

62-
self._mode = mode
63-
6466
if icc := self._decoder.get_icc():
6567
self.info["icc_profile"] = icc
6668
if exif := self._decoder.get_exif():
@@ -115,7 +117,6 @@ def _seek_check(self, frame: int) -> bool:
115117
return self.tell() != frame
116118

117119
def _seek(self, frame: int) -> None:
118-
# print("_seek: phy: {}, fr: {}".format(self.__physical_frame, frame))
119120
if frame == self.__physical_frame:
120121
return # Nothing to do
121122
if frame < self.__physical_frame:

0 commit comments

Comments
 (0)