Pillow/Tests/test_file_jpeg2k.py

211 lines
6.9 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
from PIL import Image, ImageFile, Jpeg2KImagePlugin
from .helper import (
PillowTestCase,
assert_image_equal,
assert_image_similar,
is_big_endian,
on_ci,
skip_unless_feature,
)
2014-03-19 16:17:14 +04:00
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'
@skip_unless_feature("jpg_2000")
2014-06-10 13:10:47 +04:00
class TestFileJpeg2k(PillowTestCase):
def roundtrip(self, im, **options):
out = BytesIO()
im.save(out, "JPEG2000", **options)
2015-04-24 11:24:52 +03:00
test_bytes = out.tell()
2014-06-10 13:10:47 +04:00
out.seek(0)
im = Image.open(out)
2015-04-24 11:24:52 +03:00
im.bytes = test_bytes # for testing only
2014-06-10 13:10:47 +04:00
im.load()
return im
def test_sanity(self):
# Internal version number
2020-02-22 19:07:04 +03:00
assert re.search(r"\d+\.\d+\.\d+$", Image.core.jp2klib_version)
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +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(self):
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
with Image.open("Tests/images/balloon.jpf") as im:
assert im.format == "JPEG2000"
assert im.get_format_mimetype() == "image/jpx"
2014-06-10 13:10:47 +04:00
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
2015-07-03 09:22:56 +03:00
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
Jpeg2KImagePlugin.Jpeg2KImageFile(invalid_file)
2015-07-03 08:03:25 +03:00
def test_bytesio(self):
2019-06-13 18:54:11 +03:00
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
data = BytesIO(f.read())
2019-11-25 23:03:23 +03:00
with Image.open(data) as im:
im.load()
assert_image_similar(im, test_card, 1.0e-3)
2014-06-10 13:10:47 +04:00
# These two test pre-written JPEG 2000 files that were not written with
# PIL (they were made using Adobe Photoshop)
def test_lossless(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.load()
outfile = self.tempfile("temp_test-card.png")
im.save(outfile)
assert_image_similar(im, test_card, 1.0e-3)
2014-06-10 13:10:47 +04:00
def test_lossy_tiled(self):
2019-11-25 23:03:23 +03:00
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
def test_lossless_rt(self):
im = self.roundtrip(test_card)
assert_image_equal(im, test_card)
2014-06-10 13:10:47 +04:00
def test_lossy_rt(self):
im = self.roundtrip(test_card, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
2014-06-10 13:10:47 +04:00
def test_tiled_rt(self):
im = self.roundtrip(test_card, tile_size=(128, 128))
assert_image_equal(im, test_card)
2014-06-10 13:10:47 +04:00
def test_tiled_offset_rt(self):
im = self.roundtrip(
2019-06-13 18:54:11 +03:00
test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(32, 32)
)
assert_image_equal(im, test_card)
2014-06-10 13:10:47 +04:00
2019-07-13 06:50:13 +03:00
def test_tiled_offset_too_small(self):
with pytest.raises(ValueError):
2019-07-13 06:50:13 +03:00
self.roundtrip(
test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(128, 32)
)
2014-06-10 13:10:47 +04:00
def test_irreversible_rt(self):
im = self.roundtrip(test_card, irreversible=True, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
2014-06-10 13:10:47 +04:00
def test_prog_qual_rt(self):
2019-06-13 18:54:11 +03:00
im = self.roundtrip(test_card, quality_layers=[60, 40, 20], progression="LRCP")
assert_image_similar(im, test_card, 2.0)
2014-06-10 13:10:47 +04:00
def test_prog_res_rt(self):
2019-06-13 18:54:11 +03:00
im = self.roundtrip(test_card, num_resolutions=8, progression="RLCP")
assert_image_equal(im, test_card)
2014-06-10 13:10:47 +04:00
def test_reduce(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.reduce = 2
im.load()
assert im.size == (160, 120)
2014-06-10 13:10:47 +04:00
2018-11-16 15:31:42 +03:00
def test_layers_type(self):
2019-06-13 18:54:11 +03:00
outfile = self.tempfile("temp_layers.jp2")
for quality_layers in [[100, 50, 10], (100, 50, 10), None]:
2018-11-16 15:31:42 +03:00
test_card.save(outfile, quality_layers=quality_layers)
2019-06-13 18:54:11 +03:00
for quality_layers in ["quality_layers", ("100", "50", "10")]:
with pytest.raises(ValueError):
test_card.save(outfile, quality_layers=quality_layers)
2018-11-16 15:31:42 +03:00
2014-06-10 13:10:47 +04:00
def test_layers(self):
out = BytesIO()
2019-06-13 18:54:11 +03:00
test_card.save(
out, "JPEG2000", quality_layers=[100, 50, 10], progression="LRCP"
)
2014-06-10 13:10:47 +04:00
out.seek(0)
2019-11-25 23:03:23 +03:00
with Image.open(out) as im:
im.layers = 1
im.load()
assert_image_similar(im, test_card, 13)
2014-06-10 13:10:47 +04:00
out.seek(0)
2019-11-25 23:03:23 +03:00
with Image.open(out) as im:
im.layers = 3
im.load()
assert_image_similar(im, test_card, 0.4)
2014-06-10 13:10:47 +04:00
def test_rgba(self):
# Arrange
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/rgb_trns_ycbc.j2k") as j2k:
with Image.open("Tests/images/rgb_trns_ycbc.jp2") as jp2:
2019-11-25 23:03:23 +03:00
# Act
j2k.load()
jp2.load()
2019-11-25 23:03:23 +03:00
# Assert
assert j2k.mode == "RGBA"
assert jp2.mode == "RGBA"
2014-06-10 13:10:47 +04:00
def test_16bit_monochrome_has_correct_mode(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
j2k.load()
assert j2k.mode == "I;16"
2019-11-25 23:03:23 +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")
2018-08-11 09:39:49 +03:00
def test_16bit_monochrome_jp2_like_tiff(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
assert_image_similar(jp2, tiff_16bit, 1e-3)
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
2018-08-11 09:39:49 +03:00
def test_16bit_monochrome_j2k_like_tiff(self):
2019-11-25 23:03:23 +03:00
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(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
im = self.roundtrip(j2k)
assert_image_equal(im, j2k)
def test_16bit_jp2_roundtrips(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
im = self.roundtrip(jp2)
assert_image_equal(im, jp2)
def test_unbound_local(self):
# prepatch, a malformed jp2 file could cause an UnboundLocalError
# exception.
with pytest.raises(IOError):
2019-06-13 18:54:11 +03:00
Image.open("Tests/images/unbound_variable.jp2")
2018-03-15 16:25:51 +03:00
def test_parser_feed(self):
# Arrange
2019-06-13 18:54:11 +03:00
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
2018-03-15 16:25:51 +03:00
data = f.read()
# Act
p = ImageFile.Parser()
p.feed(data)
# Assert
assert p.image.size == (640, 480)