Pillow/Tests/test_file_png.py
Brian Crowell a7e3b2e47b 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.
2013-01-10 08:46:56 -06:00

177 lines
4.3 KiB
Python

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
IHDR = chunk(b"IHDR", o32(1), o32(1), b'\x08\x02', b'\0\0\0')
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)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'tEXt', b'spam\0') + TAIL)
assert_equal(im.info, {'spam': ''})
im = load(HEAD + chunk(b'tEXt', b'spam\0egg') + TAIL)
assert_equal(im.info, {'spam': 'egg'})
im = load(HEAD + chunk(b'tEXt', b'spam\0egg\0') + TAIL)
assert_equal(im.info, {'spam': 'egg\x00'})
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())
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()
info.add_text("TXT", "VALUE")
info.add_text("ZIP", "VALUE", 1)
im = roundtrip(im, pnginfo=info)
assert_equal(im.info, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
assert_equal(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
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"
data = None
if py3:
data = base64.decodebytes(open(file, 'rb').read())
else:
data = base64.decodestring(open(file, 'rb').read())
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))