2012-10-16 00:26:38 +04:00
|
|
|
from tester import *
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageFile
|
2014-04-05 00:33:54 +04:00
|
|
|
from PIL import EpsImagePlugin
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2013-04-21 12:13:33 +04:00
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
# save original block sizes
|
|
|
|
MAXBLOCK = ImageFile.MAXBLOCK
|
|
|
|
SAFEBLOCK = ImageFile.SAFEBLOCK
|
|
|
|
|
|
|
|
def test_parser():
|
|
|
|
|
|
|
|
def roundtrip(format):
|
|
|
|
|
|
|
|
im = lena("L").resize((1000, 1000))
|
|
|
|
if format in ("MSP", "XBM"):
|
|
|
|
im = im.convert("1")
|
|
|
|
|
|
|
|
file = BytesIO()
|
|
|
|
|
|
|
|
im.save(file, format)
|
|
|
|
|
|
|
|
data = file.getvalue()
|
|
|
|
|
|
|
|
parser = ImageFile.Parser()
|
|
|
|
parser.feed(data)
|
|
|
|
imOut = parser.close()
|
|
|
|
|
|
|
|
return im, imOut
|
|
|
|
|
|
|
|
assert_image_equal(*roundtrip("BMP"))
|
|
|
|
assert_image_equal(*roundtrip("GIF"))
|
|
|
|
assert_image_equal(*roundtrip("IM"))
|
|
|
|
assert_image_equal(*roundtrip("MSP"))
|
2013-04-21 12:13:33 +04:00
|
|
|
if "zip_encoder" in codecs:
|
|
|
|
try:
|
|
|
|
# force multiple blocks in PNG driver
|
|
|
|
ImageFile.MAXBLOCK = 8192
|
|
|
|
assert_image_equal(*roundtrip("PNG"))
|
|
|
|
finally:
|
|
|
|
ImageFile.MAXBLOCK = MAXBLOCK
|
2012-10-16 00:26:38 +04:00
|
|
|
assert_image_equal(*roundtrip("PPM"))
|
|
|
|
assert_image_equal(*roundtrip("TIFF"))
|
|
|
|
assert_image_equal(*roundtrip("XBM"))
|
2012-10-24 06:58:15 +04:00
|
|
|
assert_image_equal(*roundtrip("TGA"))
|
2012-10-24 07:22:38 +04:00
|
|
|
assert_image_equal(*roundtrip("PCX"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-04-05 00:33:54 +04:00
|
|
|
if EpsImagePlugin.has_ghostscript():
|
|
|
|
im1, im2 = roundtrip("EPS")
|
2014-06-03 14:02:44 +04:00
|
|
|
assert_image_similar(im1, im2.convert('L'),20) # EPS comes back in RGB
|
|
|
|
|
2013-04-21 12:13:33 +04:00
|
|
|
if "jpeg_encoder" in codecs:
|
|
|
|
im1, im2 = roundtrip("JPEG") # lossy compression
|
|
|
|
assert_image(im1, im2.mode, im2.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2013-03-08 18:32:08 +04:00
|
|
|
# XXX Why assert exception and why does it fail?
|
2014-06-03 14:02:44 +04:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/78
|
2013-03-08 18:32:08 +04:00
|
|
|
#assert_exception(IOError, lambda: roundtrip("PDF"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-02-17 08:58:06 +04:00
|
|
|
def test_ico():
|
|
|
|
with open('Tests/images/python.ico', 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
p = ImageFile.Parser()
|
|
|
|
p.feed(data)
|
|
|
|
assert_equal((48,48), p.image.size)
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
def test_safeblock():
|
|
|
|
|
|
|
|
im1 = lena()
|
|
|
|
|
2013-04-21 12:13:33 +04:00
|
|
|
if "zip_encoder" not in codecs:
|
|
|
|
skip("PNG (zlib) encoder not available")
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
try:
|
|
|
|
ImageFile.SAFEBLOCK = 1
|
|
|
|
im2 = fromstring(tostring(im1, "PNG"))
|
|
|
|
finally:
|
|
|
|
ImageFile.SAFEBLOCK = SAFEBLOCK
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
assert_image_equal(im1, im2)
|