Added duration set to GifImagePlugin

This commit is contained in:
Andrew Murray 2015-04-03 23:22:13 +11:00
parent 935d6949e9
commit e36e7dd7a2
2 changed files with 46 additions and 38 deletions

View File

@ -292,8 +292,23 @@ def _save(im, fp, filename):
for s in header: for s in header:
fp.write(s) fp.write(s)
flags = 0 # local image header
get_local_header(fp, im)
im_out.encoderconfig = (8, get_interlace(im))
ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0,
RAWMODE[im_out.mode])])
fp.write(b"\0") # end of image data
fp.write(b";") # end of file
try:
fp.flush()
except:
pass
def get_interlace(im):
try: try:
interlace = im.encoderinfo["interlace"] interlace = im.encoderinfo["interlace"]
except KeyError: except KeyError:
@ -302,10 +317,11 @@ def _save(im, fp, filename):
# workaround for @PIL153 # workaround for @PIL153
if min(im.size) < 16: if min(im.size) < 16:
interlace = 0 interlace = 0
return interlace
if interlace: def get_local_header(fp, im, offset=(0, 0)):
flags = flags | 64 transparent_color_exists = False
try: try:
transparency = im.encoderinfo["transparency"] transparency = im.encoderinfo["transparency"]
except KeyError: except KeyError:
@ -324,38 +340,36 @@ def _save(im, fp, filename):
else: else:
transparent_color_exists = False transparent_color_exists = False
# transparency extension block if 'duration' in im.encoderinfo:
if transparent_color_exists: duration = im.encoderinfo["duration"] / 10
fp.write(b"!" + else:
o8(249) + # extension intro duration = 0
o8(4) + # length if transparent_color_exists or duration != 0:
o8(1) + # transparency info present transparency_flag = 1 if transparent_color_exists else 0
o16(0) + # duration if not transparent_color_exists:
o8(transparency) + # transparency index transparency = 0
o8(0))
fp.write(b"!" +
o8(249) + # extension intro
o8(4) + # length
o8(transparency_flag) + # transparency info present
o16(duration) + # duration
o8(transparency) + # transparency index
o8(0))
# local image header flags = 0
if get_interlace(im):
flags = flags | 64
fp.write(b"," + fp.write(b"," +
o16(0) + o16(0) + # bounding box o16(offset[0]) + # offset
o16(offset[1]) +
o16(im.size[0]) + # size o16(im.size[0]) + # size
o16(im.size[1]) + o16(im.size[1]) +
o8(flags) + # flags o8(flags) + # flags
o8(8)) # bits o8(8)) # bits
im_out.encoderconfig = (8, interlace)
ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0,
RAWMODE[im_out.mode])])
fp.write(b"\0") # end of image data
fp.write(b";") # end of file
try:
fp.flush()
except:
pass
def _save_netpbm(im, fp, filename): def _save_netpbm(im, fp, filename):
# #
@ -510,13 +524,7 @@ def getdata(im, offset=(0, 0), **params):
im.encoderinfo = params im.encoderinfo = params
# local image header # local image header
fp.write(b"," + get_local_header(fp, im, offset)
o16(offset[0]) + # offset
o16(offset[1]) +
o16(im.size[0]) + # size
o16(im.size[1]) +
o8(0) + # flags
o8(8)) # bits
ImageFile._save(im, fp, [("gif", (0, 0)+im.size, 0, RAWMODE[im.mode])]) ImageFile._save(im, fp, [("gif", (0, 0)+im.size, 0, RAWMODE[im.mode])])

View File

@ -72,8 +72,8 @@ def makedelta(fp, sequence):
for im in sequence: for im in sequence:
# # To specify duration, add the time in milliseconds to getdata(),
# FIXME: write graphics control block before each frame # e.g. getdata(im, duration=1000)
if not previous: if not previous: