2014-06-10 13:10:47 +04:00
|
|
|
from helper import unittest, PillowTestCase, tearDownModule, lena
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
|
|
|
# sample gif stream
|
2014-06-23 10:19:29 +04:00
|
|
|
file = "Tests/images/lena.gif"
|
2013-05-23 21:02:19 +04:00
|
|
|
with open(file, "rb") as f:
|
|
|
|
data = f.read()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileGif(PillowTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
if "gif_encoder" not in codecs or "gif_decoder" not in codecs:
|
|
|
|
self.skipTest("gif support not available") # can this happen?
|
|
|
|
|
|
|
|
def test_sanity(self):
|
|
|
|
im = Image.open(file)
|
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.mode, "P")
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.format, "GIF")
|
|
|
|
|
|
|
|
def test_optimize(self):
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
def test(optimize):
|
|
|
|
im = Image.new("L", (1, 1), 0)
|
|
|
|
file = BytesIO()
|
|
|
|
im.save(file, "GIF", optimize=optimize)
|
|
|
|
return len(file.getvalue())
|
|
|
|
self.assertEqual(test(0), 800)
|
|
|
|
self.assertEqual(test(1), 38)
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip(self):
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im = lena()
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_similar(reread.convert('RGB'), im, 50)
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip2(self):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/403
|
|
|
|
out = self.tempfile('temp.gif')
|
2014-06-23 10:19:29 +04:00
|
|
|
im = Image.open('Tests/images/lena.gif')
|
2014-06-10 13:10:47 +04:00
|
|
|
im2 = im.copy()
|
|
|
|
im2.save(out)
|
|
|
|
reread = Image.open(out)
|
2013-11-08 04:39:57 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_similar(reread.convert('RGB'), lena(), 50)
|
2013-11-08 04:39:57 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_palette_handling(self):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/513
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-23 10:19:29 +04:00
|
|
|
im = Image.open('Tests/images/lena.gif')
|
2014-06-10 13:10:47 +04:00
|
|
|
im = im.convert('RGB')
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = im.resize((100, 100), Image.ANTIALIAS)
|
|
|
|
im2 = im.convert('P', palette=Image.ADAPTIVE, colors=256)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
f = self.tempfile('temp.gif')
|
|
|
|
im2.save(f, optimize=True)
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
reloaded = Image.open(f)
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_similar(im, reloaded.convert('RGB'), 10)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_palette_434(self):
|
|
|
|
# see https://github.com/python-pillow/Pillow/issues/434
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def roundtrip(im, *args, **kwargs):
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im.save(out, *args, **kwargs)
|
|
|
|
reloaded = Image.open(out)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
return [im, reloaded]
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
orig = "Tests/images/test.colors.gif"
|
|
|
|
im = Image.open(orig)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_equal(*roundtrip(im))
|
|
|
|
self.assert_image_equal(*roundtrip(im, optimize=True))
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = im.convert("RGB")
|
|
|
|
# check automatic P conversion
|
|
|
|
reloaded = roundtrip(im)[1].convert('RGB')
|
|
|
|
self.assert_image_equal(im, reloaded)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# End of file
|