Skip to content

Instantly share code, notes, and snippets.

@kana
Created July 22, 2021 06:31
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 kana/e2d08b8b32d6eeca89ee9b5d8c2c665c to your computer and use it in GitHub Desktop.
Save kana/e2d08b8b32d6eeca89ee9b5d8c2c665c to your computer and use it in GitHub Desktop.
Generate "galaxy" gif for Slack emoji.
#!/bin/bash
set -euo pipefail
trap "rm -f ,$$,*" EXIT
border=3
colors=256
delay=8
frames=24
rainbow=3
while (( $# > 0 ))
do
case "$1" in
-b)
border="$2"
shift 2
;;
-c)
colors="$2"
shift 2
;;
-d)
delay="$2"
shift 2
;;
-f)
frames="$2"
shift 2
;;
-r)
rainbow="$2"
shift 2
;;
*)
break
;;
esac
done
if (( "$#" != 1 ))
then
echo <<__END__
Usage: $0 [OPTIONS] FILE
FILE must have alpha channel, and its demension must be 128x128.
-b N Increase the width of border added to FILE image.
-c N Set the preferred number of colors in the resulting image.
-d N Pause (N / 100) seconds before displaying the next frame.
-f N Generate N frames for the background animation.
-r N Generate N rainbow loops for the background.
__END__
exit 1
fi
infile="$1"
convert "$infile" -background None -gravity Center -extent 128x128 \
\( +clone -alpha Extract \
-morphology Dilate "Disk:$border" -morphology Close Octagon \
-threshold 50% \
+duplicate \
-compose CopyAlpha -composite \) \
+swap \
-compose SrcOver -composite \
",$$,infile-bordered.png"
convert -size 181x181 radial-gradient: -gravity Center -extent 128x128 +repage \
-duplicate "$((rainbow - 1))" \
-compose ModulusAdd -flatten \
",$$,bg-base.png"
for i in $(seq 0 "$((frames - 1))")
do
convert -background Transparent -virtual-pixel Tile \
\( \
",$$,bg-base.png" \
-channel R -fx "(p+${i}/${frames})%1" \
-set colorspace HSB \
-channel GB -evaluate Set 100% \
-colorspace RGB \
\) \
\( \
",$$,infile-bordered.png" \
-colorspace RGB \
\) \
-channel RGB -compose SrcOver -flatten \
-colorspace sRGB \
",$$,f-$(printf '%03d' "$i").png"
done
if (( $colors == 256 ))
then
colors_args=()
else
colors_args=(-colors $colors)
fi
convert ,$$,f-???.png \
+repage +dither +map -layers OptimizeTransparency ${colors_args[@]:-} \
-set delay $delay \
${infile/.*/}-galaxy-b$border-c$colors-d$delay-f$frames-r$rainbow.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment