4
from PIL import PngImagePlugin
6
codecs = dir(Image.core)
8
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
9
skip("zip/deflate support not available")
13
file = "Images/lena.png"
14
data = open(file, "rb").read()
16
# stuff to create inline PNG images
18
MAGIC = PngImagePlugin._MAGIC
20
def chunk(cid, *data):
22
PngImagePlugin.putchunk(*(file, cid) + data)
23
return file.getvalue()
25
o32 = PngImagePlugin.o32
27
IHDR = chunk(b"IHDR", o32(1), o32(1), b'\x08\x02', b'\0\0\0')
35
return Image.open(BytesIO(data))
37
def roundtrip(im, **options):
39
im.save(out, "PNG", **options)
41
return Image.open(out)
43
# --------------------------------------------------------------------
47
# internal version number
48
assert_match(Image.core.zlib_version, "\d+\.\d+\.\d+(\.\d+)?$")
50
file = tempfile("temp.png")
52
lena("RGB").save(file)
56
assert_equal(im.mode, "RGB")
57
assert_equal(im.size, (128, 128))
58
assert_equal(im.format, "PNG")
69
lena("RGB").save(file)
75
# --------------------------------------------------------------------
78
# Check reading of totally broken files. In this case, the test
79
# file was checked into Subversion as a text file.
81
file = "Tests/images/broken.png"
82
assert_exception(IOError, lambda: Image.open(file))
85
# Make sure PIL can read malformed tEXt chunks (@PIL152)
87
im = load(HEAD + chunk(b'tEXt') + TAIL)
88
assert_equal(im.info, {})
90
im = load(HEAD + chunk(b'tEXt', b'spam') + TAIL)
91
assert_equal(im.info, {'spam': ''})
93
im = load(HEAD + chunk(b'tEXt', b'spam\0') + TAIL)
94
assert_equal(im.info, {'spam': ''})
96
im = load(HEAD + chunk(b'tEXt', b'spam\0egg') + TAIL)
97
assert_equal(im.info, {'spam': 'egg'})
99
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
100
assert_equal(im.info, {'spam': 'egg\x00'})
102
def test_interlace():
104
file = "Tests/images/pil123p.png"
105
im = Image.open(file)
107
assert_image(im, "P", (162, 150))
108
assert_true(im.info.get("interlace"))
110
assert_no_exception(lambda: im.load())
112
file = "Tests/images/pil123rgba.png"
113
im = Image.open(file)
115
assert_image(im, "RGBA", (162, 150))
116
assert_true(im.info.get("interlace"))
118
assert_no_exception(lambda: im.load())
120
def test_load_verify():
121
# Check open/load/verify exception (@PIL150)
123
im = Image.open("Images/lena.png")
124
assert_no_exception(lambda: im.verify())
126
im = Image.open("Images/lena.png")
128
assert_exception(RuntimeError, lambda: im.verify())
130
def test_roundtrip_dpi():
131
# Check dpi roundtripping
133
im = Image.open(file)
135
im = roundtrip(im, dpi=(100, 100))
136
assert_equal(im.info["dpi"], (100, 100))
138
def test_roundtrip_text():
139
# Check text roundtripping
141
im = Image.open(file)
143
info = PngImagePlugin.PngInfo()
144
info.add_text("TXT", "VALUE")
145
info.add_text("ZIP", "VALUE", 1)
147
im = roundtrip(im, pnginfo=info)
148
assert_equal(im.info, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
149
assert_equal(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
152
# Check reading of evil PNG file. For information, see:
153
# http://scary.beasts.org/security/CESA-2004-001.txt
156
file = "Tests/images/pngtest_bad.png.base64"
160
data = base64.decodebytes(open(file, 'rb').read())
162
data = base64.decodestring(open(file, 'rb').read())
165
assert_exception(IOError, lambda: Image.open(file))
168
# Check writing and reading of tRNS chunks for RGB images.
169
# Independent file sample provided by Sebastian Spaeth.
171
file = "Tests/images/caption_6_33_22.png"
172
im = Image.open(file)
173
assert_equal(im.info["transparency"], (248, 248, 248))
175
im = roundtrip(im, transparency=(0, 1, 2))
176
assert_equal(im.info["transparency"], (0, 1, 2))