Skip to content

Instantly share code, notes, and snippets.

@Wikinaut
Last active April 17, 2021 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wikinaut/5e13422f08cfaa6e35759aef65ecf2d1 to your computer and use it in GitHub Desktop.
Save Wikinaut/5e13422f08cfaa6e35759aef65ecf2d1 to your computer and use it in GitHub Desktop.
Extend (or crop) the canvas background of an image to a multiple of a modulus (such as 32)
#!/bin/bash
if [ $# -lt 1 ]; then
echo "extend-crop-background <imagefile> [modulus [extent]]"
echo "Extend or crop image to a multiple of modulus ±'extent'"
echo
echo "modulus: default 32"
echo "extent: default=modulus"
echo
echo "Examples:"
echo "extend-crop-background in.jpg 32"
echo "extend-crop-background in.jpg 32 -32"
echo "extend-crop-background in.jpg 100 -32"
exit
fi
magick "$1" \
-background none \
-gravity center \
-set option:xmod "${2:-32}" \
-set option:zz "$3" \
-set wx '%[fx:mod(w,xmod)==0?w:w-mod(w,xmod)+zz+xmod]' \
-set hx '%[fx:mod(h,xmod)==0?h:h-mod(h,xmod)+zz+xmod]' \
-extent '%[wx]x%[hx]' \
-set filename:mod '%[xmod]' \
-set filename:size '%wx%h' \
-set filename:name '%t' \
'%[filename:name]_mod%[filename:mod]-%[filename:size].png'
# to be used in a small script for composing small images as sprites in a bigger png
#
# #!/bin/bash
#
# if [ $# -lt 1 ] ; then
# echo "Make a sprite sheet from images"
# echo "Usage: make-sprite-sheet.sh *.png"
# exit
# fi
#
# now=$(date +"%Y%m%d-%H%M%S")
# for i in "$@";do extend-crop-background.sh "$i";done
# montage -mode concatenate -background transparent *.png sprite-sheet-${now}.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment