2012-10-16 00:26:38 +04:00
|
|
|
from tester import *
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import PngImagePlugin
|
|
|
|
|
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
|
|
|
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
|
|
|
|
skip("zip/deflate support not available")
|
|
|
|
|
|
|
|
# sample png stream
|
|
|
|
|
|
|
|
file = "Images/lena.png"
|
|
|
|
data = open(file, "rb").read()
|
|
|
|
|
|
|
|
# stuff to create inline PNG images
|
|
|
|
|
|
|
|
MAGIC = PngImagePlugin._MAGIC
|
|
|
|
|
|
|
|
def chunk(cid, *data):
|
|
|
|
file = BytesIO()
|
|
|
|
PngImagePlugin.putchunk(*(file, cid) + data)
|
|
|
|
return file.getvalue()
|
|
|
|
|
|
|
|
o32 = PngImagePlugin.o32
|
|
|
|
|
2012-10-15 09:55:39 +04: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
|
|
|
|
|
|
|
|
def load(data):
|
|
|
|
return Image.open(BytesIO(data))
|
|
|
|
|
|
|
|
def roundtrip(im, **options):
|
|
|
|
out = BytesIO()
|
|
|
|
im.save(out, "PNG", **options)
|
|
|
|
out.seek(0)
|
|
|
|
return Image.open(out)
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
def test_sanity():
|
|
|
|
|
|
|
|
# internal version number
|
|
|
|
assert_match(Image.core.zlib_version, "\d+\.\d+\.\d+(\.\d+)?$")
|
|
|
|
|
|
|
|
file = tempfile("temp.png")
|
|
|
|
|
|
|
|
lena("RGB").save(file)
|
|
|
|
|
|
|
|
im = Image.open(file)
|
|
|
|
im.load()
|
|
|
|
assert_equal(im.mode, "RGB")
|
|
|
|
assert_equal(im.size, (128, 128))
|
|
|
|
assert_equal(im.format, "PNG")
|
|
|
|
|
|
|
|
lena("1").save(file)
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
lena("L").save(file)
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
lena("P").save(file)
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
lena("RGB").save(file)
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
lena("I").save(file)
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
def test_broken():
|
|
|
|
# Check reading of totally broken files. In this case, the test
|
|
|
|
# file was checked into Subversion as a text file.
|
|
|
|
|
|
|
|
file = "Tests/images/broken.png"
|
|
|
|
assert_exception(IOError, lambda: Image.open(file))
|
|
|
|
|
|
|
|
def test_bad_text():
|
|
|
|
# Make sure PIL can read malformed tEXt chunks (@PIL152)
|
|
|
|
|
|
|
|
im = load(HEAD + chunk(b'tEXt') + TAIL)
|
|
|
|
assert_equal(im.info, {})
|
|
|
|
|
|
|
|
im = load(HEAD + chunk(b'tEXt', b'spam') + TAIL)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
assert_equal(im.info, {'spam': ''})
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
im = load(HEAD + chunk(b'tEXt', b'spam\0') + TAIL)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
assert_equal(im.info, {'spam': ''})
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
im = load(HEAD + chunk(b'tEXt', b'spam\0egg') + TAIL)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
assert_equal(im.info, {'spam': 'egg'})
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
assert_equal(im.info, {'spam': 'egg\x00'})
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
def test_interlace():
|
|
|
|
|
|
|
|
file = "Tests/images/pil123p.png"
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
assert_image(im, "P", (162, 150))
|
|
|
|
assert_true(im.info.get("interlace"))
|
|
|
|
|
|
|
|
assert_no_exception(lambda: im.load())
|
|
|
|
|
|
|
|
file = "Tests/images/pil123rgba.png"
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
assert_image(im, "RGBA", (162, 150))
|
|
|
|
assert_true(im.info.get("interlace"))
|
|
|
|
|
|
|
|
assert_no_exception(lambda: im.load())
|
|
|
|
|
2013-03-11 23:33:04 +04:00
|
|
|
def test_load_transparent_p():
|
|
|
|
file = "Tests/images/pil123p.png"
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
assert_image(im, "P", (162, 150))
|
|
|
|
im = im.convert("RGBA")
|
|
|
|
assert_image(im, "RGBA", (162, 150))
|
|
|
|
|
|
|
|
# image has 124 uniqe qlpha values
|
|
|
|
assert_equal(len(im.split()[3].getcolors()), 124)
|
|
|
|
|
2013-03-26 14:24:07 +04:00
|
|
|
def test_save_p_transparent_palette():
|
|
|
|
in_file = "Tests/images/pil123p.png"
|
|
|
|
im = Image.open(in_file)
|
|
|
|
|
|
|
|
file = tempfile("temp.png")
|
|
|
|
assert_no_exception(lambda: im.save(file))
|
|
|
|
|
|
|
|
def test_save_p_single_transparency():
|
|
|
|
in_file = "Tests/images/p_trns_single.png"
|
|
|
|
im = Image.open(in_file)
|
|
|
|
|
|
|
|
file = tempfile("temp.png")
|
|
|
|
assert_no_exception(lambda: im.save(file))
|
|
|
|
|
|
|
|
def test_save_l_transparency():
|
|
|
|
in_file = "Tests/images/l_trns.png"
|
|
|
|
im = Image.open(in_file)
|
|
|
|
|
|
|
|
file = tempfile("temp.png")
|
|
|
|
assert_no_exception(lambda: im.save(file))
|
|
|
|
|
|
|
|
def test_save_rgb_single_transparency():
|
|
|
|
in_file = "Tests/images/caption_6_33_22.png"
|
|
|
|
im = Image.open(in_file)
|
|
|
|
|
|
|
|
file = tempfile("temp.png")
|
|
|
|
assert_no_exception(lambda: im.save(file))
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
def test_load_verify():
|
|
|
|
# Check open/load/verify exception (@PIL150)
|
|
|
|
|
|
|
|
im = Image.open("Images/lena.png")
|
|
|
|
assert_no_exception(lambda: im.verify())
|
|
|
|
|
|
|
|
im = Image.open("Images/lena.png")
|
|
|
|
im.load()
|
|
|
|
assert_exception(RuntimeError, lambda: im.verify())
|
|
|
|
|
|
|
|
def test_roundtrip_dpi():
|
|
|
|
# Check dpi roundtripping
|
|
|
|
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
im = roundtrip(im, dpi=(100, 100))
|
|
|
|
assert_equal(im.info["dpi"], (100, 100))
|
|
|
|
|
|
|
|
def test_roundtrip_text():
|
|
|
|
# Check text roundtripping
|
|
|
|
|
|
|
|
im = Image.open(file)
|
|
|
|
|
|
|
|
info = PngImagePlugin.PngInfo()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
info.add_text("TXT", "VALUE")
|
|
|
|
info.add_text("ZIP", "VALUE", 1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
im = roundtrip(im, pnginfo=info)
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
assert_equal(im.info, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
|
|
|
|
assert_equal(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
def test_scary():
|
|
|
|
# Check reading of evil PNG file. For information, see:
|
|
|
|
# http://scary.beasts.org/security/CESA-2004-001.txt
|
|
|
|
|
|
|
|
import base64
|
|
|
|
file = "Tests/images/pngtest_bad.png.base64"
|
2012-10-15 09:55:39 +04:00
|
|
|
data = None
|
|
|
|
|
|
|
|
if py3:
|
|
|
|
data = base64.decodebytes(open(file, 'rb').read())
|
|
|
|
else:
|
|
|
|
data = base64.decodestring(open(file, 'rb').read())
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
file = BytesIO(data)
|
|
|
|
assert_exception(IOError, lambda: Image.open(file))
|
|
|
|
|
|
|
|
def test_trns_rgb():
|
|
|
|
# Check writing and reading of tRNS chunks for RGB images.
|
|
|
|
# Independent file sample provided by Sebastian Spaeth.
|
|
|
|
|
|
|
|
file = "Tests/images/caption_6_33_22.png"
|
|
|
|
im = Image.open(file)
|
|
|
|
assert_equal(im.info["transparency"], (248, 248, 248))
|
|
|
|
|
|
|
|
im = roundtrip(im, transparency=(0, 1, 2))
|
|
|
|
assert_equal(im.info["transparency"], (0, 1, 2))
|