Last active
September 27, 2021 21:01
-
-
Save trepmal/15df60c90ebb18f59a77a782aa768a9f to your computer and use it in GitHub Desktop.
intensify your emojis
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 | |
# | |
# Call this script, passing an image filename | |
# Creates an intensified version | |
convert='/usr/local/bin/convert' | |
realpath='/usr/local/bin/realpath' | |
# flight check: must have imagemagick | |
if [ ! $(which $convert) ]; then | |
echo 'Error: `convert` required. Available via homebrew: `brew install imagemagick`.' | |
exit; | |
fi; | |
# flight check: must have realpath | |
if [ ! $(which $realpath) ]; then | |
echo 'Error: Cannot find realpath. Available via homebrew: `brew install coreutils`' | |
exit; | |
fi; | |
# flight check: must pass image | |
if [ $# -eq 0 ]; then | |
echo "Usage:" | |
echo "bash ${0} <filename>" | |
exit; | |
fi | |
delay=3 | |
while [ ! $# -eq 0 ] | |
do | |
case "$1" in | |
--help | -h) | |
echo "Usage:" | |
echo "bash ${0} <filename>" | |
echo "--help | -h Show this" | |
echo "--debug Print some extra info" | |
echo "--less Lessen the shakyness (more delay)" | |
echo "--alt Alternate style (better for filled backgrounds)" | |
exit; | |
;; | |
--debug) | |
shift | |
debug=true | |
;; | |
--less) | |
shift | |
delay=100 | |
;; | |
--alt) | |
shift | |
alt=true | |
;; | |
*) | |
fileinput=$1 | |
shift | |
;; | |
esac | |
done | |
fullpath=$($realpath "$fileinput") | |
filename=$(basename "$fullpath") | |
dirname=$(dirname "$fullpath") | |
plainname=${filename%.*} | |
extension=${filename#*.} | |
# verify input image | |
if [ ! -f "$fullpath" ]; then | |
echo 'Error: input not valid' | |
exit; | |
fi; | |
if [ ! -z $debug ]; then | |
echo "What are we working with:" | |
echo 'fileinput: ' $fileinput | |
echo 'fullpath: ' $fullpath | |
echo 'filename: ' $filename | |
echo 'dirname: ' $dirname | |
echo 'plainname: ' $plainname | |
echo 'extension: ' $extension | |
echo | |
fi | |
# force use of png to ensure transparency | |
baseframe="${dirname}/${plainname}-base.png" | |
frame1="${dirname}/${plainname}-frame1.png" | |
frame2="${dirname}/${plainname}-frame2.png" | |
frame3="${dirname}/${plainname}-frame3.png" | |
frame4="${dirname}/${plainname}-frame4.png" | |
suffix='intensifies' | |
if [[ delay -gt 3 ]]; then | |
suffix='less-intense' | |
fi | |
if [ ! -z $alt ]; then | |
suffix='alt-intense' | |
fi | |
gif="${dirname}/${plainname}-${suffix}.gif" | |
if [ ! -z $debug ]; then | |
echo "What are we making:" | |
echo 'baseframe: ' $baseframe | |
echo 'frame1: ' $frame1 | |
echo 'frame2: ' $frame2 | |
echo 'frame3: ' $frame3 | |
echo 'frame4: ' $frame4 | |
echo 'gif: ' $gif | |
echo | |
fi | |
if [ ! -z $alt ]; then | |
# newimgw=$(expr $imgw + 10) | |
# newimgh=$(expr $imgh + 10) | |
# first, stretch the image a bit, so we have something to crop | |
# convert "$fullpath" -resize '138x138>' -resize '138x138<' "$baseframe" | |
# convert "$fullpath" -resize ${newimgw}x${newimgh} "$baseframe" | |
# actually, stretching seems unnecessary. Perhaps I forgot an edge case? | |
$convert "$fullpath" -resize '138x138>' "$baseframe" | |
dims=$(convert "$baseframe" -print "%wx%h\n" /dev/null) | |
imgw="$(cut -d'x' -f1 <<<$dims)" | |
imgh="$(cut -d'x' -f2 <<<$dims)" | |
newimgw=$(expr $imgw - 10) | |
newimgh=$(expr $imgh - 10) | |
if [ ! -z $debug ]; then | |
echo "Dims math:" | |
echo 'imgw: ' $imgw | |
echo 'imgh: ' $imgh | |
echo 'newimgw: ' $newimgw | |
echo 'newimgh: ' $newimgh | |
echo | |
fi | |
# -crop 128x128+5+5 : In the final step, trim the image down to max 128x128 | |
resize="-crop ${newimgw}x${newimgh}+5+5" | |
else # or | |
# first, add some padding around the image, gives us room to dance | |
$convert "$fullpath" -resize '128x128>' -bordercolor none -border 5 "$baseframe" | |
# -resize 128x128> : In the final step, if bigger than 128x128 (>), resize down to max 128x128 | |
resize='-resize 128x128>' | |
fi | |
# make it dance | |
$convert "$baseframe" -page '+5+5' -background none -flatten "$frame1" | |
$convert "$baseframe" -page '+5-5' -background none -flatten "$frame2" | |
$convert "$baseframe" -page '-5-5' -background none -flatten "$frame3" | |
$convert "$baseframe" -page '-5+5' -background none -flatten "$frame4" | |
# gif it | |
# -dispose previous : don't stack frames | |
# -page '+0+0' : don't realign frames | |
$convert -dispose previous -delay "$delay" "$frame1" "$frame2" "$frame3" "$frame4" -page '+0+0' $resize -loop 0 "${gif}" | |
echo $gif | |
# cleanup | |
rm "$baseframe" "$frame1" "$frame2" "$frame3" "$frame4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment