Pillow/Tests/test_file_jpeg2k.py

206 lines
6.4 KiB
Python
Raw Normal View History

from .helper import PillowTestCase
2014-03-19 16:17:14 +04:00
2015-07-03 08:03:25 +03:00
from PIL import Image, Jpeg2KImagePlugin
2014-06-10 13:10:47 +04:00
from io import BytesIO
2014-03-19 16:17:14 +04:00
codecs = dir(Image.core)
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'
class TestFileJpeg2k(PillowTestCase):
def setUp(self):
if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs:
2019-06-13 18:54:11 +03:00
self.skipTest("JPEG 2000 support not available")
2014-06-10 13:10:47 +04:00
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-06-13 18:54:11 +03:00
im = Image.open("Tests/images/test-card-lossless.jp2")
px = im.load()
2016-09-03 05:17:22 +03:00
self.assertEqual(px[0, 0], (0, 0, 0))
2019-06-13 18:54:11 +03:00
self.assertEqual(im.mode, "RGB")
2014-06-10 13:10:47 +04:00
self.assertEqual(im.size, (640, 480))
2019-06-13 18:54:11 +03:00
self.assertEqual(im.format, "JPEG2000")
self.assertEqual(im.get_format_mimetype(), "image/jp2")
def test_jpf(self):
2019-06-13 18:54:11 +03:00
im = Image.open("Tests/images/balloon.jpf")
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
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())
im = Image.open(data)
im.load()
self.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-06-13 18:54:11 +03:00
im = Image.open("Tests/images/test-card-lossless.jp2")
2014-06-10 13:10:47 +04:00
im.load()
2019-06-13 18:54:11 +03:00
outfile = self.tempfile("temp_test-card.png")
2014-08-20 01:13:30 +04:00
im.save(outfile)
2014-06-10 13:10:47 +04:00
self.assert_image_similar(im, test_card, 1.0e-3)
def test_lossy_tiled(self):
2019-06-13 18:54:11 +03:00
im = Image.open("Tests/images/test-card-lossy-tiled.jp2")
2014-06-10 13:10:47 +04:00
im.load()
self.assert_image_similar(im, test_card, 2.0)
def test_lossless_rt(self):
im = self.roundtrip(test_card)
self.assert_image_equal(im, test_card)
def test_lossy_rt(self):
im = self.roundtrip(test_card, quality_layers=[20])
self.assert_image_similar(im, test_card, 2.0)
def test_tiled_rt(self):
im = self.roundtrip(test_card, tile_size=(128, 128))
self.assert_image_equal(im, test_card)
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)
)
2014-06-10 13:10:47 +04:00
self.assert_image_equal(im, test_card)
def test_irreversible_rt(self):
im = self.roundtrip(test_card, irreversible=True, quality_layers=[20])
self.assert_image_similar(im, test_card, 2.0)
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")
2014-06-10 13:10:47 +04:00
self.assert_image_similar(im, test_card, 2.0)
def test_prog_res_rt(self):
2019-06-13 18:54:11 +03:00
im = self.roundtrip(test_card, num_resolutions=8, progression="RLCP")
2014-06-10 13:10:47 +04:00
self.assert_image_equal(im, test_card)
def test_reduce(self):
2019-06-13 18:54:11 +03:00
im = Image.open("Tests/images/test-card-lossless.jp2")
2014-06-10 13:10:47 +04:00
im.reduce = 2
im.load()
self.assertEqual(im.size, (160, 120))
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)
im = Image.open(out)
im.layers = 1
im.load()
self.assert_image_similar(im, test_card, 13)
out.seek(0)
im = Image.open(out)
im.layers = 3
im.load()
self.assert_image_similar(im, test_card, 0.4)
def test_rgba(self):
# Arrange
2019-06-13 18:54:11 +03:00
j2k = Image.open("Tests/images/rgb_trns_ycbc.j2k")
jp2 = Image.open("Tests/images/rgb_trns_ycbc.jp2")
# Act
j2k.load()
jp2.load()
# Assert
2019-06-13 18:54:11 +03:00
self.assertEqual(j2k.mode, "RGBA")
self.assertEqual(jp2.mode, "RGBA")
2014-06-10 13:10:47 +04:00
def test_16bit_monochrome_has_correct_mode(self):
2019-06-13 18:54:11 +03:00
j2k = Image.open("Tests/images/16bit.cropped.j2k")
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
j2k.load()
jp2.load()
2019-06-13 18:54:11 +03:00
self.assertEqual(j2k.mode, "I;16")
self.assertEqual(jp2.mode, "I;16")
2018-08-11 09:39:49 +03:00
def test_16bit_monochrome_jp2_like_tiff(self):
2019-06-13 18:54:11 +03:00
tiff_16bit = Image.open("Tests/images/16bit.cropped.tif")
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
self.assert_image_similar(jp2, tiff_16bit, 1e-3)
2018-08-11 09:39:49 +03:00
def test_16bit_monochrome_j2k_like_tiff(self):
2019-06-13 18:54:11 +03:00
tiff_16bit = Image.open("Tests/images/16bit.cropped.tif")
j2k = Image.open("Tests/images/16bit.cropped.j2k")
self.assert_image_similar(j2k, tiff_16bit, 1e-3)
def test_16bit_j2k_roundtrips(self):
2019-06-13 18:54:11 +03:00
j2k = Image.open("Tests/images/16bit.cropped.j2k")
im = self.roundtrip(j2k)
self.assert_image_equal(im, j2k)
def test_16bit_jp2_roundtrips(self):
2019-06-13 18:54:11 +03:00
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
im = self.roundtrip(jp2)
self.assert_image_equal(im, jp2)
def test_unbound_local(self):
# prepatch, a malformed jp2 file could cause an UnboundLocalError
# exception.
with self.assertRaises(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
from PIL import ImageFile
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))