Simplified code

This commit is contained in:
Andrew Murray 2017-01-28 11:45:59 +11:00
parent bc8cd5fabf
commit af57ff8d45
2 changed files with 22 additions and 30 deletions

View File

@ -294,7 +294,7 @@ except ImportError:
RAWMODE = { RAWMODE = {
"1": "L", "1": "L",
"L": "L", "L": "L",
"P": "P", "P": "P"
} }
@ -317,7 +317,6 @@ def _save_all(im, fp, filename):
def _save(im, fp, filename, save_all=False): def _save(im, fp, filename, save_all=False):
im.encoderinfo.update(im.info) im.encoderinfo.update(im.info)
if _imaging_gif: if _imaging_gif:
# call external driver # call external driver
@ -362,7 +361,7 @@ def _save(im, fp, filename, save_all=False):
# delta frame # delta frame
previous = im_frames[-1] previous = im_frames[-1]
delta = ImageChops.subtract_modulo(im_frame, delta = ImageChops.subtract_modulo(im_frame,
previous['im_frame']) previous['im'])
bbox = delta.getbbox() bbox = delta.getbbox()
if not bbox: if not bbox:
# This frame is identical to the previous frame # This frame is identical to the previous frame
@ -372,37 +371,36 @@ def _save(im, fp, filename, save_all=False):
else: else:
bbox = None bbox = None
im_frames.append({ im_frames.append({
"im_frame":im_frame, 'im':im_frame,
"bbox":bbox, 'bbox':bbox,
"encoderinfo":encoderinfo 'encoderinfo':encoderinfo
}) })
if len(im_frames) < 2: if len(im_frames) < 2:
save_all = False save_all = False
else: else:
for data in im_frames: for frame_data in im_frames:
if data['bbox'] is None: im_frame = frame_data['im']
if not frame_data['bbox']:
# global header # global header
header = getheader(data['im_frame'], palette, data['encoderinfo'])[0] for s in getheader(im_frame, palette, frame_data['encoderinfo'])[0]:
for s in header + getdata(data['im_frame'],
(0, 0), **data['encoderinfo']):
fp.write(s) fp.write(s)
offset = (0, 0)
else: else:
# compress difference # compress difference
data['encoderinfo']['include_color_table'] = True frame_data['encoderinfo']['include_color_table'] = True
for s in getdata(data['im_frame'].crop(data['bbox']),
data['bbox'][:2], **data['encoderinfo']): im_frame = im_frame.crop(frame_data['bbox'])
fp.write(s) offset = frame_data['bbox'][:2]
for s in getdata(im_frame, offset, **frame_data['encoderinfo']):
fp.write(s)
if not save_all: if not save_all:
header = getheader(im_out, palette, im.encoderinfo)[0] for s in getheader(im_out, palette, im.encoderinfo)[0]:
for s in header:
fp.write(s) fp.write(s)
# local image header
flags = 0 flags = 0
if get_interlace(im): if get_interlace(im):
flags = flags | 64 flags = flags | 64
# local image header
_get_local_header(fp, im, (0, 0), flags) _get_local_header(fp, im, (0, 0), flags)
im_out.encoderconfig = (8, get_interlace(im)) im_out.encoderconfig = (8, get_interlace(im))

View File

@ -269,11 +269,8 @@ class TestFileGif(PillowTestCase):
duration = 1000 duration = 1000
out = self.tempfile('temp.gif') out = self.tempfile('temp.gif')
with open(out, "wb") as fp: im = Image.new('L', (100, 100), '#000')
im = Image.new('L', (100, 100), '#000') im.save(out, duration=duration)
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
fp.write(s)
fp.write(b";")
reread = Image.open(out) reread = Image.open(out)
self.assertEqual(reread.info['duration'], duration) self.assertEqual(reread.info['duration'], duration)
@ -350,11 +347,8 @@ class TestFileGif(PillowTestCase):
number_of_loops = 2 number_of_loops = 2
out = self.tempfile('temp.gif') out = self.tempfile('temp.gif')
with open(out, "wb") as fp: im = Image.new('L', (100, 100), '#000')
im = Image.new('L', (100, 100), '#000') im.save(out, loop=number_of_loops)
for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
fp.write(s)
fp.write(b";")
reread = Image.open(out) reread = Image.open(out)
self.assertEqual(reread.info['loop'], number_of_loops) self.assertEqual(reread.info['loop'], number_of_loops)