Pillow/Tests/test_file_jxl.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-03-02 02:56:38 +03:00
import re
2024-03-02 02:56:38 +03:00
import pytest
2024-03-20 00:17:35 +03:00
from PIL import Image, JpegXlImagePlugin, features
2024-03-02 02:56:38 +03:00
from .helper import (
assert_image_similar_tofile,
skip_unless_feature,
)
try:
2024-03-20 00:17:35 +03:00
from PIL import _jpegxl
2024-03-02 02:56:38 +03:00
2024-03-20 00:17:35 +03:00
HAVE_JPEGXL = True
2024-03-02 02:56:38 +03:00
except ImportError:
2024-03-20 00:17:35 +03:00
HAVE_JPEGXL = False
2024-03-02 02:56:38 +03:00
# 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
2024-03-02 02:56:38 +03:00
2024-03-20 00:17:35 +03:00
class TestUnsupportedJpegXl:
2024-03-02 02:56:38 +03:00
def test_unsupported(self) -> None:
2024-03-20 00:17:35 +03:00
if HAVE_JPEGXL:
JpegXlImagePlugin.SUPPORTED = False
2024-03-02 02:56:38 +03:00
file_path = "Tests/images/hopper.jxl"
2024-03-20 00:17:35 +03:00
with pytest.raises(OSError):
with Image.open(file_path):
pass
2024-03-02 02:56:38 +03:00
2024-03-20 00:17:35 +03:00
if HAVE_JPEGXL:
JpegXlImagePlugin.SUPPORTED = True
2024-03-02 02:56:38 +03:00
2024-03-20 00:17:35 +03:00
@skip_unless_feature("jpegxl")
class TestFileJpegXl:
2024-03-02 02:56:38 +03:00
def setup_method(self) -> None:
self.rgb_mode = "RGB"
self.i16_mode = "I;16"
2024-03-02 02:56:38 +03:00
def test_version(self) -> None:
2024-03-20 00:17:35 +03:00
_jpegxl.JpegXlDecoderVersion()
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("jpegxl"))
2024-03-02 02:56:38 +03:00
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
)
2024-03-20 00:17:35 +03:00
def test_JpegXlDecode_with_invalid_args(self) -> None:
2024-03-02 02:56:38 +03:00
"""
Calling decoder functions with no arguments should result in an error.
"""
with pytest.raises(TypeError):
2024-03-20 00:17:35 +03:00
_jpegxl.PILJpegXlDecoder()