~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to Tests/test_file_png.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from tester import *
 
2
 
 
3
from PIL import Image
 
4
from PIL import PngImagePlugin
 
5
 
 
6
codecs = dir(Image.core)
 
7
 
 
8
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
 
9
    skip("zip/deflate support not available")
 
10
 
 
11
# sample png stream
 
12
 
 
13
file = "Images/lena.png"
 
14
data = open(file, "rb").read()
 
15
 
 
16
# stuff to create inline PNG images
 
17
 
 
18
MAGIC = PngImagePlugin._MAGIC
 
19
 
 
20
def chunk(cid, *data):
 
21
    file = BytesIO()
 
22
    PngImagePlugin.putchunk(*(file, cid) + data)
 
23
    return file.getvalue()
 
24
 
 
25
o32 = PngImagePlugin.o32
 
26
 
 
27
IHDR = chunk(b"IHDR", o32(1), o32(1), b'\x08\x02', b'\0\0\0')
 
28
IDAT = chunk(b"IDAT")
 
29
IEND = chunk(b"IEND")
 
30
 
 
31
HEAD = MAGIC + IHDR
 
32
TAIL = IDAT + IEND
 
33
 
 
34
def load(data):
 
35
    return Image.open(BytesIO(data))
 
36
 
 
37
def roundtrip(im, **options):
 
38
    out = BytesIO()
 
39
    im.save(out, "PNG", **options)
 
40
    out.seek(0)
 
41
    return Image.open(out)
 
42
 
 
43
# --------------------------------------------------------------------
 
44
 
 
45
def test_sanity():
 
46
 
 
47
    # internal version number
 
48
    assert_match(Image.core.zlib_version, "\d+\.\d+\.\d+(\.\d+)?$")
 
49
 
 
50
    file = tempfile("temp.png")
 
51
 
 
52
    lena("RGB").save(file)
 
53
 
 
54
    im = Image.open(file)
 
55
    im.load()
 
56
    assert_equal(im.mode, "RGB")
 
57
    assert_equal(im.size, (128, 128))
 
58
    assert_equal(im.format, "PNG")
 
59
 
 
60
    lena("1").save(file)
 
61
    im = Image.open(file)
 
62
 
 
63
    lena("L").save(file)
 
64
    im = Image.open(file)
 
65
 
 
66
    lena("P").save(file)
 
67
    im = Image.open(file)
 
68
 
 
69
    lena("RGB").save(file)
 
70
    im = Image.open(file)
 
71
 
 
72
    lena("I").save(file)
 
73
    im = Image.open(file)
 
74
 
 
75
# --------------------------------------------------------------------
 
76
 
 
77
def test_broken():
 
78
    # Check reading of totally broken files.  In this case, the test
 
79
    # file was checked into Subversion as a text file.
 
80
 
 
81
    file = "Tests/images/broken.png"
 
82
    assert_exception(IOError, lambda: Image.open(file))
 
83
 
 
84
def test_bad_text():
 
85
    # Make sure PIL can read malformed tEXt chunks (@PIL152)
 
86
 
 
87
    im = load(HEAD + chunk(b'tEXt') + TAIL)
 
88
    assert_equal(im.info, {})
 
89
 
 
90
    im = load(HEAD + chunk(b'tEXt', b'spam') + TAIL)
 
91
    assert_equal(im.info, {'spam': ''})
 
92
 
 
93
    im = load(HEAD + chunk(b'tEXt', b'spam\0') + TAIL)
 
94
    assert_equal(im.info, {'spam': ''})
 
95
 
 
96
    im = load(HEAD + chunk(b'tEXt', b'spam\0egg') + TAIL)
 
97
    assert_equal(im.info, {'spam': 'egg'})
 
98
 
 
99
    im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
 
100
    assert_equal(im.info,  {'spam': 'egg\x00'})
 
101
 
 
102
def test_interlace():
 
103
 
 
104
    file = "Tests/images/pil123p.png"
 
105
    im = Image.open(file)
 
106
 
 
107
    assert_image(im, "P", (162, 150))
 
108
    assert_true(im.info.get("interlace"))
 
109
 
 
110
    assert_no_exception(lambda: im.load())
 
111
 
 
112
    file = "Tests/images/pil123rgba.png"
 
113
    im = Image.open(file)
 
114
 
 
115
    assert_image(im, "RGBA", (162, 150))
 
116
    assert_true(im.info.get("interlace"))
 
117
 
 
118
    assert_no_exception(lambda: im.load())
 
119
 
 
120
def test_load_verify():
 
121
    # Check open/load/verify exception (@PIL150)
 
122
 
 
123
    im = Image.open("Images/lena.png")
 
124
    assert_no_exception(lambda: im.verify())
 
125
 
 
126
    im = Image.open("Images/lena.png")
 
127
    im.load()
 
128
    assert_exception(RuntimeError, lambda: im.verify())
 
129
 
 
130
def test_roundtrip_dpi():
 
131
    # Check dpi roundtripping
 
132
 
 
133
    im = Image.open(file)
 
134
 
 
135
    im = roundtrip(im, dpi=(100, 100))
 
136
    assert_equal(im.info["dpi"], (100, 100))
 
137
 
 
138
def test_roundtrip_text():
 
139
    # Check text roundtripping
 
140
 
 
141
    im = Image.open(file)
 
142
 
 
143
    info = PngImagePlugin.PngInfo()
 
144
    info.add_text("TXT", "VALUE")
 
145
    info.add_text("ZIP", "VALUE", 1)
 
146
 
 
147
    im = roundtrip(im, pnginfo=info)
 
148
    assert_equal(im.info, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
 
149
    assert_equal(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
 
150
 
 
151
def test_scary():
 
152
    # Check reading of evil PNG file.  For information, see:
 
153
    # http://scary.beasts.org/security/CESA-2004-001.txt
 
154
 
 
155
    import base64
 
156
    file = "Tests/images/pngtest_bad.png.base64"
 
157
    data = None
 
158
 
 
159
    if py3:
 
160
        data = base64.decodebytes(open(file, 'rb').read())
 
161
    else:
 
162
        data = base64.decodestring(open(file, 'rb').read())
 
163
 
 
164
    file = BytesIO(data)
 
165
    assert_exception(IOError, lambda: Image.open(file))
 
166
 
 
167
def test_trns_rgb():
 
168
    # Check writing and reading of tRNS chunks for RGB images.
 
169
    # Independent file sample provided by Sebastian Spaeth.
 
170
 
 
171
    file = "Tests/images/caption_6_33_22.png"
 
172
    im = Image.open(file)
 
173
    assert_equal(im.info["transparency"], (248, 248, 248))
 
174
 
 
175
    im = roundtrip(im, transparency=(0, 1, 2))
 
176
    assert_equal(im.info["transparency"], (0, 1, 2))