Last active
April 9, 2026 04:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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