2014-09-04 09:44:46 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper, netpbm_available
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
2014-06-27 07:37:49 +04:00
|
|
|
from PIL import GifImagePlugin
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
codecs = dir(Image.core)
|
|
|
|
|
|
|
|
# sample gif stream
|
2014-09-04 09:44:46 +04:00
|
|
|
TEST_GIF = "Tests/images/hopper.gif"
|
|
|
|
|
|
|
|
with open(TEST_GIF, "rb") as f:
|
2013-05-23 21:02:19 +04:00
|
|
|
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):
|
2014-09-04 09:44:46 +04:00
|
|
|
im = Image.open(TEST_GIF)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.mode, "P")
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.format, "GIF")
|
2015-08-21 15:10:13 +03:00
|
|
|
self.assertEqual(im.info["version"], b"GIF89a")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
def test_invalid_file(self):
|
2015-07-03 09:22:56 +03:00
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
|
|
|
|
self.assertRaises(SyntaxError,
|
|
|
|
lambda: GifImagePlugin.GifImageFile(invalid_file))
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_optimize(self):
|
|
|
|
from io import BytesIO
|
|
|
|
|
2014-11-06 15:29:27 +03:00
|
|
|
def test_grayscale(optimize):
|
2014-06-10 13:10:47 +04:00
|
|
|
im = Image.new("L", (1, 1), 0)
|
2014-11-27 22:43:45 +03:00
|
|
|
filename = BytesIO()
|
|
|
|
im.save(filename, "GIF", optimize=optimize)
|
|
|
|
return len(filename.getvalue())
|
2014-11-06 15:29:27 +03:00
|
|
|
|
|
|
|
def test_bilevel(optimize):
|
|
|
|
im = Image.new("1", (1, 1), 0)
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = BytesIO()
|
|
|
|
im.save(test_file, "GIF", optimize=optimize)
|
|
|
|
return len(test_file.getvalue())
|
2014-11-06 15:29:27 +03:00
|
|
|
|
|
|
|
self.assertEqual(test_grayscale(0), 800)
|
|
|
|
self.assertEqual(test_grayscale(1), 38)
|
|
|
|
self.assertEqual(test_bilevel(0), 800)
|
|
|
|
self.assertEqual(test_bilevel(1), 800)
|
2013-11-08 04:39:36 +04:00
|
|
|
|
2014-08-19 13:32:52 +04:00
|
|
|
def test_optimize_full_l(self):
|
2014-08-19 13:24:44 +04:00
|
|
|
from io import BytesIO
|
|
|
|
|
2014-08-19 16:00:15 +04:00
|
|
|
im = Image.frombytes("L", (16, 16), bytes(bytearray(range(256))))
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = BytesIO()
|
|
|
|
im.save(test_file, "GIF", optimize=True)
|
2014-08-19 13:24:44 +04:00
|
|
|
self.assertEqual(im.mode, "L")
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_roundtrip(self):
|
|
|
|
out = self.tempfile('temp.gif')
|
2014-09-04 09:44:46 +04:00
|
|
|
im = hopper()
|
2014-06-10 13:10:47 +04:00
|
|
|
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-09-04 09:44:46 +04:00
|
|
|
im = Image.open(TEST_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-09-04 09:44:46 +04:00
|
|
|
self.assert_image_similar(reread.convert('RGB'), hopper(), 50)
|
2013-11-08 04:39:57 +04:00
|
|
|
|
2015-06-30 11:07:23 +03:00
|
|
|
def test_roundtrip_save_all(self):
|
2015-07-01 02:18:05 +03:00
|
|
|
# Single frame image
|
2015-06-30 11:07:23 +03:00
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im = hopper()
|
|
|
|
im.save(out, save_all=True)
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
self.assert_image_similar(reread.convert('RGB'), im, 50)
|
|
|
|
|
2015-07-01 02:18:05 +03:00
|
|
|
# Multiframe image
|
|
|
|
im = Image.open("Tests/images/dispose_bgnd.gif")
|
|
|
|
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im.save(out, save_all=True)
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
2015-07-23 12:29:26 +03:00
|
|
|
self.assertEqual(reread.n_frames, 5)
|
2015-07-01 02:18:05 +03:00
|
|
|
|
2015-07-24 12:14:20 +03:00
|
|
|
def test_headers_saving_for_animated_gifs(self):
|
2015-08-21 15:09:05 +03:00
|
|
|
important_headers = ['background', 'version', 'duration', 'loop']
|
2015-07-24 12:14:20 +03:00
|
|
|
# Multiframe image
|
|
|
|
im = Image.open("Tests/images/dispose_bgnd.gif")
|
|
|
|
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im.save(out, save_all=True)
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
for header in important_headers:
|
|
|
|
self.assertEqual(
|
|
|
|
im.info[header],
|
|
|
|
reread.info[header]
|
|
|
|
)
|
|
|
|
|
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-09-04 09:44:46 +04:00
|
|
|
im = Image.open(TEST_GIF)
|
2014-06-10 13:10:47 +04:00
|
|
|
im = im.convert('RGB')
|
2014-03-05 10:02:03 +04:00
|
|
|
|
2014-11-28 01:41:56 +03:00
|
|
|
im = im.resize((100, 100), Image.LANCZOS)
|
2014-06-10 13:10:47 +04:00
|
|
|
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')
|
2015-05-13 09:39:25 +03:00
|
|
|
im.copy().save(out, *args, **kwargs)
|
2014-06-10 13:10:47 +04:00
|
|
|
reloaded = Image.open(out)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2015-05-13 09:39:25 +03:00
|
|
|
return 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
|
|
|
|
2015-05-13 09:39:25 +03:00
|
|
|
self.assert_image_similar(im, roundtrip(im), 1)
|
|
|
|
self.assert_image_similar(im, roundtrip(im, optimize=True), 1)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = im.convert("RGB")
|
|
|
|
# check automatic P conversion
|
2015-05-13 09:39:25 +03:00
|
|
|
reloaded = roundtrip(im).convert('RGB')
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_equal(im, reloaded)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
@unittest.skipUnless(netpbm_available(), "netpbm not available")
|
2014-06-27 07:37:49 +04:00
|
|
|
def test_save_netpbm_bmp_mode(self):
|
2014-09-04 10:42:31 +04:00
|
|
|
img = Image.open(TEST_GIF).convert("RGB")
|
2014-06-27 07:37:49 +04:00
|
|
|
|
|
|
|
tempfile = self.tempfile("temp.gif")
|
|
|
|
GifImagePlugin._save_netpbm(img, 0, tempfile)
|
|
|
|
self.assert_image_similar(img, Image.open(tempfile).convert("RGB"), 0)
|
|
|
|
|
2014-06-28 02:12:37 +04:00
|
|
|
@unittest.skipUnless(netpbm_available(), "netpbm not available")
|
2014-06-27 07:37:49 +04:00
|
|
|
def test_save_netpbm_l_mode(self):
|
2014-09-04 10:42:31 +04:00
|
|
|
img = Image.open(TEST_GIF).convert("L")
|
2014-06-27 07:37:49 +04:00
|
|
|
|
|
|
|
tempfile = self.tempfile("temp.gif")
|
|
|
|
GifImagePlugin._save_netpbm(img, 0, tempfile)
|
|
|
|
self.assert_image_similar(img, Image.open(tempfile).convert("L"), 0)
|
|
|
|
|
2014-07-07 22:47:18 +04:00
|
|
|
def test_seek(self):
|
|
|
|
img = Image.open("Tests/images/dispose_none.gif")
|
|
|
|
framecount = 0
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
framecount += 1
|
2014-08-26 17:47:10 +04:00
|
|
|
img.seek(img.tell() + 1)
|
2014-07-07 22:47:18 +04:00
|
|
|
except EOFError:
|
2014-07-07 23:49:45 +04:00
|
|
|
self.assertEqual(framecount, 5)
|
2014-07-07 22:47:18 +04:00
|
|
|
|
2015-06-07 18:01:34 +03:00
|
|
|
def test_n_frames(self):
|
2015-06-18 17:49:18 +03:00
|
|
|
im = Image.open(TEST_GIF)
|
|
|
|
self.assertEqual(im.n_frames, 1)
|
2015-06-30 06:25:00 +03:00
|
|
|
self.assertFalse(im.is_animated)
|
2015-06-18 17:49:18 +03:00
|
|
|
|
2015-06-07 18:01:34 +03:00
|
|
|
im = Image.open("Tests/images/iss634.gif")
|
2015-06-18 17:49:18 +03:00
|
|
|
self.assertEqual(im.n_frames, 42)
|
2015-06-30 06:25:00 +03:00
|
|
|
self.assertTrue(im.is_animated)
|
2015-06-18 17:49:18 +03:00
|
|
|
|
|
|
|
def test_eoferror(self):
|
|
|
|
im = Image.open(TEST_GIF)
|
|
|
|
|
|
|
|
n_frames = im.n_frames
|
|
|
|
while True:
|
|
|
|
n_frames -= 1
|
|
|
|
try:
|
|
|
|
im.seek(n_frames)
|
|
|
|
break
|
|
|
|
except EOFError:
|
|
|
|
self.assertTrue(im.tell() < n_frames)
|
2015-06-07 18:01:34 +03:00
|
|
|
|
2014-07-07 22:47:18 +04:00
|
|
|
def test_dispose_none(self):
|
|
|
|
img = Image.open("Tests/images/dispose_none.gif")
|
|
|
|
try:
|
|
|
|
while True:
|
2014-08-26 17:47:10 +04:00
|
|
|
img.seek(img.tell() + 1)
|
2014-07-07 23:49:45 +04:00
|
|
|
self.assertEqual(img.disposal_method, 1)
|
2014-07-07 22:47:18 +04:00
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_dispose_background(self):
|
|
|
|
img = Image.open("Tests/images/dispose_bgnd.gif")
|
|
|
|
try:
|
|
|
|
while True:
|
2014-08-26 17:47:10 +04:00
|
|
|
img.seek(img.tell() + 1)
|
2014-07-07 23:49:45 +04:00
|
|
|
self.assertEqual(img.disposal_method, 2)
|
2014-07-07 22:47:18 +04:00
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_dispose_previous(self):
|
|
|
|
img = Image.open("Tests/images/dispose_prev.gif")
|
|
|
|
try:
|
|
|
|
while True:
|
2014-08-26 17:47:10 +04:00
|
|
|
img.seek(img.tell() + 1)
|
2014-07-07 23:49:45 +04:00
|
|
|
self.assertEqual(img.disposal_method, 3)
|
2014-07-07 22:47:18 +04:00
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_iss634(self):
|
|
|
|
img = Image.open("Tests/images/iss634.gif")
|
|
|
|
# seek to the second frame
|
2014-08-26 17:47:10 +04:00
|
|
|
img.seek(img.tell() + 1)
|
|
|
|
# all transparent pixels should be replaced with the color from the
|
|
|
|
# first frame
|
2014-07-07 23:49:45 +04:00
|
|
|
self.assertEqual(img.histogram()[img.info['transparency']], 0)
|
2014-07-07 22:47:18 +04:00
|
|
|
|
2015-04-04 03:45:30 +03:00
|
|
|
def test_duration(self):
|
|
|
|
duration = 1000
|
|
|
|
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
fp = open(out, "wb")
|
2015-04-23 16:40:42 +03:00
|
|
|
im = Image.new('L', (100, 100), '#000')
|
2015-04-04 03:45:30 +03:00
|
|
|
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
|
|
|
|
fp.write(s)
|
|
|
|
fp.write(b";")
|
|
|
|
fp.close()
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
self.assertEqual(reread.info['duration'], duration)
|
|
|
|
|
|
|
|
def test_number_of_loops(self):
|
|
|
|
number_of_loops = 2
|
|
|
|
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
fp = open(out, "wb")
|
2015-04-23 16:40:42 +03:00
|
|
|
im = Image.new('L', (100, 100), '#000')
|
2015-04-04 03:45:30 +03:00
|
|
|
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
|
|
|
|
fp.write(s)
|
|
|
|
fp.write(b";")
|
|
|
|
fp.close()
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
self.assertEqual(reread.info['loop'], number_of_loops)
|
2014-03-05 10:29:55 +04:00
|
|
|
|
2015-06-11 04:10:05 +03:00
|
|
|
def test_background(self):
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im = Image.new('L', (100, 100), '#000')
|
|
|
|
im.info['background'] = 1
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
self.assertEqual(reread.info['background'], im.info['background'])
|
|
|
|
|
2016-05-07 06:57:40 +03:00
|
|
|
def test_comment(self):
|
|
|
|
im = Image.open(TEST_GIF)
|
|
|
|
self.assertEqual(im.info['comment'], b"File written by Adobe Photoshop\xa8 4.0")
|
|
|
|
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
im = Image.new('L', (100, 100), '#000')
|
|
|
|
im.info['comment'] = b"Test comment text"
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
|
|
|
|
self.assertEqual(reread.info['comment'], im.info['comment'])
|
|
|
|
|
2015-08-21 15:10:13 +03:00
|
|
|
def test_version(self):
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
|
|
|
|
# Test that GIF87a is used by default
|
|
|
|
im = Image.new('L', (100, 100), '#000')
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.info["version"], b"GIF87a")
|
|
|
|
|
|
|
|
# Test that adding a GIF89a feature changes the version
|
|
|
|
im.info["transparency"] = 1
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.info["version"], b"GIF89a")
|
|
|
|
|
|
|
|
# Test that a GIF87a image is also saved in that format
|
2016-05-07 06:57:40 +03:00
|
|
|
im = Image.open("Tests/images/test.colors.gif")
|
2015-08-21 15:10:13 +03:00
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.info["version"], b"GIF87a")
|
|
|
|
|
|
|
|
# Test that a GIF89a image is also saved in that format
|
|
|
|
im.info["version"] = "GIF89a"
|
|
|
|
im.save(out)
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.info["version"], b"GIF87a")
|
|
|
|
|
2016-09-11 05:04:01 +03:00
|
|
|
def test_append_images(self):
|
|
|
|
out = self.tempfile('temp.gif')
|
|
|
|
|
|
|
|
# Test appending single frame images
|
|
|
|
im = Image.new('RGB', (100, 100), '#f00')
|
|
|
|
ims = [Image.new('RGB', (100, 100), color) for color in ['#0f0', '#00f']]
|
|
|
|
im.save(out, save_all=True, append_images=ims)
|
|
|
|
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.n_frames, 3)
|
|
|
|
|
|
|
|
# Tests appending single and multiple frame images
|
|
|
|
im = Image.open("Tests/images/dispose_none.gif")
|
|
|
|
ims = [Image.open("Tests/images/dispose_prev.gif")]
|
|
|
|
im.save(out, save_all=True, append_images=ims)
|
|
|
|
|
|
|
|
reread = Image.open(out)
|
|
|
|
self.assertEqual(reread.n_frames, 10)
|
2015-08-21 15:10:13 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|