Pillow/Tests/test_file_jpeg2k.py

236 lines
6.2 KiB
Python
Raw Normal View History

2020-02-22 19:07:04 +03:00
import re
from io import BytesIO
2014-03-19 16:17:14 +04:00
import pytest
2019-10-12 16:29:10 +03:00
from PIL import Image, ImageFile, Jpeg2KImagePlugin, features
from .helper import (
assert_image_equal,
assert_image_similar,
is_big_endian,
on_ci,
skip_unless_feature,
)
2014-03-19 16:17:14 +04:00
2020-03-02 17:02:19 +03:00
pytestmark = skip_unless_feature("jpg_2000")
2019-06-13 18:54:11 +03:00
test_card = Image.open("Tests/images/test-card.png")
2014-03-19 16:17:14 +04:00
test_card.load()
2014-06-10 13:10:47 +04:00
# OpenJPEG 2.0.0 outputs this debugging message sometimes; we should
# ignore it---it doesn't represent a test failure.
# 'Not enough memory to handle tile data'
2020-03-02 17:02:19 +03:00
def roundtrip(im, **options):
out = BytesIO()
im.save(out, "JPEG2000", **options)
test_bytes = out.tell()
out.seek(0)
im = Image.open(out)
im.bytes = test_bytes # for testing only
im.load()
return im
def test_sanity():
# Internal version number
2019-10-12 16:29:10 +03:00
assert re.search(r"\d+\.\d+\.\d+$", features.version_codec("jpg_2000"))
2020-03-02 17:02:19 +03:00
with Image.open("Tests/images/test-card-lossless.jp2") as im:
px = im.load()
assert px[0, 0] == (0, 0, 0)
assert im.mode == "RGB"
assert im.size == (640, 480)
assert im.format == "JPEG2000"
assert im.get_format_mimetype() == "image/jp2"
def test_jpf():
with Image.open("Tests/images/balloon.jpf") as im:
assert im.format == "JPEG2000"
assert im.get_format_mimetype() == "image/jpx"
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
Jpeg2KImagePlugin.Jpeg2KImageFile(invalid_file)
def test_bytesio():
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
data = BytesIO(f.read())
with Image.open(data) as im:
2014-06-10 13:10:47 +04:00
im.load()
assert_image_similar(im, test_card, 1.0e-3)
2014-06-10 13:10:47 +04:00
2020-03-02 17:02:19 +03:00
# These two test pre-written JPEG 2000 files that were not written with
# PIL (they were made using Adobe Photoshop)
2014-06-10 13:10:47 +04:00
2020-03-02 17:02:19 +03:00
def test_lossless(tmp_path):
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.load()
outfile = str(tmp_path / "temp_test-card.png")
im.save(outfile)
assert_image_similar(im, test_card, 1.0e-3)
def test_lossy_tiled():
with Image.open("Tests/images/test-card-lossy-tiled.jp2") as im:
im.load()
assert_image_similar(im, test_card, 2.0)
2014-06-10 13:10:47 +04:00
2020-03-02 17:02:19 +03:00
def test_lossless_rt():
im = roundtrip(test_card)
assert_image_equal(im, test_card)
2014-06-10 13:10:47 +04:00
2019-07-13 06:50:13 +03:00
2020-03-02 17:02:19 +03:00
def test_lossy_rt():
im = roundtrip(test_card, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
2014-06-10 13:10:47 +04:00
2020-03-02 17:02:19 +03:00
def test_tiled_rt():
im = roundtrip(test_card, tile_size=(128, 128))
assert_image_equal(im, test_card)
def test_tiled_offset_rt():
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(32, 32))
assert_image_equal(im, test_card)
def test_tiled_offset_too_small():
with pytest.raises(ValueError):
roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(128, 32))
2014-06-10 13:10:47 +04:00
2020-03-02 17:02:19 +03:00
def test_irreversible_rt():
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
def test_prog_qual_rt():
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression="LRCP")
assert_image_similar(im, test_card, 2.0)
def test_prog_res_rt():
im = roundtrip(test_card, num_resolutions=8, progression="RLCP")
assert_image_equal(im, test_card)
def test_reduce():
with Image.open("Tests/images/test-card-lossless.jp2") as im:
2020-03-24 11:19:46 +03:00
assert callable(im.reduce)
2020-03-02 17:02:19 +03:00
im.reduce = 2
2020-03-24 11:19:46 +03:00
assert im.reduce == 2
2020-03-02 17:02:19 +03:00
im.load()
assert im.size == (160, 120)
2014-06-10 13:10:47 +04:00
2020-03-29 08:29:34 +03:00
im.thumbnail((40, 40))
assert im.size == (40, 30)
2020-03-24 11:19:46 +03:00
2020-03-02 17:02:19 +03:00
def test_layers_type(tmp_path):
outfile = str(tmp_path / "temp_layers.jp2")
for quality_layers in [[100, 50, 10], (100, 50, 10), None]:
test_card.save(outfile, quality_layers=quality_layers)
for quality_layers in ["quality_layers", ("100", "50", "10")]:
with pytest.raises(ValueError):
2018-11-16 15:31:42 +03:00
test_card.save(outfile, quality_layers=quality_layers)
2020-03-02 17:02:19 +03:00
def test_layers():
out = BytesIO()
test_card.save(out, "JPEG2000", quality_layers=[100, 50, 10], progression="LRCP")
out.seek(0)
with Image.open(out) as im:
im.layers = 1
im.load()
assert_image_similar(im, test_card, 13)
out.seek(0)
with Image.open(out) as im:
im.layers = 3
im.load()
assert_image_similar(im, test_card, 0.4)
def test_rgba():
# Arrange
with Image.open("Tests/images/rgb_trns_ycbc.j2k") as j2k:
with Image.open("Tests/images/rgb_trns_ycbc.jp2") as jp2:
# Act
j2k.load()
2019-11-25 23:03:23 +03:00
jp2.load()
2020-03-02 17:02:19 +03:00
# Assert
assert j2k.mode == "RGBA"
assert jp2.mode == "RGBA"
2020-03-02 17:02:19 +03:00
def test_16bit_monochrome_has_correct_mode():
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
j2k.load()
assert j2k.mode == "I;16"
2020-03-02 17:02:19 +03:00
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
jp2.load()
assert jp2.mode == "I;16"
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_16bit_monochrome_jp2_like_tiff():
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
2020-03-02 17:02:19 +03:00
assert_image_similar(jp2, tiff_16bit, 1e-3)
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
def test_16bit_monochrome_j2k_like_tiff():
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
assert_image_similar(j2k, tiff_16bit, 1e-3)
def test_16bit_j2k_roundtrips():
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
im = roundtrip(j2k)
assert_image_equal(im, j2k)
def test_16bit_jp2_roundtrips():
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
im = roundtrip(jp2)
assert_image_equal(im, jp2)
def test_unbound_local():
# prepatch, a malformed jp2 file could cause an UnboundLocalError exception.
with pytest.raises(OSError):
2020-03-02 17:02:19 +03:00
Image.open("Tests/images/unbound_variable.jp2")
def test_parser_feed():
# Arrange
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
data = f.read()
# Act
p = ImageFile.Parser()
p.feed(data)
# Assert
assert p.image.size == (640, 480)