def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None for im in sequence: # # FIXME: write graphics control block before each frame if not previous: # global header for s in getheader(im)[0]: fp.write(s) for s in getdata(im): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset = bbox[:2]): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im.copy() frames += 1 fp.write(b";") return frames
def _writeGifToFile(fp, images, durations, loops): frames = 0 previous = None for im in images: if not previous: palette = getheader(im)[1] data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) fp.write(header) fp.write(palette) fp.write(appext) fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) previous = im.copy() frames = frames + 1 fp.write(";") return frames
def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None loop_code = '!\xff\x0bNETSCAPE2.0\x03\x01\xff\xff\x00' for im in sequence: if not previous: # global header for s in getheader(im)[0] + [loop_code] + getdata(im): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset=bbox[:2]): fp.write(s) else: # duplicate frame, write anyway for s in getdata(im): fp.write(s) previous = im.copy() frames = frames + 1 fp.write(";") return frames
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream. """ # init frames = 0 previous = None for im in images: if not previous: # first image # gather data palette = getheader(im)[1] data = getdata(im) imdes, data = data[0], data[1:] header = CreateAnimationHeader(im) appext = CreateApplicationExtension(loops) graphext = CreateGraphicsControlExtension(durations[0]) # write global header fp.write(header) fp.write(palette) fp.write(appext) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # gather info (compress difference) data = getdata(im) imdes, data = data[0], data[1:] graphext = CreateGraphicsControlExtension(durations[frames]) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # prepare for next round previous = im.copy() frames = frames + 1 fp.write(";") # end gif return frames
def _write_frame(self, im, duration = None, dispose = None, xy = None): duration = self.duration if duration is None else duration dispose = self.dispose if dispose is None else dispose xy = self.xy if xy is None else xy palette = getheader(im)[0][-1] if not palette: palette = im.palette.tobytes() # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.pointless_instance.getGraphicsControlExt(duration, dispose) # Make image descriptor suitable for using 256 local color palette lid = self.pointless_instance.getImageDescriptor(im, xy) # Write local header if (palette != self.globalPalette) or (dispose != 2): # Use local color palette self.fp.write(encode(graphext)) self.fp.write(encode(lid)) # write suitable image descriptor self.fp.write(palette) # write local color table self.fp.write(encode('\x08')) # LZW minimum size code else: # Use global color palette self.fp.write(encode(graphext)) self.fp.write(imdes) # write suitable image descriptor # Write image data for d in data: self.fp.write(d)
def _write_frame(self, im, duration=None, dispose=None, xy=None): duration = self.duration if duration is None else duration dispose = self.dispose if dispose is None else dispose xy = self.xy if xy is None else xy palette = getheader(im)[0][-1] if not palette: palette = im.palette.tobytes() # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.pointless_instance.getGraphicsControlExt( duration, dispose) # Make image descriptor suitable for using 256 local color palette lid = self.pointless_instance.getImageDescriptor(im, xy) # Write local header if (palette != self.globalPalette) or (dispose != 2): # Use local color palette self.fp.write(encode(graphext)) self.fp.write(encode(lid)) # write suitable image descriptor self.fp.write(palette) # write local color table self.fp.write(encode('\x08')) # LZW minimum size code else: # Use global color palette self.fp.write(encode(graphext)) self.fp.write(imdes) # write suitable image descriptor # Write image data for d in data: self.fp.write(d)
def _writeGifToFile(fp, images, durations, loops): """ 把一系列图像转换为字节并存入文件流中 """ # 初始化 frames = 0 previous = None for im in images: if not previous: # 第一个图像 # 获取相关数据 palette = getheader(im)[0][3] #取第一个图像的调色板 # palette = Image.ADAPTIVE data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # 写入全局头 fp.write(header) fp.write(palette) fp.write(appext) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # 获取相关数据 data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # 准备下一个回合 previous = im.copy() frames = frames + 1 fp.write(";") # 写入完成 return frames
def _writeGifToFile(fp, images, durations, loops): """ 把一系列图像转换为字节并存入文件流中 """ # 初始化 frames = 0 previous = None for im in images: if not previous: # 第一个图像 # 获取相关数据 palette = getheader(im)[1] #取第一个图像的调色板 data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # 写入全局头 fp.write(header) fp.write(palette) fp.write(appext) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # 获取相关数据 data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # 写入图像 fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # 准备下一个回合 previous = im.copy() frames = frames + 1 fp.write(";") # 写入完成 return frames
def write_gif_to_file(fp, images, durations, loops): """ 把一系列图像转换为字节并存入文件流中 """ # 初始化 frames = 0 previous = None for im in images: if not previous: # 第一个图像 # 获取相关数据 palette = getheader(im)[1] # 取第一个图像的调色板 data = getdata(im) im_des, data = data[0], data[1:] header = get_header_animat(im) app_ext = get_app_ext(loops) graph_ext = get_graphics_control_ext(durations[0]) # 写入全局头 fp.write(header) fp.write(palette) fp.write(app_ext) # 写入图像 fp.write(graph_ext) fp.write(im_des) for d in data: fp.write(d) else: # 获取相关数据 data = getdata(im) im_des, data = data[0], data[1:] graph_ext = get_graphics_control_ext(durations[frames]) # 写入图像 fp.write(graph_ext) fp.write(im_des) for d in data: fp.write(d) # 准备下一个回合 previous = im.copy() frames += 1 # 写入完成 fp.write(";") return frames
def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None for im in sequence: # To specify duration, add the time in milliseconds to getdata(), # e.g. getdata(im, duration=1000) if not previous: # global header loops = 2**16 - 1 for s in getheader(im, info={"loop": loops})[0] + getdata( im, duration=10, loop=2**16 - 1): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset=bbox[:2], duration=10): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im.copy() frames += 1 fp.write(b";") return frames
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream. """ # init frames = 0 previous = False for im in images: if not previous: palette = getheader(im)[1] data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # write global header fp.write(header) fp.write(palette) fp.write(appext) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # gather info (compress difference) data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) previous = im.copy() frames = frames + 1 fp.write(";") # end gif return frames
def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None for im in sequence: # To specify duration, add the time in milliseconds to getdata(), # e.g. getdata(im, duration=1000) if not previous: # global header loops = 2 ** 16 - 1 for s in getheader(im, info={"loop": loops})[0] + getdata(im, duration=10, loop=2 ** 16 - 1): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset=bbox[:2], duration=10): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im.copy() frames += 1 fp.write(b";") return frames
def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None for im in sequence: # # FIXME: write graphics control block before each frame if not previous: # global header for s in getheader(im) + getdata(im): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset=bbox[:2]): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im.copy() frames = frames + 1 fp.write(";") return frames
def _writeGifToFile(fp, images, durations, loops): # init frames = 0 previous = None for im in images: if not previous: # first frame palette = getheader(im)[1] data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # write global header fp.write(header) fp.write(palette) fp.write(appext) # write images fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # write images fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # for next loop previous = im.copy() frames += 1 fp.write(";") # done return frames
def _writeGifToFile(fp, images, durations, loops): frames = 0 previous = None for im in images: # # FIXME: write graphics control block before each frame if not previous: # global header for s in getheader(im)[0] + getdata(im): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for s in getdata(im.crop(bbox), offset=bbox[:2]): fp.write(s) else: # FIXME: what should we do in this case? pass previous = im.copy() frames = frames + 1 fp.write(";") return frames
def makedelta(fp, sequence): """Convert list of image frames to a GIF animation file""" frames = 0 previous = None for im in sequence: # # FIXME: write graphics control block before each frame if not previous: for data in filter(None, getheader(im)): if isinstance(data, list): for d in data: fp.write(d) else: fp.write(data) # global header for data in getdata(im): fp.write(data) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() if bbox: # compress difference for data in getdata(im.crop(bbox), offset = bbox[:2]): if isinstance(data, bytes): fp.write(data) else: import pdb; pdb.set_trace() fp.write(bytes(data)) else: # FIXME: what should we do in this case? pass previous = im.copy() frames = frames + 1 fp.write(b';') return frames
def makeAnimatedGIF(filename, images): """Convert list of image frames to a GIF animation file using simple delta coding """ frames = 0 previous = None fp = open(filename, 'wb') if images[0].mode in ('RGB', 'RGBA'): # first make an optimised palette optimPalette = makePalette(images, verbose=True) for n, im in enumerate(images): print('converting frame %i of %i to GIF' % (n + 1, len(images))) if im.mode == 'RGB': im = rgb2palette(im, palette=optimPalette, verbose=False) if not previous: # global header for s in getheader(im) + getdata(im): fp.write(s) else: # delta frame delta = ImageChops.subtract_modulo(im, previous) bbox = delta.getbbox() # compress difference if bbox: for s in getdata(im.crop(bbox), offset=bbox[:2]): fp.write(s) else: for s in getdata(im): fp.write(s) previous = im.copy() frames += 1 fp.write(";") fp.close() return frames
def _write_gif_to_file(fp, images, durations, loops): palettes, occur = [], [] for im in images: palettes.append(im.palette.getdata()[1]) for palette in palettes: occur.append(palettes.count(palette)) global_palette = palettes[occur.index(max(occur))] frame = 0 first_frame = True for im, palette in zip(images, palettes): if first_frame: header = _get_header(im) appext = _get_app_ext(loops) fp.write(header) fp.write(global_palette) fp.write(appext) first_frame = False if True: data = getdata(im) imdes, data = data[0], data[1:] graphext = _get_graph_ctrl_ext(durations[frame]) lid = _get_image_des(im) if palette != global_palette: fp.write(graphext) fp.write(lid) fp.write(palette) fp.write('\x08') else: fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) frame += 1 fp.write(";") return frame
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream.""" # first image, gather data for global header img = images[0] header = getheaderAnim(img) palette = getheader(img)[1] appext = getAppExt(loops) # output the initial header fp.write(header) fp.write(palette) fp.write(appext) # write sequence of images for img, duration in zip(images, durations): graphext = getGraphicsControlExt(duration) fp.write(graphext) for dat in getdata(img): fp.write(dat) fp.write(';') # end gif marker
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palette = getheader(im)[1] if not palette: palette = PIL.ImagePalette.ImageColor if isinstance(palette, type(os)): # Older or newer? version of Pil(low) data = PIL.ImagePalette.ImagePalette().getdata() palette = data[0].encode('utf-8') + data[1] # Arg this does not work. Go use imageio raise RuntimeError('Cannot get palette. ' 'Maybe you should try imageio instead.') palettes.append(palette) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[ occur.index(max(occur)) ] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header)#.encode('utf-8')) fp.write(globalPalette) fp.write(appext)#.encode('utf-8')) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext)#.encode('utf-8')) fp.write(lid)#.encode('utf-8')) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08'.encode('utf-8')) # LZW minimum size code else: # Use global color palette fp.write(graphext.encode('utf-8')) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";".encode('utf-8')) # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: # http://stackoverflow.com/questions/19149643/error-in-images2gif-py-with-globalpalette palettes.append( im.palette.getdata()[1] ) for palette in palettes: occur.append( palettes.count( palette ) ) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[ occur.index(max(occur)) ] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info # FIXME: this line breaks: # ImageFile._save(im, fp, [("gif", (0, 0)+im.size, 0, RAWMODE[im.mode])]) # KeyError: 'RGBA' data = getdata(im) imdes, data = data[0], data[1:] transparent_flag = 0 if self.transparency: transparent_flag = 1 graphext = self.getGraphicsControlExt(durations[frames], disposes[frames],transparent_flag=transparent_flag,transparency_index=255) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write(b'\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(b";") # end gif return frames
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream. """ # init frames = 0 previous = None for im in images: if not previous: # first image # gather data palette = getheader(im)[1] data = getdata(im) imdes, data = data[0], data[1:] header = getheaderAnim(im) appext = getAppExt(loops) graphext = getGraphicsControlExt(durations[0]) # write global header fp.write(header) fp.write(palette) fp.write(appext) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) else: # gather info (compress difference) data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # write image fp.write(graphext) fp.write(imdes) for d in data: fp.write(d) # # delta frame - does not seem to work # delta = ImageChops.subtract_modulo(im, previous) # bbox = delta.getbbox() # # if bbox: # # # gather info (compress difference) # data = getdata(im.crop(bbox), offset = bbox[:2]) # imdes, data = data[0], data[1:] # graphext = getGraphicsControlExt(durations[frames]) # # # write image # fp.write(graphext) # fp.write(imdes) # for d in data: # fp.write(d) # # else: # # FIXME: what should we do in this case? # pass # prepare for next round previous = im.copy() frames = frames + 1 fp.write(";") # end gif return frames
def loadVideo(): """filename = loadVideoHelper() cap = cv2.VideoCapture(filename[0]) i=0 frames = cap.get(7) vidHeight = cap.get(4) vidWidth = cap.get(3) firstFrame = True palettes, occur = [], [] while(i < 20): ret, frame = cap.read() testimg = frame if firstFrame: firstFrame = False im = Image.fromarray(np.roll(testimg[...,[1,0,2]], 1, axis=-1)) im2= im.convert("P") text22 =im2.im.getpalette("RGB")[:768] palettes.append(text22) for palette in palettes: occur.append( palettes.count( palette ) ) i = i + 4 header1 = gifHeader() colorTable = palettes[occur.index(max(occur))] graphics = gifGraphicsControl() descrip = gifImageDescrip() header2 = gifAnimation() data = getdata(im2) fp = open("fp.gif",'wb') fp.write(header1) fp.write(colorTable) fp.write(graphics) fp.write(descrip) fp.write(header2) for s in data: fp.write(s) i = i + 4""" filename = loadVideoHelper() cap = cv2.VideoCapture(filename[0]) i=0 frames = cap.get(7) vidHeight = cap.get(4) vidWidth = cap.get(3) firstFrame = True while(i < frames): ret, frame = cap.read() testimg = frame if firstFrame: firstFrame = False im = Image.fromarray(np.roll(testimg[...,[1,0,2]], 1, axis=-1)) im2= im.convert("P") first_frame = getheader(im2)[0] first_frame += getdata(im2, duration=0.1) i = i + 4 im = Image.fromarray(np.roll(testimg[...,[1,0,2]], 1, axis=-1)) im3 = im.convert("P") if i == 4: frametxt = getdata(im3, duration=0.1) frametxt += getdata(im3, duration=0.1) i = i + 4 fp = open("fp.gif",'wb') for s in first_frame: fp.write(s) for x in frametxt: fp.write(x) #fp.write(b"\0") #fp.write(b";") fp.close()
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: # Appengine production p = getheader(im)[1] if not p: # Appengine development p = im.palette.getdata()[1] palettes.append(p) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] transparent_flag = 0 if self.transparency: transparent_flag = 1 graphext = self.getGraphicsControlExt( durations[frames], disposes[frames], transparent_flag=transparent_flag, transparency_index=255) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def write_gif_to_file(self, fp, images, durations, loops, xys, disposes): """Given a set of images writes the bytes to the specified stream.""" # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palettes.append(getheader(im)[0][-1]) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) global_palette = palettes[occur.index(max(occur))] # Init frames = 0 first_frame = True for im, palette in zip(images, palettes): if first_frame: # Write header # Gather info header = self.get_header_anim(im) appext = self.get_application_ext(loops) # Write fp.write(header) fp.write(global_palette) fp.write(appext) # Next frame is not the first first_frame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] transparent_flag = 1 if self.transparency else 0 graphext = self.get_graphics_control_ext( durations[frames], disposes[frames], transparent_flag=transparent_flag, transparency_index=255) # Make image descriptor suitable for using 256 local color palette lid = self.get_image_descriptor(im, xys[frames]) # Write local header if (palette != global_palette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: # FIX: Line changed thanks to StackOverflow: http://stackoverflow.com/questions/19149643/error-in-images2gif-py-with-globalpalette palettes.append( im.palette.getdata()[1]) #getheader(im)[1] ) for palette in palettes: occur.append( palettes.count( palette ) ) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[ occur.index(max(occur)) ] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = getheaderAnim(im) appext = getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # Make image descriptor suitable for using 256 local color palette lid = getImageDescriptor(im) # Write local header if palette != globalPalette: # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palette = getheader(im)[1] if not palette: palette = PIL.ImagePalette.ImageColor palettes.append(palette) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info print im.info header = self.getheaderAnim(im) appext = self.getAppExt(loops) print repr(appext) print appext # Write print repr(header) print header header = header[:7] for i in range(len(header)): char = repr(header[i]) print "{} {}".format(i, char) print unicode(header) fp.write(header.encode('utf-8')) #fp.write(globalPalette) fp.write(appext.encode('utf-8')) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext.encode('utf-8')) fp.write( lid.encode('utf-8')) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08'.encode('utf-8')) # LZW minimum size code else: # Use global color palette fp.write(graphext.encode('utf-8')) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";".encode('utf-8')) # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palette = getheader(im)[1] if not palette: palette = PIL.ImagePalette.ImageColor if isinstance(palette, type(os)): # Older or newer? version of Pil(low) data = PIL.ImagePalette.ImagePalette().getdata() palette = data[0].encode('utf-8') + data[1] # Arg this does not work. Go use imageio raise RuntimeError('Cannot get palette. ' 'Maybe you should try imageio instead.') palettes.append(palette) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[ occur.index(max(occur)) ] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header.encode('utf-8')) fp.write(globalPalette) fp.write(appext.encode('utf-8')) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext.encode('utf-8')) fp.write(lid.encode('utf-8')) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08'.encode('utf-8')) # LZW minimum size code else: # Use global color palette fp.write(graphext.encode('utf-8')) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";".encode('utf-8')) # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. Requires different handling of palette for PIL and Pillow: based on https://github.com/rec/echomesh/blob/master/ code/python/external/images2gif.py """ # Obtain palette for all images and count each occurrence palettes, occur = [], [] for im in images: if not pillow: palette = getheader(im)[1] else: palette = getheader(im)[0][-1] if not palette: palette = im.palette.tobytes() palettes.append(palette) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palettes.append( im.palette.getdata()[1] ) for palette in palettes: occur.append( palettes.count( palette ) ) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[ occur.index(max(occur)) ] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def _writeGifToFile(fp, images, durations, loops): """ Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palettes.append(im.palette.getdata()[1]) # palettes.append( getheader(im)[1] ) # following suggestion at http://stackoverflow.com/questions/19149643/error-in-images2gif-py-with-globalpalette for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = getheaderAnim(im) appext = getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = getGraphicsControlExt(durations[frames]) # Make image descriptor suitable for using 256 local color palette lid = getImageDescriptor(im) # Write local header if palette != globalPalette: # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def _writeGifToFile(fp, images, durations, loops): """ 把一系列图像转换为字节并存入文件流中 """ # 初始化 # frames = 0 # previous = None # print durations # for im in images: # for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im,duration=0.05,loop=loops): # fp.write(s) # # 准备下一个回合 # previous = im.copy() # frames = frames + 1 # fp.write(";") # 写入完成 # return frames """ 把一系列图像转换为字节并存入文件流中 """ """ Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: # print getheader(im) palettes.append(getheader(im)[0]) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) # globalPalette = palettes[ occur.index(max(occur)) ] globalPalette = palettes[0] # print globalPalette # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): print im.mode im2 = im.copy() im2.save('ff%s.png' % frames) data = getdata(im, durations=5, loop=loops) if firstFrame: # Write header # Gather info # header = getheaderAnim(im) # appext = getAppExt(loops) # Write # fp.write(header) # fp.write(globalPalette) for d in globalPalette: fp.write(d) # fp.write(b'%d'%d) # fp.write(appext) # Next frame is not the first firstFrame = False else: # Write palette and image data # Gather info # imdes, data = data[0], data[1:] # graphext = getGraphicsControlExt(durations[frames]) # Make image descriptor suitable for using 256 local color palette # lid = getImageDescriptor(im) # Write local header # if palette != globalPalette: # Use local color palette # fp.write(graphext) # fp.write(lid) # write suitable image descriptor # fp.write(palette) # for d in palette: # fp.write(d) # write local color table # fp.write(b'%d'%d) # fp.write('\x08') # LZW minimum size code # fp.write(imdes) # LZW minimum size code # else: # Use global color palette # fp.write(graphext) # fp.write(imdes) # write suitable image descriptor # pass # Write image data pass for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(b";") # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: header, usedPaletteColors = getheader(im) palettes.append( header[-1]) # Last part of the header is the frame palette for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(header) fp.write(globalPalette) fp.write(appext) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = b''.join(data[:-2]), data[-2:] graphext = self.getGraphicsControlExt(durations[frames], disposes[frames]) # Make image descriptor suitable for using 256 local color palette lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(graphext) fp.write(lid) # write suitable image descriptor fp.write(palette) # write local color table fp.write('\x08') # LZW minimum size code else: # Use global color palette fp.write(graphext) fp.write(imdes) # write suitable image descriptor for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(";") # end gif return frames
def writeGifToFile(self, fp, images, durations, loops, xys, disposes): """ writeGifToFile(fp, images, durations, loops, xys, disposes) Given a set of images writes the bytes to the specified stream. """ # Obtain palette for all images and count each occurance palettes, occur = [], [] for im in images: palette = getheader(im)[0][-1] if not palette: palette = im.palette.tobytes() palettes.append(palette) for palette in palettes: occur.append(palettes.count(palette)) # Select most-used palette as the global one (or first in case no max) globalPalette = palettes[occur.index(max(occur))] # Init frames = 0 firstFrame = True for im, palette in zip(images, palettes): if firstFrame: # Write header # Gather info header = self.getheaderAnim(im) appext = self.getAppExt(loops) # Write fp.write(encode(header)) fp.write(globalPalette) fp.write(encode(appext)) # Next frame is not the first firstFrame = False if True: # Write palette and image data # Gather info data = getdata(im) imdes, data = data[0], data[1:] graphext = self.getGraphicsControlExt( durations[frames], disposes[frames] ) lid = self.getImageDescriptor(im, xys[frames]) # Write local header if (palette != globalPalette) or (disposes[frames] != 2): # Use local color palette fp.write(encode(graphext)) fp.write(encode(lid)) fp.write(palette) fp.write(encode('\x08')) else: # Use global color palette fp.write(encode(graphext)) fp.write(imdes) # Write image data for d in data: fp.write(d) # Prepare for next round frames = frames + 1 fp.write(encode(";")) return frames