Corrected version number when saving GIFs

This commit is contained in:
Andrew Murray 2015-08-21 22:10:13 +10:00
parent f64bc891d4
commit b8ff91ab3b
2 changed files with 41 additions and 1 deletions

View File

@ -537,8 +537,19 @@ def getheader(im, palette=None, info=None):
# Header Block
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
version = b"87a"
for extensionKey in ["transparency", "duration", "loop"]:
if info and extensionKey in info and \
not (extensionKey == "duration" and info[extensionKey] == 0):
version = b"89a"
break
else:
if im.info.get("version") == "89a":
version = b"89a"
header = [
b"GIF87a" + # signature + version
b"GIF"+version + # signature + version
o16(im.size[0]) + # canvas width
o16(im.size[1]) # canvas height
]

View File

@ -24,6 +24,7 @@ class TestFileGif(PillowTestCase):
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "GIF")
self.assertEqual(im.info["version"], b"GIF89a")
def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"
@ -266,6 +267,34 @@ class TestFileGif(PillowTestCase):
self.assertEqual(reread.info['background'], im.info['background'])
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
im = Image.open(TEST_GIF)
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")
if __name__ == '__main__':
unittest.main()