Ejemplo n.º 1
0
def resize_gif(im: GifImageFile, size: int = DEFAULT_EMOJI_SIZE) -> bytes:
    frames = []
    duration_info = []
    disposals = []
    # If 'loop' info is not set then loop for infinite number of times.
    loop = im.info.get("loop", 0)
    for frame_num in range(0, im.n_frames):
        im.seek(frame_num)
        new_frame = im.copy()
        new_frame.paste(im, (0, 0), im.convert("RGBA"))
        new_frame = ImageOps.pad(new_frame, (size, size), Image.ANTIALIAS)
        frames.append(new_frame)
        duration_info.append(im.info["duration"])
        disposals.append(im.disposal_method)
    out = io.BytesIO()
    frames[0].save(
        out,
        save_all=True,
        optimize=False,
        format="GIF",
        append_images=frames[1:],
        duration=duration_info,
        disposal=disposals,
        loop=loop,
    )
    return out.getvalue()
Ejemplo n.º 2
0
def resize_gif(im: GifImageFile, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
    frames = []
    duration_info = []
    # If 'loop' info is not set then loop for infinite number of times.
    loop = im.info.get("loop", 0)
    for frame_num in range(0, im.n_frames):
        im.seek(frame_num)
        new_frame = Image.new("RGBA", im.size)
        new_frame.paste(im, (0, 0), im.convert("RGBA"))
        new_frame = ImageOps.fit(new_frame, (size, size), Image.ANTIALIAS)
        frames.append(new_frame)
        duration_info.append(im.info['duration'])
    out = io.BytesIO()
    frames[0].save(out, save_all=True, optimize=True,
                   format="GIF", append_images=frames[1:],
                   duration=duration_info,
                   loop=loop)
    return out.getvalue()
def convert_frame_to_transparent(imageObject: GifImageFile) -> Image:
    """
    https://stackoverflow.com/questions/3752476/python-pil-replace-a-single-rgba-color
    replaces #FFFFFF (white) with #00000000 (clear)
    """
    rgbaImage: Image = imageObject.convert('RGBA')

    data = np.array(rgbaImage)  # "data" is a height x width x 4 numpy array
    red, green, blue, alpha = data.T  # Temporarily unpack the bands for readability

    # Replace white with clear... (leaves alpha values alone...)
    white_areas = (red == 255) & (blue == 255) & (green == 255)
    data[...][white_areas.T] = (0, 0, 0, 0)  # Transpose back needed

    return Image.fromarray(data)
Ejemplo n.º 4
0
    def __init__(self, texture, *args, **kwargs):

        super(Gif, self).__init__(texture=texture, *args, **kwargs)

        self.gif = None
        self.gif_index = 0
        self.gif_normal_index = 0.0
        self.gif_changed_time = 0
        self.gif_duration = 0
        self.gif_speed = 1.0
        self.gif_length = 0

        if isinstance(texture, str) and '.gif' in texture:
            gif = GifImageFile(texture)
            if gif.is_animated:
                self.set_frames(gif)
Ejemplo n.º 5
0
def check_with_matches(filename, matches):
    if 'IsGIF' not in matches:
        return None

    try:
        with GifImageFile(filename) as image:
            image.seek(image.n_frames - 1)
            while image.data():  # Pass the last frame
                pass
            level = PolyglotLevel()
            image_end = image.fp.tell()
            if image.fp.read(1) == b';':
                image_end += 1
            image.fp.seek(0, io.SEEK_END)
            image_size = image.fp.tell()
            if image_end != image_size:
                level.add_chunk(image_end, image_size - image_end)
            return level
    except SyntaxError:
        return None