Skip to content

Instantly share code, notes, and snippets.

@sarah-j-smith
Last active April 9, 2026 04:04
Show Gist options
  • Select an option

  • Save sarah-j-smith/410d4ebe794beaad45c6bb3c4d396083 to your computer and use it in GitHub Desktop.

Select an option

Save sarah-j-smith/410d4ebe794beaad45c6bb3c4d396083 to your computer and use it in GitHub Desktop.
Script in bash to convert a folder of PNG files to a single animated GIF
#!/bin/bash
# Install gifsicle first via brew:
# brew install gifsicle
# Fail fast when something goes wrong
set -e
# Try to make a new directory to hold gifs
mkdir -p gifs
outputfn="${1:-gifoutput}"
outputcnt=0
echo "Scanning for PNG files to create ${outputfn}"
if [ "${outputfn}x" = "gifoutputx" ]; then
echo "Use $0 output_file_name.gif to customize ${outputfn}"
fi
if [ -d pngs ]; then
for p in $(ls pngs/*); do
filename=$(basename "$p")
extension="${p##*.}"
filename="${filename%.*}"
if [ "${extension}x" = "pngx" ]; then
sips -s format gif $p --out "gifs/${filename}.gif"
else
echo "Ignoring ${filename} with extension ${extension}"
fi
outputfn=$filename
# This line may fail on shells other than bash
outputcnt=$(($outputcnt + 1))
done
if [ $outputcnt -eq 0 ]; then
echo "Could not convert any pngs to gifs"
exit 1;
fi
gifsicle gifs/*.gif --optimize=3 --delay=10 --loop -o "${outputfn}.gif"
# Use quick look to show the resulting gif
qlmanage -p "${outputfn}.gif"
else
echo "Did not find a folder/directory called \"pngs\" - bailing out"
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment