2020-02-22 19:07:04 +03:00
|
|
|
import re
|
2019-07-06 23:40:53 +03:00
|
|
|
import zlib
|
|
|
|
from io import BytesIO
|
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
from PIL import Image, ImageFile, PngImagePlugin, features
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
from .helper import (
|
|
|
|
PillowLeakTestCase,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image,
|
|
|
|
assert_image_equal,
|
2020-01-05 00:07:59 +03:00
|
|
|
hopper,
|
|
|
|
is_big_endian,
|
|
|
|
is_win32,
|
2020-02-18 01:03:32 +03:00
|
|
|
skip_unless_feature,
|
2020-01-05 00:07:59 +03:00
|
|
|
)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
# sample png stream
|
|
|
|
|
2014-09-04 13:09:52 +04:00
|
|
|
TEST_PNG_FILE = "Tests/images/hopper.png"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
# stuff to create inline PNG images
|
|
|
|
|
|
|
|
MAGIC = PngImagePlugin._MAGIC
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
def chunk(cid, *data):
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = BytesIO()
|
|
|
|
PngImagePlugin.putchunk(*(test_file, cid) + data)
|
|
|
|
return test_file.getvalue()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
o32 = PngImagePlugin.o32
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
IHDR = chunk(b"IHDR", o32(1), o32(1), b"\x08\x02", b"\0\0\0")
|
2012-10-16 00:26:38 +04:00
|
|
|
IDAT = chunk(b"IDAT")
|
|
|
|
IEND = chunk(b"IEND")
|
|
|
|
|
|
|
|
HEAD = MAGIC + IHDR
|
|
|
|
TAIL = IDAT + IEND
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
def load(data):
|
|
|
|
return Image.open(BytesIO(data))
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
def roundtrip(im, **options):
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, "PNG", **options)
|
|
|
|
out.seek(0)
|
|
|
|
return Image.open(out)
|
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2020-03-02 17:02:19 +03:00
|
|
|
class TestFilePng:
|
2017-12-20 13:27:13 +03:00
|
|
|
def get_chunks(self, filename):
|
|
|
|
chunks = []
|
|
|
|
with open(filename, "rb") as fp:
|
|
|
|
fp.read(8)
|
2017-12-20 14:45:52 +03:00
|
|
|
with PngImagePlugin.PngStream(fp) as png:
|
|
|
|
while True:
|
|
|
|
cid, pos, length = png.read()
|
|
|
|
chunks.append(cid)
|
|
|
|
try:
|
|
|
|
s = png.call(cid, pos, length)
|
|
|
|
except EOFError:
|
|
|
|
break
|
|
|
|
png.crc(cid, s)
|
2017-12-20 13:27:13 +03:00
|
|
|
return chunks
|
|
|
|
|
2020-07-16 12:04:16 +03:00
|
|
|
@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_sanity(self, tmp_path):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# internal version number
|
2019-10-12 16:29:10 +03:00
|
|
|
assert re.search(r"\d+\.\d+\.\d+(\.\d+)?$", features.version_codec("zlib"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
hopper("RGB").save(test_file)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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(test_file) as im:
|
|
|
|
im.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == "RGB"
|
|
|
|
assert im.size == (128, 128)
|
|
|
|
assert im.format == "PNG"
|
|
|
|
assert im.get_format_mimetype() == "image/png"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-03-12 09:28:42 +03:00
|
|
|
for mode in ["1", "L", "P", "RGB", "I", "I;16"]:
|
|
|
|
im = hopper(mode)
|
|
|
|
im.save(test_file)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as reloaded:
|
|
|
|
if mode == "I;16":
|
|
|
|
reloaded = reloaded.convert(mode)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(reloaded, im)
|
2012-10-16 00:26:38 +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"
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
PngImagePlugin.PngImageFile(invalid_file)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_broken(self):
|
|
|
|
# Check reading of totally broken files. In this case, the test
|
|
|
|
# file was checked into Subversion as a text file.
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/broken.png"
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
Image.open(test_file)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_bad_text(self):
|
|
|
|
# Make sure PIL can read malformed tEXt chunks (@PIL152)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"tEXt") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"tEXt", b"spam") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"tEXt", b"spam\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"tEXt", b"spam\0egg") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "egg"}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"tEXt", b"spam\0egg\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "egg\x00"}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_bad_ztxt(self):
|
|
|
|
# Test reading malformed zTXt chunks (python-pillow/Pillow#318)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt", b"spam") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt", b"spam\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt", b"spam\0\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt", b"spam\0\0" + zlib.compress(b"egg")[:1]) + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"zTXt", b"spam\0\0" + zlib.compress(b"egg")) + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "egg"}
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2014-07-23 12:09:06 +04:00
|
|
|
def test_bad_itxt(self):
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt", b"spam") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt", b"spam\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt", b"spam\0\x02") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt", b"spam\0\0\0foo\0") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(HEAD + chunk(b"iTXt", b"spam\0\0\0en\0Spam\0egg") + TAIL)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "egg"}
|
|
|
|
assert im.info["spam"].lang == "en"
|
|
|
|
assert im.info["spam"].tkey == "Spam"
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(
|
|
|
|
HEAD
|
|
|
|
+ chunk(b"iTXt", b"spam\0\1\0en\0Spam\0" + zlib.compress(b"egg")[:1])
|
|
|
|
+ TAIL
|
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": ""}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(
|
|
|
|
HEAD
|
|
|
|
+ chunk(b"iTXt", b"spam\0\1\1en\0Spam\0" + zlib.compress(b"egg"))
|
|
|
|
+ TAIL
|
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {}
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
im = load(
|
|
|
|
HEAD
|
|
|
|
+ chunk(b"iTXt", b"spam\0\1\0en\0Spam\0" + zlib.compress(b"egg"))
|
|
|
|
+ TAIL
|
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "egg"}
|
|
|
|
assert im.info["spam"].lang == "en"
|
|
|
|
assert im.info["spam"].tkey == "Spam"
|
2014-07-23 12:09:06 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_interlace(self):
|
2013-08-20 16:17:17 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/pil123p.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "P", (162, 150))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("interlace")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im.load()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/pil123rgba.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (162, 150))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info.get("interlace")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im.load()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_load_transparent_p(self):
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/pil123p.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "P", (162, 150))
|
2019-11-25 23:03:23 +03:00
|
|
|
im = im.convert("RGBA")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (162, 150))
|
2013-03-11 23:33:04 +04:00
|
|
|
|
2016-10-02 13:31:53 +03:00
|
|
|
# image has 124 unique alpha values
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.getchannel("A").getcolors()) == 124
|
2013-03-11 23:33:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_load_transparent_rgb(self):
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/rgb_trns.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == (0, 255, 52)
|
2013-03-11 23:33:04 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGB", (64, 64))
|
2019-11-25 23:03:23 +03:00
|
|
|
im = im.convert("RGBA")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (64, 64))
|
2013-11-23 04:04:51 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# image has 876 transparent pixels
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getchannel("A").getcolors()[0][0] == 876
|
2013-11-23 04:04:51 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_p_transparent_palette(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
in_file = "Tests/images/pil123p.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(in_file) as im:
|
|
|
|
# 'transparency' contains a byte string with the opacity for
|
|
|
|
# each palette entry
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.info["transparency"]) == 256
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file)
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2016-04-19 10:31:15 +03:00
|
|
|
# check if saved image contains same transparency
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.info["transparency"]) == 256
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "P", (162, 150))
|
2019-11-25 23:03:23 +03:00
|
|
|
im = im.convert("RGBA")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (162, 150))
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2016-05-03 21:46:22 +03:00
|
|
|
# image has 124 unique alpha values
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.getchannel("A").getcolors()) == 124
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_p_single_transparency(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
in_file = "Tests/images/p_trns_single.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(in_file) as im:
|
|
|
|
# pixel value 164 is full transparent
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == 164
|
|
|
|
assert im.getpixel((31, 31)) == 164
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file)
|
2016-04-19 10:31:15 +03:00
|
|
|
|
|
|
|
# check if saved image contains same transparency
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == 164
|
|
|
|
assert im.getpixel((31, 31)) == 164
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "P", (64, 64))
|
2019-11-25 23:03:23 +03:00
|
|
|
im = im.convert("RGBA")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (64, 64))
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getpixel((31, 31)) == (0, 255, 52, 0)
|
2016-04-19 10:31:15 +03:00
|
|
|
|
|
|
|
# image has 876 transparent pixels
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getchannel("A").getcolors()[0][0] == 876
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_p_transparent_black(self, tmp_path):
|
2016-04-19 10:31:15 +03:00
|
|
|
# check if solid black image with full transparency
|
|
|
|
# is supported (check for #1838)
|
|
|
|
im = Image.new("RGBA", (10, 10), (0, 0, 0, 0))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getcolors() == [(100, (0, 0, 0, 0))]
|
2016-04-19 10:31:15 +03:00
|
|
|
|
|
|
|
im = im.convert("P")
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2015-04-24 11:24:52 +03:00
|
|
|
im.save(test_file)
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2016-04-19 10:31:15 +03:00
|
|
|
# check if saved image contains same transparency
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(im.info["transparency"]) == 256
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "P", (10, 10))
|
2019-11-25 23:03:23 +03:00
|
|
|
im = im.convert("RGBA")
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, "RGBA", (10, 10))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.getcolors() == [(100, (0, 0, 0, 0))]
|
2016-04-19 10:31:15 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_greyscale_transparency(self, tmp_path):
|
2019-06-13 18:54:11 +03:00
|
|
|
for mode, num_transparent in {"1": 1994, "L": 559, "I": 559}.items():
|
|
|
|
in_file = "Tests/images/" + mode.lower() + "_trns.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(in_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.mode == mode
|
|
|
|
assert im.info["transparency"] == 255
|
2019-03-26 23:41:33 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im_rgba = im.convert("RGBA")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im_rgba.getchannel("A").getcolors()[0][0] == num_transparent
|
2019-03-26 23:41:33 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-03-26 23:41:33 +03:00
|
|
|
im.save(test_file)
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as test_im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im.mode == mode
|
|
|
|
assert test_im.info["transparency"] == 255
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im, test_im)
|
2018-06-14 12:20:04 +03:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
test_im_rgba = test_im.convert("RGBA")
|
2020-02-22 16:06:21 +03:00
|
|
|
assert test_im_rgba.getchannel("A").getcolors()[0][0] == num_transparent
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_save_rgb_single_transparency(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
in_file = "Tests/images/caption_6_33_22.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(in_file) as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file)
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_load_verify(self):
|
|
|
|
# Check open/load/verify exception (@PIL150)
|
2013-03-26 14:24:07 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
# Assert that there is no unclosed file warning
|
2020-02-03 12:11:32 +03:00
|
|
|
pytest.warns(None, im.verify)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
im.load()
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
im.verify()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2016-04-04 13:08:22 +03:00
|
|
|
def test_verify_struct_error(self):
|
|
|
|
# Check open/load/verify exception (#1755)
|
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
# offsets to test, -10: breaks in i32() in read. (OSError)
|
2016-04-04 13:08:22 +03:00
|
|
|
# -13: breaks in crc, txt chunk.
|
|
|
|
# -14: malformed chunk
|
|
|
|
|
|
|
|
for offset in (-10, -13, -14):
|
2019-06-13 18:54:11 +03:00
|
|
|
with open(TEST_PNG_FILE, "rb") as f:
|
2016-04-04 13:08:22 +03:00
|
|
|
test_file = f.read()[:offset]
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(BytesIO(test_file)) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.fp is not None
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises((OSError, SyntaxError)):
|
2020-02-22 16:06:21 +03:00
|
|
|
im.verify()
|
2016-04-04 13:08:22 +03:00
|
|
|
|
2016-06-29 22:24:37 +03:00
|
|
|
def test_verify_ignores_crc_error(self):
|
|
|
|
# check ignores crc errors in ancillary chunks
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
chunk_data = chunk(b"tEXt", b"spam")
|
|
|
|
broken_crc_chunk_data = chunk_data[:-1] + b"q" # break CRC
|
2016-06-29 22:24:37 +03:00
|
|
|
|
|
|
|
image_data = HEAD + broken_crc_chunk_data + TAIL
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
PngImagePlugin.PngImageFile(BytesIO(image_data))
|
2016-06-29 22:24:37 +03:00
|
|
|
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
|
|
try:
|
|
|
|
im = load(image_data)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im is not None
|
2016-06-29 22:24:37 +03:00
|
|
|
finally:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
|
|
|
|
|
|
|
def test_verify_not_ignores_crc_error_in_required_chunk(self):
|
|
|
|
# check does not ignore crc errors in required chunks
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
image_data = MAGIC + IHDR[:-1] + b"q" + TAIL
|
2016-06-29 22:24:37 +03:00
|
|
|
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
|
|
|
try:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
PngImagePlugin.PngImageFile(BytesIO(image_data))
|
2016-06-29 22:24:37 +03:00
|
|
|
finally:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip_dpi(self):
|
|
|
|
# Check dpi roundtripping
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
im = roundtrip(im, dpi=(100, 100))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == (100, 100)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-03-30 07:03:57 +03:00
|
|
|
def test_load_dpi_rounding(self):
|
|
|
|
# Round up
|
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(TEST_PNG_FILE) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == (96, 96)
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
# Round down
|
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/icc_profile_none.png") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == (72, 72)
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
def test_save_dpi_rounding(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
im = roundtrip(im, dpi=(72.2, 72.2))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == (72, 72)
|
2019-03-30 07:03:57 +03:00
|
|
|
|
|
|
|
im = roundtrip(im, dpi=(72.8, 72.8))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["dpi"] == (73, 73)
|
2019-03-30 07:03:57 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip_text(self):
|
|
|
|
# Check text roundtripping
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
info = PngImagePlugin.PngInfo()
|
|
|
|
info.add_text("TXT", "VALUE")
|
|
|
|
info.add_text("ZIP", "VALUE", zip=True)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im = roundtrip(im, pnginfo=info)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"TXT": "VALUE", "ZIP": "VALUE"}
|
|
|
|
assert im.text == {"TXT": "VALUE", "ZIP": "VALUE"}
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-07-23 18:27:51 +04:00
|
|
|
def test_roundtrip_itxt(self):
|
|
|
|
# Check iTXt roundtripping
|
|
|
|
|
|
|
|
im = Image.new("RGB", (32, 32))
|
|
|
|
info = PngImagePlugin.PngInfo()
|
|
|
|
info.add_itxt("spam", "Eggs", "en", "Spam")
|
2019-06-13 18:54:11 +03:00
|
|
|
info.add_text("eggs", PngImagePlugin.iTXt("Spam", "en", "Eggs"), zip=True)
|
2014-07-23 18:27:51 +04:00
|
|
|
|
|
|
|
im = roundtrip(im, pnginfo=info)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"spam": "Eggs", "eggs": "Spam"}
|
|
|
|
assert im.text == {"spam": "Eggs", "eggs": "Spam"}
|
|
|
|
assert im.text["spam"].lang == "en"
|
|
|
|
assert im.text["spam"].tkey == "Spam"
|
|
|
|
assert im.text["eggs"].lang == "en"
|
|
|
|
assert im.text["eggs"].tkey == "Eggs"
|
2014-07-23 18:27:51 +04:00
|
|
|
|
2014-07-23 19:17:11 +04:00
|
|
|
def test_nonunicode_text(self):
|
|
|
|
# Check so that non-Unicode text is saved as a tEXt rather than iTXt
|
|
|
|
|
|
|
|
im = Image.new("RGB", (32, 32))
|
|
|
|
info = PngImagePlugin.PngInfo()
|
|
|
|
info.add_text("Text", "Ascii")
|
|
|
|
im = roundtrip(im, pnginfo=info)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(im.info["Text"], str)
|
2014-07-23 19:17:11 +04:00
|
|
|
|
|
|
|
def test_unicode_text(self):
|
2019-10-07 15:34:12 +03:00
|
|
|
# Check preservation of non-ASCII characters
|
2014-07-23 19:17:11 +04:00
|
|
|
|
|
|
|
def rt_text(value):
|
|
|
|
im = Image.new("RGB", (32, 32))
|
|
|
|
info = PngImagePlugin.PngInfo()
|
|
|
|
info.add_text("Text", value)
|
|
|
|
im = roundtrip(im, pnginfo=info)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info == {"Text": value}
|
2014-07-23 19:17:11 +04:00
|
|
|
|
2019-09-26 15:12:28 +03:00
|
|
|
rt_text(" Aa" + chr(0xA0) + chr(0xC4) + chr(0xFF)) # Latin1
|
|
|
|
rt_text(chr(0x400) + chr(0x472) + chr(0x4FF)) # Cyrillic
|
|
|
|
# CJK:
|
|
|
|
rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))
|
|
|
|
rt_text("A" + chr(0xC4) + chr(0x472) + chr(0x3042)) # Combined
|
2014-07-23 19:17:11 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_scary(self):
|
|
|
|
# Check reading of evil PNG file. For information, see:
|
|
|
|
# http://scary.beasts.org/security/CESA-2004-001.txt
|
|
|
|
# The first byte is removed from pngtest_bad.png
|
|
|
|
# to avoid classification as malware.
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
with open("Tests/images/pngtest_bad.png.bin", "rb") as fd:
|
|
|
|
data = b"\x89" + fd.read()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
pngfile = BytesIO(data)
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
Image.open(pngfile)
|
2012-10-15 09:55:39 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_trns_rgb(self):
|
|
|
|
# Check writing and reading of tRNS chunks for RGB images.
|
|
|
|
# Independent file sample provided by Sebastian Spaeth.
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/caption_6_33_22.png"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(test_file) as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == (248, 248, 248)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
# check saving transparency by default
|
|
|
|
im = roundtrip(im)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == (248, 248, 248)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = roundtrip(im, transparency=(0, 1, 2))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == (0, 1, 2)
|
2013-11-27 02:59:03 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_trns_p(self, tmp_path):
|
2014-06-10 13:10:47 +04:00
|
|
|
# Check writing a transparency of 0, issue #528
|
2019-06-13 18:54:11 +03:00
|
|
|
im = hopper("P")
|
|
|
|
im.info["transparency"] = 0
|
2014-01-19 19:40:39 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
f = str(tmp_path / "temp.png")
|
2014-06-10 13:10:47 +04:00
|
|
|
im.save(f)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(f) as im2:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "transparency" in im2.info
|
2014-03-01 04:29:34 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im2.convert("RGBA"), im.convert("RGBA"))
|
2014-03-01 04:29:34 +04:00
|
|
|
|
2015-05-27 18:45:27 +03:00
|
|
|
def test_trns_null(self):
|
|
|
|
# Check reading images with null tRNS value, issue #1239
|
|
|
|
test_file = "Tests/images/tRNS_null_1x1.png"
|
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(test_file) as im:
|
2015-05-27 18:45:27 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["transparency"] == 0
|
2015-05-27 18:45:27 +03:00
|
|
|
|
2016-05-12 20:28:58 +03:00
|
|
|
def test_save_icc_profile(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/icc_profile_none.png") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["icc_profile"] is None
|
2014-06-03 14:02:44 +04:00
|
|
|
|
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/icc_profile.png") as with_icc:
|
|
|
|
expected_icc = with_icc.info["icc_profile"]
|
2016-05-12 20:28:58 +03:00
|
|
|
|
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
|
|
|
im = roundtrip(im, icc_profile=expected_icc)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["icc_profile"] == expected_icc
|
2016-05-12 20:28:58 +03:00
|
|
|
|
|
|
|
def test_discard_icc_profile(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/icc_profile.png") as im:
|
|
|
|
im = roundtrip(im, icc_profile=None)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "icc_profile" not in im.info
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip_icc_profile(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/icc_profile.png") as im:
|
|
|
|
expected_icc = im.info["icc_profile"]
|
2014-01-19 19:40:39 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im = roundtrip(im)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["icc_profile"] == expected_icc
|
2014-01-19 22:09:40 +04:00
|
|
|
|
2016-05-12 20:28:58 +03:00
|
|
|
def test_roundtrip_no_icc_profile(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/icc_profile_none.png") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["icc_profile"] is None
|
2016-05-12 20:28:58 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
im = roundtrip(im)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "icc_profile" not in im.info
|
2016-05-12 20:28:58 +03:00
|
|
|
|
2015-01-28 20:35:31 +03:00
|
|
|
def test_repr_png(self):
|
|
|
|
im = hopper()
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(BytesIO(im._repr_png_())) as repr_png:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert repr_png.format == "PNG"
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(im, repr_png)
|
2015-01-28 20:35:31 +03:00
|
|
|
|
2020-12-27 07:36:16 +03:00
|
|
|
def test_repr_png_error(self):
|
|
|
|
im = hopper("F")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im._repr_png_()
|
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_chunk_order(self, tmp_path):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/icc_profile.png") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.convert("P").save(test_file, dpi=(100, 100))
|
2017-01-15 09:36:59 +03:00
|
|
|
|
2017-12-20 13:27:13 +03:00
|
|
|
chunks = self.get_chunks(test_file)
|
2017-01-21 06:47:59 +03:00
|
|
|
|
|
|
|
# https://www.w3.org/TR/PNG/#5ChunkOrdering
|
|
|
|
# IHDR - shall be first
|
2020-02-22 16:06:21 +03:00
|
|
|
assert chunks.index(b"IHDR") == 0
|
2017-01-21 06:47:59 +03:00
|
|
|
# PLTE - before first IDAT
|
2020-02-22 16:06:21 +03:00
|
|
|
assert chunks.index(b"PLTE") < chunks.index(b"IDAT")
|
2017-01-21 06:47:59 +03:00
|
|
|
# iCCP - before PLTE and IDAT
|
2020-02-22 16:06:21 +03:00
|
|
|
assert chunks.index(b"iCCP") < chunks.index(b"PLTE")
|
|
|
|
assert chunks.index(b"iCCP") < chunks.index(b"IDAT")
|
2017-01-21 06:47:59 +03:00
|
|
|
# tRNS - after PLTE, before IDAT
|
2020-02-22 16:06:21 +03:00
|
|
|
assert chunks.index(b"tRNS") > chunks.index(b"PLTE")
|
|
|
|
assert chunks.index(b"tRNS") < chunks.index(b"IDAT")
|
2017-01-21 06:47:59 +03:00
|
|
|
# pHYs - before IDAT
|
2020-02-22 16:06:21 +03:00
|
|
|
assert chunks.index(b"pHYs") < chunks.index(b"IDAT")
|
2017-01-15 09:36:59 +03:00
|
|
|
|
2017-09-01 13:36:51 +03:00
|
|
|
def test_getchunks(self):
|
|
|
|
im = hopper()
|
|
|
|
|
|
|
|
chunks = PngImagePlugin.getchunks(im)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert len(chunks) == 3
|
2017-09-01 13:36:51 +03:00
|
|
|
|
2020-03-11 13:40:17 +03:00
|
|
|
def test_read_private_chunks(self):
|
2021-02-02 15:39:53 +03:00
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
|
|
|
assert im.private_chunks == [(b"orNT", b"\x01")]
|
2020-03-11 13:40:17 +03:00
|
|
|
|
|
|
|
def test_roundtrip_private_chunk(self):
|
|
|
|
# Check private chunk roundtripping
|
|
|
|
|
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
info = PngImagePlugin.PngInfo()
|
|
|
|
info.add(b"prIV", b"VALUE")
|
|
|
|
info.add(b"atEC", b"VALUE2")
|
|
|
|
info.add(b"prIV", b"VALUE3", True)
|
|
|
|
|
|
|
|
im = roundtrip(im, pnginfo=info)
|
|
|
|
assert im.private_chunks == [(b"prIV", b"VALUE"), (b"atEC", b"VALUE2")]
|
|
|
|
im.load()
|
|
|
|
assert im.private_chunks == [
|
|
|
|
(b"prIV", b"VALUE"),
|
|
|
|
(b"atEC", b"VALUE2"),
|
|
|
|
(b"prIV", b"VALUE3", True),
|
|
|
|
]
|
|
|
|
|
2018-12-24 15:58:19 +03:00
|
|
|
def test_textual_chunks_after_idat(self):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.png") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert "comment" in im.text.keys()
|
2019-11-25 23:03:23 +03:00
|
|
|
for k, v in {
|
|
|
|
"date:create": "2014-09-04T09:37:08+03:00",
|
|
|
|
"date:modify": "2014-09-04T09:37:08+03:00",
|
|
|
|
}.items():
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.text[k] == v
|
2018-12-24 15:58:19 +03:00
|
|
|
|
|
|
|
# Raises a SyntaxError in load_end
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/broken_data_stream.png") as im:
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(im.text, dict)
|
2018-12-24 15:58:19 +03:00
|
|
|
|
|
|
|
# Raises a UnicodeDecodeError in load_end
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/truncated_image.png") as im:
|
|
|
|
# The file is truncated
|
2020-04-07 09:58:21 +03:00
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
im.text()
|
2019-11-25 23:03:23 +03:00
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(im.text, dict)
|
2019-11-25 23:03:23 +03:00
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
2018-12-24 15:58:19 +03:00
|
|
|
|
2019-01-03 11:13:19 +03:00
|
|
|
# Raises an EOFError in load_end
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper_idat_after_image_end.png") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.text == {"TXT": "VALUE", "ZIP": "VALUE"}
|
2019-01-03 11:13:19 +03:00
|
|
|
|
2020-04-16 14:14:19 +03:00
|
|
|
def test_exif(self):
|
|
|
|
# With an EXIF chunk
|
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
2020-03-26 13:21:40 +03:00
|
|
|
exif = im._getexif()
|
|
|
|
assert exif[274] == 1
|
2019-03-11 13:18:36 +03:00
|
|
|
|
2020-04-16 14:14:19 +03:00
|
|
|
# With an ImageMagick zTXt chunk
|
|
|
|
with Image.open("Tests/images/exif_imagemagick.png") as im:
|
|
|
|
exif = im._getexif()
|
|
|
|
assert exif[274] == 1
|
|
|
|
|
|
|
|
# Assert that info still can be extracted
|
|
|
|
# when the image is no longer a PngImageFile instance
|
|
|
|
exif = im.copy().getexif()
|
|
|
|
assert exif[274] == 1
|
|
|
|
|
2020-07-31 10:42:48 +03:00
|
|
|
# With a tEXt chunk
|
|
|
|
with Image.open("Tests/images/exif_text.png") as im:
|
|
|
|
exif = im._getexif()
|
|
|
|
assert exif[274] == 1
|
|
|
|
|
2020-04-16 14:14:19 +03:00
|
|
|
# With XMP tags
|
2020-04-16 14:05:34 +03:00
|
|
|
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
|
|
|
|
exif = im.getexif()
|
|
|
|
assert exif[274] == 3
|
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_exif_save(self, tmp_path):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/exif.png") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file)
|
2019-03-11 13:18:36 +03:00
|
|
|
|
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(test_file) as reloaded:
|
|
|
|
exif = reloaded._getexif()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert exif[274] == 1
|
2019-03-11 13:18:36 +03:00
|
|
|
|
2021-01-07 16:50:25 +03:00
|
|
|
@pytest.mark.valgrind_known_error(reason="Known Failing")
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_exif_from_jpg(self, tmp_path):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/pil_sample_rgb.jpg") as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file)
|
2019-02-23 05:30:38 +03:00
|
|
|
|
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(test_file) as reloaded:
|
|
|
|
exif = reloaded._getexif()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert exif[305] == "Adobe Photoshop CS Macintosh"
|
2019-02-23 05:30:38 +03:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
def test_exif_argument(self, tmp_path):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
2020-03-02 17:02:19 +03:00
|
|
|
test_file = str(tmp_path / "temp.png")
|
2019-11-25 23:03:23 +03:00
|
|
|
im.save(test_file, exif=b"exifstring")
|
2019-02-23 05:30:38 +03:00
|
|
|
|
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(test_file) as reloaded:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert reloaded.info["exif"] == b"Exif\x00\x00exifstring"
|
2019-02-23 05:30:38 +03:00
|
|
|
|
2020-04-02 11:17:54 +03:00
|
|
|
def test_tell(self):
|
2020-04-02 08:49:26 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
assert im.tell() == 0
|
|
|
|
|
2020-04-02 11:17:54 +03:00
|
|
|
def test_seek(self):
|
2020-04-01 22:32:14 +03:00
|
|
|
with Image.open(TEST_PNG_FILE) as im:
|
|
|
|
im.seek(0)
|
|
|
|
|
2020-04-05 08:29:13 +03:00
|
|
|
with pytest.raises(EOFError):
|
|
|
|
im.seek(1)
|
|
|
|
|
2014-01-22 08:50:54 +04:00
|
|
|
|
2020-03-02 17:02:19 +03:00
|
|
|
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("zlib")
|
2017-09-04 12:44:57 +03:00
|
|
|
class TestTruncatedPngPLeaks(PillowLeakTestCase):
|
2019-06-13 18:54:11 +03:00
|
|
|
mem_limit = 2 * 1024 # max increase in K
|
2018-03-04 06:24:36 +03:00
|
|
|
iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs
|
2017-06-21 13:31:32 +03:00
|
|
|
|
|
|
|
def test_leak_load(self):
|
2019-06-13 18:54:11 +03:00
|
|
|
with open("Tests/images/hopper.png", "rb") as f:
|
2017-06-21 13:31:32 +03:00
|
|
|
DATA = BytesIO(f.read(16 * 1024))
|
|
|
|
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
2017-06-27 23:20:46 +03:00
|
|
|
with Image.open(DATA) as im:
|
|
|
|
im.load()
|
2017-09-04 12:44:57 +03:00
|
|
|
|
|
|
|
def core():
|
|
|
|
with Image.open(DATA) as im:
|
|
|
|
im.load()
|
|
|
|
|
2017-06-21 13:31:32 +03:00
|
|
|
try:
|
2017-09-04 12:44:57 +03:00
|
|
|
self._test_leak(core)
|
2017-06-21 13:31:32 +03:00
|
|
|
finally:
|
|
|
|
ImageFile.LOAD_TRUNCATED_IMAGES = False
|