Skip to content

Instantly share code, notes, and snippets.

@twooster
Forked from jmhobbs/README.md
Last active April 2, 2020 10:06
Show Gist options
  • Save twooster/0e6afe4d21977e4544f86c30a2eb03ba to your computer and use it in GitHub Desktop.
Save twooster/0e6afe4d21977e4544f86c30a2eb03ba to your computer and use it in GitHub Desktop.
Create scrolling text gifs for Slack

(Forked from yolo.sh that works on mac)

Makes little scrolly text jiffs in Flywheel colors.

Prerequisites

  • imagemagick sudo apt install imagemagick
  • gifsicle sudo apt install gifsicle
  • u linux

Usage

usage: ./yolo.sh [options] <message>

Options:

  -h                Show this help message

  -o <output.gif>   Path to output gif. Default: output.gif

  -c <color>        Choose color set, valid options are:
                    red, orange, yellow, green, blue, purple,
                    pink, black, white.  Default: blue

  -d <delay>        Delay between frames. Default: 6

  -s <step>         How many pixels to step each frame. Default: 10

  -H <height>       Image height. Default: 128

  -f <font name>    The font name. Default: helvetica

 -w                 Force white text color

Examples

$ yolo.sh -c orange -o gtfo.gif GTFO
#!/usr/bin/env bash
contains() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
usage() {
if [ "$1" != "" ]; then
echo -e "Error: $1\n" >&2
fi
echo "usage: $0 [options] <message>"
echo
echo "Options:"
echo
echo " -h Show this help message"
echo
echo " -o <output.gif> Path to output gif. Default: output.gif"
echo
echo " -c <color> Choose color set, valid options are:"
echo " red, orange, yellow, green, blue, purple,"
echo " pink, black, white. Default: blue"
echo
echo " -d <delay> Delay between frames. Default: 6"
echo
echo " -s <step> How many pixels to step each frame. Default: 10"
echo
echo " -H <height> Image height. Default: 128"
echo
echo " -f <font name> The font name. Default: helvetica"
echo
echo " -w Force white text color"
exit 1
}
VALID_COLORS=("red" "orange" "yellow" "green" "blue" "purple" "pink" "black" "white")
COLOR="black"
OUTPUT="output.gif"
FONT_NAME="helvetica"
FORCE_WHITE_TEXT=0
SPEED=16
DELAY=6
HEIGHT=128
while getopts ":c:o:f:s:d:H:hw" opt; do
case $opt in
h)
usage
;;
w)
FORCE_WHITE_TEXT=1
;;
c)
if contains "$OPTARG" "${VALID_COLORS[@]}"; then
COLOR="$OPTARG"
else
usage "Invalid color: $OPTARG"
fi
;;
o)
OUTPUT="$OPTARG"
;;
H)
HEIGHT="$OPTARG"
;;
f)
FONT_NAME="$OPTARG"
;;
d)
DELAY="$OPTARG"
;;
s)
SPEED="$OPTARG"
;;
\?)
usage "Invalid option: -$OPTARG"
;;
:)
usage "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Find our font before we do anything.
FONT_PATH="$( fc-match "${FONT_NAME}" -f'%{file}' )"
if [ "$FONT_PATH" == "" ]; then
usage "Could not find '${FONT_NAME}' font. Is it installed?"
else
echo "Using ${FONT_PATH} for ${FONT_NAME} font."
fi
MESSAGE="$*"
if [ "$MESSAGE" == "" ]; then
usage "A message is required."
fi
case $COLOR in
red)
BACKGROUND="#ef4e65"
FILL="#8c2738"
;;
orange)
BACKGROUND="#f47820"
FILL="#8e4402"
;;
yellow)
BACKGROUND="#f0ce15"
FILL="#947c00"
;;
green)
BACKGROUND="#51bb7b"
FILL="#267048"
;;
blue)
BACKGROUND="#50c6db"
FILL="#01516e"
;;
purple)
BACKGROUND="#8351a0"
FILL="#4e2760"
;;
pink)
BACKGROUND="#e0368c"
FILL="#851252"
;;
black)
BACKGROUND="#ffffff"
FILL="#111111"
;;
white)
BACKGROUND="#ffffff"
FILL="#000000"
;;
esac
if [ "$FORCE_WHITE_TEXT" == 1 ]; then
FILL="#ffffff"
fi
# Make a "unique" prefix for this run
PREFIX="$(head -c 32 /dev/urandom | shasum | cut -b 1-10)"
# Generate image from text input
convert -background "$BACKGROUND" -fill "$FILL" -font "$FONT_PATH" -density 200 -pointsize 100 "label:${MESSAGE}" "/tmp/${PREFIX}_label.png"
# Resize to 128px high
convert -resize x${HEIGHT} "/tmp/${PREFIX}_label.png" "/tmp/${PREFIX}_sized.png"
# Add white space to front and back for empty frames
WIDTH="$(identify -format "%[fx:w]" "/tmp/${PREFIX}_sized.png")"
CANVAS_SIZE=$((WIDTH + HEIGHT + HEIGHT + (HEIGHT / 4))) # HEIGHT in front, HEIGHT / 4 in back
convert -size ${CANVAS_SIZE}x${HEIGHT} "xc:$BACKGROUND" "/tmp/${PREFIX}_canvas.png"
convert "/tmp/${PREFIX}_canvas.png" "/tmp/${PREFIX}_sized.png" -geometry +"${HEIGHT}"+0 -composite "/tmp/${PREFIX}_padded.png"
# Generate individual frames
STEP=$((HEIGHT / (64 / SPEED)))
OFFSET=0
I=0
LIMIT=$((CANVAS_SIZE - HEIGHT))
while [[ "$OFFSET" -lt "$LIMIT" ]]; do
convert "/tmp/${PREFIX}_padded.png" -crop "${HEIGHT}x${HEIGHT}+${OFFSET}+0!" "$(printf "/tmp/${PREFIX}_frame_%05d.png" $I)"
I=$((I + 1))
OFFSET=$((OFFSET + STEP))
done
# Compile to gif
convert -delay "$DELAY" -loop 0 "/tmp/${PREFIX}_frame_*.png" "$OUTPUT"
# Smush it
gifsicle -bO "$OUTPUT"
# Clean up!
rm /tmp/${PREFIX}_*.png
@asaaki
Copy link

asaaki commented Apr 2, 2020

For macos users with homebrew:

brew install fontconfig imagemagick gifsicle

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