def renderToString(f, points): orig = Image.open(f).convert("RGB") images = [im.convert("P") for im in gif(orig, 15, points)] durations = [.1 for image in images] loops = 0 # forever xys = [(0, 0) for image in images] disposes = [1 for image in images] gw = GifWriter() fp = StringIO() gw.writeGifToFile(fp, images, durations, loops, xys, disposes) bytes = fp.getvalue() fp.close() return bytes
FRAMECOUNT = 0 LINE_SPACING = 14 MARGIN = 2 FONT = ImageFont.truetype(os.path.join(BASE_PATH, "anony.ttf"), 12) IMAGE_DEFS = { 'sig.gif': (600, 120), 'site.gif': (600, 400), } DEFAULT_IMG_NAME = 'sig.gif' BUFFER_MAX = 50 output_buffer = ["" for _ in range(BUFFER_MAX)] gifWriter = GifWriter() gifWriter.transparency = False streams = {} LAST_FRAME = {} HEADER_DATA = {} for img_name in IMAGE_DEFS.keys(): streams[img_name] = set() LAST_FRAME[img_name] = None HEADER_DATA[img_name] = None def get_header_data(width, height): rio = BytesIO() img, palette = gen_img(width, height)
IRC_NETWORK = "irc.synirc.net" BIND_ADDR = "0.0.0.0" BIND_PORT = 80 TIME_STEP = 4 MAX_TIME = 60 LINE_SPACING = 14 MARGIN = 2 FONT = ImageFont.truetype(os.path.join(BASE_PATH, "anony.ttf"), 12) streams = set() output_buffer = deque(["" for _ in range(6)]) gifWriter = GifWriter() gifWriter.transparency = False def get_header_data(): rio = BytesIO() img,palette = gen_img() header = gifWriter.getheaderAnim(img) appext = gifWriter.getAppExt(1) #num loops rio.write(header) rio.write(palette) rio.write(appext) return rio.getvalue() def new_msg(msg):
def writeGifFp(FP, images, duration=0.1, repeat=True, dither=False, nq=0, subRectangles=True, dispose=None): """ writeGif(filename, images, duration=0.1, repeat=True, dither=False, nq=0, subRectangles=True, dispose=None) Write an animated gif from the specified images. Parameters ---------- FP : the file pointer - images : list Should be a list consisting of PIL images or numpy arrays. The latter should be between 0 and 255 for integer types, and between 0 and 1 for float types. duration : scalar or list of scalars The duration for all frames, or (if a list) for each frame. repeat : bool or integer The amount of loops. If True, loops infinitetely. dither : bool Whether to apply dithering nq : integer If nonzero, applies the NeuQuant quantization algorithm to create the color palette. This algorithm is superior, but slower than the standard PIL algorithm. The value of nq is the quality parameter. 1 represents the best quality. 10 is in general a good tradeoff between quality and speed. When using this option, better results are usually obtained when subRectangles is False. subRectangles : False, True, or a list of 2-element tuples Whether to use sub-rectangles. If True, the minimal rectangle that is required to update each frame is automatically detected. This can give significant reductions in file size, particularly if only a part of the image changes. One can also give a list of x-y coordinates if you want to do the cropping yourself. The default is True. dispose : int How to dispose each frame. 1 means that each frame is to be left in place. 2 means the background color should be restored after each frame. 3 means the decoder should restore the previous frame. If subRectangles==False, the default is 2, otherwise it is 1. """ # Check PIL if PIL is None: raise RuntimeError("Need PIL to write animated gif files.") # Check images images = checkImages(images) # Instantiate writer object gifWriter = GifWriter() # Check loops if repeat is False: loops = 1 elif repeat is True: loops = 0 # zero means infinite else: loops = int(repeat) # Check duration if hasattr(duration, '__len__'): if len(duration) == len(images): duration = [d for d in duration] else: raise ValueError("len(duration) doesn't match amount of images.") else: duration = [duration for im in images] # Check subrectangles if subRectangles: images, xy = gifWriter.handleSubRectangles(images, subRectangles) defaultDispose = 1 # Leave image in place else: # Normal mode xy = [(0,0) for im in images] defaultDispose = 2 # Restore to background color. # Check dispose if dispose is None: dispose = defaultDispose if hasattr(dispose, '__len__'): if len(dispose) != len(images): raise ValueError("len(xy) doesn't match amount of images.") else: dispose = [dispose for im in images] # Make images in a format that we can write easy images = gifWriter.convertImagesToPIL(images, dither, nq) # Write gifWriter.writeGifToFile(FP, images, duration, loops, xy, dispose)