2014-09-05 14:03:56 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper, fromstring, tostring
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
from io import BytesIO
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-06-10 13:10:47 +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
|
|
|
|
|
|
|
|
|
2014-07-19 21:21:18 +04:00
|
|
|
class TestImageFile(PillowTestCase):
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_parser(self):
|
|
|
|
|
|
|
|
def roundtrip(format):
|
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper("L").resize((1000, 1000))
|
2014-06-10 13:10:47 +04:00
|
|
|
if format in ("MSP", "XBM"):
|
|
|
|
im = im.convert("1")
|
|
|
|
|
|
|
|
file = BytesIO()
|
|
|
|
|
|
|
|
im.save(file, format)
|
|
|
|
|
|
|
|
data = file.getvalue()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
parser = ImageFile.Parser()
|
|
|
|
parser.feed(data)
|
|
|
|
imOut = parser.close()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
return im, imOut
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_equal(*roundtrip("BMP"))
|
|
|
|
self.assert_image_equal(*roundtrip("GIF"))
|
|
|
|
self.assert_image_equal(*roundtrip("IM"))
|
|
|
|
self.assert_image_equal(*roundtrip("MSP"))
|
|
|
|
if "zip_encoder" in codecs:
|
|
|
|
try:
|
|
|
|
# force multiple blocks in PNG driver
|
|
|
|
ImageFile.MAXBLOCK = 8192
|
|
|
|
self.assert_image_equal(*roundtrip("PNG"))
|
|
|
|
finally:
|
|
|
|
ImageFile.MAXBLOCK = MAXBLOCK
|
|
|
|
self.assert_image_equal(*roundtrip("PPM"))
|
|
|
|
self.assert_image_equal(*roundtrip("TIFF"))
|
|
|
|
self.assert_image_equal(*roundtrip("XBM"))
|
|
|
|
self.assert_image_equal(*roundtrip("TGA"))
|
|
|
|
self.assert_image_equal(*roundtrip("PCX"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if EpsImagePlugin.has_ghostscript():
|
|
|
|
im1, im2 = roundtrip("EPS")
|
2014-09-30 09:14:26 +04:00
|
|
|
# This test fails on Ubuntu 12.04, PPC (Bigendian) It
|
|
|
|
# appears to be a ghostscript 9.05 bug, since the
|
|
|
|
# ghostscript rendering is wonky and the file is identical
|
|
|
|
# to that written on ubuntu 12.04 x64
|
|
|
|
# md5sum: ba974835ff2d6f3f2fd0053a23521d4a
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# EPS comes back in RGB:
|
|
|
|
self.assert_image_similar(im1, im2.convert('L'), 20)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if "jpeg_encoder" in codecs:
|
|
|
|
im1, im2 = roundtrip("JPEG") # lossy compression
|
|
|
|
self.assert_image(im1, im2.mode, im2.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertRaises(IOError, lambda: roundtrip("PDF"))
|
|
|
|
|
|
|
|
def test_ico(self):
|
|
|
|
with open('Tests/images/python.ico', 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
p = ImageFile.Parser()
|
|
|
|
p.feed(data)
|
|
|
|
self.assertEqual((48, 48), p.image.size)
|
|
|
|
|
|
|
|
def test_safeblock(self):
|
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im1 = hopper()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if "zip_encoder" not in codecs:
|
|
|
|
self.skipTest("PNG (zlib) encoder not available")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2013-04-21 12:13:33 +04:00
|
|
|
try:
|
2014-06-10 13:10:47 +04:00
|
|
|
ImageFile.SAFEBLOCK = 1
|
|
|
|
im2 = fromstring(tostring(im1, "PNG"))
|
2013-04-21 12:13:33 +04:00
|
|
|
finally:
|
2014-06-10 13:10:47 +04:00
|
|
|
ImageFile.SAFEBLOCK = SAFEBLOCK
|
|
|
|
|
|
|
|
self.assert_image_equal(im1, im2)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|