2019-07-06 23:40:53 +03:00
|
|
|
from io import BytesIO
|
2014-03-19 16:17:14 +04:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
import pytest
|
2020-02-17 21:31:38 +03:00
|
|
|
from PIL import Image, ImageFile, Jpeg2KImagePlugin
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
|
|
|
PillowTestCase,
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_similar,
|
|
|
|
is_big_endian,
|
|
|
|
on_ci,
|
2020-02-18 01:03:32 +03:00
|
|
|
skip_unless_feature,
|
2020-01-30 17:56:07 +03:00
|
|
|
)
|
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'
|
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@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
|
2019-06-13 18:54:11 +03:00
|
|
|
self.assertRegex(Image.core.jp2klib_version, r"\d+\.\d+\.\d+$")
|
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()
|
|
|
|
self.assertEqual(px[0, 0], (0, 0, 0))
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
|
|
|
self.assertEqual(im.size, (640, 480))
|
|
|
|
self.assertEqual(im.format, "JPEG2000")
|
|
|
|
self.assertEqual(im.get_format_mimetype(), "image/jp2")
|
2019-01-02 07:39:39 +03:00
|
|
|
|
|
|
|
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:
|
|
|
|
self.assertEqual(im.format, "JPEG2000")
|
|
|
|
self.assertEqual(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"
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
self.assertRaises(SyntaxError, Jpeg2KImagePlugin.Jpeg2KImageFile, invalid_file)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2014-07-01 22:09:20 +04:00
|
|
|
def test_bytesio(self):
|
2019-06-13 18:54:11 +03:00
|
|
|
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
|
2014-07-01 22:09:20 +04:00
|
|
|
data = BytesIO(f.read())
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(data) as im:
|
|
|
|
im.load()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(im, test_card, 1.0e-3)
|
2014-07-01 22:09:20 +04:00
|
|
|
|
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)
|
2020-01-30 17:56:07 +03:00
|
|
|
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()
|
2020-01-30 17:56:07 +03:00
|
|
|
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)
|
2020-01-30 17:56:07 +03:00
|
|
|
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])
|
2020-01-30 17:56:07 +03:00
|
|
|
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))
|
2020-01-30 17:56:07 +03:00
|
|
|
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)
|
|
|
|
)
|
2020-01-30 17:56:07 +03:00
|
|
|
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 self.assertRaises(ValueError):
|
|
|
|
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])
|
2020-01-30 17:56:07 +03:00
|
|
|
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")
|
2020-01-30 17:56:07 +03:00
|
|
|
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")
|
2020-01-30 17:56:07 +03:00
|
|
|
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()
|
|
|
|
self.assertEqual(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")]:
|
|
|
|
self.assertRaises(
|
|
|
|
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()
|
2020-01-30 17:56:07 +03:00
|
|
|
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()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(im, test_card, 0.4)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-06-23 14:39:27 +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:
|
2014-06-23 14:39:27 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Act
|
|
|
|
j2k.load()
|
|
|
|
jp2.load()
|
2014-06-23 14:39:27 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# Assert
|
|
|
|
self.assertEqual(j2k.mode, "RGBA")
|
|
|
|
self.assertEqual(jp2.mode, "RGBA")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-06-25 22:06:56 +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()
|
|
|
|
self.assertEqual(j2k.mode, "I;16")
|
2014-06-25 22:06:56 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
|
|
|
|
jp2.load()
|
|
|
|
self.assertEqual(jp2.mode, "I;16")
|
2014-06-25 22:06:56 +04:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
@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:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(jp2, tiff_16bit, 1e-3)
|
2014-06-25 22:06:56 +04:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
@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:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(j2k, tiff_16bit, 1e-3)
|
2014-06-25 22:06:56 +04:00
|
|
|
|
|
|
|
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)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im, j2k)
|
2014-06-25 22:06:56 +04:00
|
|
|
|
|
|
|
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)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im, jp2)
|
2014-06-25 22:06:56 +04:00
|
|
|
|
2016-03-26 23:41:00 +03:00
|
|
|
def test_unbound_local(self):
|
|
|
|
# prepatch, a malformed jp2 file could cause an UnboundLocalError
|
|
|
|
# exception.
|
2016-11-06 19:58:45 +03:00
|
|
|
with self.assertRaises(IOError):
|
2019-06-13 18:54:11 +03:00
|
|
|
Image.open("Tests/images/unbound_variable.jp2")
|
2014-06-25 22:06:56 +04:00
|
|
|
|
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
|
|
|
|
self.assertEqual(p.image.size, (640, 480))
|