Skip to content

Instantly share code, notes, and snippets.

Last active Feb 12, 2021
What would you like to do?
#!/usr/bin/env python3
import PIL.Image
from PIL import ImageSequence
import wand.image
from io import BytesIO, RawIOBase
def gmagik(gif: PIL.Image.Image, out: RawIOBase):
total = gif.n_frames
out_frames = []
for idx, frame in enumerate(ImageSequence.Iterator(gif), start=1):
print(f"Processing frame {idx}/{total}")
b = BytesIO()
magik(frame, b)
print(f"Done processing {total} frames")
out_frames[0].save(out, "GIF", loop=0, save_all=True, append_images=out_frames[1:])
def magik(im: PIL.Image.Image, out: RawIOBase):
b = BytesIO(), "gif")
im = wand.image.Image(file=b)
width=int(im.width * 0.5), height=int(im.height * 0.5), delta_x=1, rigidity=0
width=int(im.width * 1.5), height=int(im.height * 1.5), delta_x=2, rigidity=0
im.resize(im.width, im.height)
if __name__ == "__main__":
import argparse, sys
parser = argparse.ArgumentParser("gmagik")
subparsers = parser.add_subparsers(required=True)
for action in ('magik', 'gmagik'):
action_parser = subparsers.add_parser(action)
action_parser.add_argument("input", nargs=1)
action_parser.add_argument("output", nargs=1)
args = parser.parse_args()
inp = args.input[0]
out = args.output[0]
inp =
with open(out, "wb") as f:
args.func(inp, f)
except Exception as e:
print("an exception occurred:", file=sys.stderr)
Copy link

coolreader18 commented Oct 29, 2019

You need Pillow and Wand/ImageMagick installed, with Liquid Rescale enabled for ImageMagick (you have to recompile ImageMagick to get it, if it's not there already). Then, you can run:

$ ./ gmagik input.gif gmagicked-output.gif
# or for magik on a single-frame image
$ ./ magik input.png gmagicked-output.gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment