def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": imOut = im.convert("P") rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header for s in getheader(imOut, im.encoderinfo): fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except: # default is on (since we're writing uncompressed images) interlace = 1 flags = flags | 64 # local image header fp.write( "," + o16(0) + o16(0) + o16(im.size[0]) + o16(im.size[1]) + chr(flags) + chr(8) # bounding box # size # flags ) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0, 0) + im.size, 0, rawmode)]) fp.write("\0") # end of image data fp.write(";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file # header for s in getheader(im): fp.write(s) # local image header fp.write("," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + chr(0) + # flags chr(8)) # bits ImageFileH._save(im, fp, [("gif", (0,0)+im.size, 0, RAWMODE[im.mode])]) fp.write("\0") # end of image data fp.write(";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file # header for s in getheader(im): fp.write(s) # local image header fp.write("," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + chr(0) + # flags chr(8)) # bits ImageFileH._save(im, fp, [("gif", (0, 0) + im.size, 0, RAWMODE[im.mode])]) fp.write("\0") # end of image data fp.write(";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file if im.mode in RAWMODE: im_out = im else: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": palette_size = 256 if im.palette: palette_size = len(im.palette.getdata()[1]) // 3 im_out = im.convert("P", palette=1, colors=palette_size) else: im_out = im.convert("L") # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) header, used_palette_colors = getheader(im_out, palette, im.encoderinfo) for s in header: fp.write(s) flags = 0 if get_interlace(im): flags = flags | 64 # local image header get_local_header(fp, im, (0, 0), flags) 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 _save(im, fp, filename, save_all=False): im.encoderinfo.update(im.info) if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file if im.mode in RAWMODE: im_out = im.copy() else: im_out = _convert_mode(im, True) # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) if save_all: previous = None first_frame = None append_images = im.encoderinfo.get("append_images", []) if "duration" in im.encoderinfo: duration = im.encoderinfo["duration"] else: duration = None frame_count = 0 for imSequence in [im] + append_images: for im_frame in ImageSequence.Iterator(imSequence): encoderinfo = im.encoderinfo.copy() im_frame = _convert_mode(im_frame) if isinstance(duration, (list, tuple)): encoderinfo["duration"] = duration[frame_count] frame_count += 1 # To specify duration, add the time in milliseconds to getdata(), # e.g. getdata(im_frame, duration=1000) if not previous: # global header first_frame = getheader(im_frame, palette, encoderinfo)[0] first_frame += getdata(im_frame, (0, 0), **encoderinfo) else: if first_frame: for s in first_frame: fp.write(s) first_frame = None # delta frame delta = ImageChops.subtract_modulo(im_frame, previous.copy()) bbox = delta.getbbox() if bbox: # compress difference encoderinfo['include_color_table'] = True for s in getdata(im_frame.crop(bbox), bbox[:2], **encoderinfo): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im_frame if first_frame: save_all = False if not save_all: header = getheader(im_out, palette, im.encoderinfo)[0] for s in header: fp.write(s) flags = 0 if get_interlace(im): flags = flags | 64 # local image header _get_local_header(fp, im, (0, 0), flags) 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 if hasattr(fp, "flush"): fp.flush()
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": imOut = im.convert("P") rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header for s in getheader(imOut, im.encoderinfo): fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except KeyError: interlace = 1 # workaround for @PIL153 if min(im.size) < 16: interlace = 0 if interlace: flags = flags | 64 try: transparency = im.encoderinfo["transparency"] except KeyError: pass else: # transparency extension block fp.write("!" + chr(249) + # extension intro chr(4) + # length chr(1) + # transparency info present o16(0) + # duration chr(int(transparency)) # transparency index + chr(0)) # local image header fp.write("," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + chr(flags) + # flags chr(8)) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0,0)+im.size, 0, rawmode)]) fp.write("\0") # end of image data fp.write(";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": imOut = im.convert("P") rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None if im.palette: # use existing if possible palette = im.palette.getdata()[1] header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo) for s in header: fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except KeyError: interlace = 1 # workaround for @PIL153 if min(im.size) < 16: interlace = 0 if interlace: flags = flags | 64 try: transparency = im.encoderinfo["transparency"] except KeyError: pass else: transparency = int(transparency) # optimize the block away if transparent color is not used transparentColorExists = True # adjust the transparency index after optimize if usedPaletteColors is not None and len(usedPaletteColors) < 256: for i in range(len(usedPaletteColors)): if usedPaletteColors[i] == transparency: transparency = i transparentColorExists = True break else: transparentColorExists = False # transparency extension block if transparentColorExists: fp.write( b"!" + o8(249) + o8(4) # extension intro + o8(1) # length + o16(0) # transparency info present + o8(transparency) # duration # transparency index + o8(0) ) # local image header fp.write( b"," + o16(0) + o16(0) + o16(im.size[0]) + o16(im.size[1]) + o8(flags) + o8(8) # bounding box # size # flags ) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0, 0) + im.size, 0, rawmode)]) fp.write(b"\0") # end of image data fp.write(b";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename, save_all=False): im.encoderinfo.update(im.info) if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file if im.mode in RAWMODE: im_out = im.copy() else: im_out = _convert_mode(im, True) # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) if save_all: previous = None first_frame = None append_images = im.encoderinfo.get("append_images", []) if "duration" in im.encoderinfo: duration = im.encoderinfo["duration"] else: duration = None frame_count = 0 for imSequence in [im]+append_images: for im_frame in ImageSequence.Iterator(imSequence): encoderinfo = im.encoderinfo.copy() im_frame = _convert_mode(im_frame) if isinstance(duration, (list, tuple)): encoderinfo["duration"] = duration[frame_count] frame_count += 1 # To specify duration, add the time in milliseconds to getdata(), # e.g. getdata(im_frame, duration=1000) if not previous: # global header first_frame = getheader(im_frame, palette, encoderinfo)[0] first_frame += getdata(im_frame, (0, 0), **encoderinfo) else: if first_frame: for s in first_frame: fp.write(s) first_frame = None # delta frame delta = ImageChops.subtract_modulo(im_frame, previous.copy()) bbox = delta.getbbox() if bbox: # compress difference encoderinfo['include_color_table'] = True for s in getdata(im_frame.crop(bbox), bbox[:2], **encoderinfo): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im_frame if first_frame: save_all = False if not save_all: header = getheader(im_out, palette, im.encoderinfo)[0] for s in header: fp.write(s) flags = 0 if get_interlace(im): flags = flags | 64 # local image header _get_local_header(fp, im, (0, 0), flags) 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 if hasattr(fp, "flush"): fp.flush()
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": palette_size = 256 if im.palette: palette_size = len(im.palette.getdata()[1]) // 3 imOut = im.convert("P", palette=1, colors=palette_size) rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) if im.encoderinfo["optimize"]: # When the mode is L, and we optimize, we end up with # im.mode == P and rawmode = L, which fails. # If we're optimizing the palette, we're going to be # in a rawmode of P anyway. rawmode = 'P' header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo) for s in header: fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except KeyError: interlace = 1 # workaround for @PIL153 if min(im.size) < 16: interlace = 0 if interlace: flags = flags | 64 try: transparency = im.encoderinfo["transparency"] except KeyError: pass else: transparency = int(transparency) # optimize the block away if transparent color is not used transparentColorExists = True # adjust the transparency index after optimize if usedPaletteColors is not None and len(usedPaletteColors) < 256: for i in range(len(usedPaletteColors)): if usedPaletteColors[i] == transparency: transparency = i transparentColorExists = True break else: transparentColorExists = False # transparency extension block if transparentColorExists: fp.write(b"!" + o8(249) + # extension intro o8(4) + # length o8(1) + # transparency info present o16(0) + # duration o8(transparency) # transparency index + o8(0)) # local image header fp.write(b"," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + o8(flags) + # flags o8(8)) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0,0)+im.size, 0, rawmode)]) fp.write(b"\0") # end of image data fp.write(b";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": imOut = im.convert("P") rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header for s in getheader(imOut, im.encoderinfo): fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except KeyError: interlace = 1 # workaround for @PIL153 if min(im.size) < 16: interlace = 0 if interlace: flags = flags | 64 try: transparency = im.encoderinfo["transparency"] except KeyError: pass else: # transparency extension block fp.write("!" + chr(249) + # extension intro chr(4) + # length chr(1) + # transparency info present o16(0) + # duration chr(int(transparency)) # transparency index + chr(0)) # local image header fp.write("," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + chr(flags) + # flags chr(8)) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0, 0) + im.size, 0, rawmode)]) fp.write("\0") # end of image data fp.write(";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file try: rawmode = RAWMODE[im.mode] imOut = im except KeyError: # convert on the fly (EXPERIMENTAL -- I'm not sure PIL # should automatically convert images on save...) if Image.getmodebase(im.mode) == "RGB": imOut = im.convert("P") rawmode = "P" else: imOut = im.convert("L") rawmode = "L" # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo) for s in header: fp.write(s) flags = 0 try: interlace = im.encoderinfo["interlace"] except KeyError: interlace = 1 # workaround for @PIL153 if min(im.size) < 16: interlace = 0 if interlace: flags = flags | 64 try: transparency = im.encoderinfo["transparency"] except KeyError: pass else: transparency = int(transparency) # optimize the block away if transparent color is not used transparentColorExists = True # adjust the transparency index after optimize if usedPaletteColors is not None and len(usedPaletteColors) < 256: for i in range(len(usedPaletteColors)): if usedPaletteColors[i] == transparency: transparency = i transparentColorExists = True break else: transparentColorExists = False # transparency extension block if transparentColorExists: fp.write(b"!" + o8(249) + # extension intro o8(4) + # length o8(1) + # transparency info present o16(0) + # duration o8(transparency) # transparency index + o8(0)) # local image header fp.write(b"," + o16(0) + o16(0) + # bounding box o16(im.size[0]) + # size o16(im.size[1]) + o8(flags) + # flags o8(8)) # bits imOut.encoderconfig = (8, interlace) ImageFile._save(imOut, fp, [("gif", (0, 0) + im.size, 0, rawmode)]) fp.write(b"\0") # end of image data fp.write(b";") # end of file try: fp.flush() except: pass
def _save(im, fp, filename, save_all=False): if _imaging_gif: # call external driver try: _imaging_gif.save(im, fp, filename) return except IOError: pass # write uncompressed file if im.mode in RAWMODE: im_out = im.copy() else: im_out = _convert_mode(im) # header try: palette = im.encoderinfo["palette"] except KeyError: palette = None im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) if save_all: previous = None for im_frame in ImageSequence.Iterator(im): im_frame = _convert_mode(im_frame) # To specify duration, add the time in milliseconds to getdata(), # e.g. getdata(im_frame, duration=1000) if not previous: # global header for s in getheader(im_frame, palette, im.encoderinfo)[0] + getdata(im_frame): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im_frame, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im_frame.crop(bbox), offset=bbox[:2]): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im_frame.copy() else: header = getheader(im_out, palette, im.encoderinfo)[0] for s in header: fp.write(s) flags = 0 if get_interlace(im): flags = flags | 64 # local image header _get_local_header(fp, im, (0, 0), flags) 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