Skip to content

Instantly share code, notes, and snippets.

@adeak

adeak/gifify.py Secret

Created July 20, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeak/8e01334443d632f6009fb6dcfd7db8cb to your computer and use it in GitHub Desktop.
Save adeak/8e01334443d632f6009fb6dcfd7db8cb to your computer and use it in GitHub Desktop.
command-line gif creation with GIMP (proof of concept)
from __future__ import print_function
import itertools
import glob
from gimpfu import pdb # (although already defined when called from gimp)
def gifify(*args):
"""Load a list of sorted glob patterns of images and create a gif called out.gif"""
file_iter = itertools.chain.from_iterable(sorted(glob.glob(arg)) for arg in args)
# load first image, add the rest as layers
first = next(file_iter)
img = pdb.gimp_file_load(first, first)
for infile in file_iter:
lay = pdb.gimp_file_load_layer(img, infile)
# we also have to explicitly attach the new layer to the image
pdb.gimp_image_insert_layer(img, lay, None, -1)
# convert image to indexed (either that or grayscale needed for gif)
pdb.gimp_image_convert_indexed(
img, # image
0, # dither (no dither)
0, # palette (make one)
255, # num_colors
0, # alpha_dither (no alpha dither for fake partial opacity)
0, # remove_unused (don't remove unused or duplicate colors)
"" # palette (unused)
)
# save as a looping gif with some parameters
outfile = "out.gif"
pdb.file_gif_save(
img, # image
img.active_drawable, # drawable (active layer, really)
outfile, # filename
outfile, # raw_filename
0, # interlace (don't)
1, # loop (do)
1000, # delay (in milliseconds)
2 # dispose (disposal strategy: replace)
)
print(outfile, "done.")
pdb.gimp_image_delete(img)
# hack: need to add . to path :'(
PYTHONPATH='.':$PYTHONPATH gimp-console --batch-interpreter python-fu-eval -b 'import gifify; gifify.gifify("pics/img*.jpg"); pdb.gimp_quit(0)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment