Skip to content

Instantly share code, notes, and snippets.

@maik-s
Created December 2, 2020 10:57
Show Gist options
  • Save maik-s/8ea9c6a1ff38a51e0e71262027a64af8 to your computer and use it in GitHub Desktop.
Save maik-s/8ea9c6a1ff38a51e0e71262027a64af8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# this script expects an image, a target resolution and an output file.
# it then outputs a new png image with the given resolution
# and the given image, centered with transparent background.
# requires imagemagick
image="$1"
targetres="$2"
outfile="$3"
tempdir="$(mktemp -d)/"
if [ -z "${image}" ] || [ -z "${targetres}" ] || [ -z "${outfile}" ] ; then
echo "Usage: "./$(basename "$0")" /path/to/image.ext WidthxHeigth /path/to/output.png"
echo "Please make sure to correctly specify height and width delimited by an x, otherwise unexpected sideeffects may happen!"
exit
fi
if [ -f "${outfile}" ] ; then
echo "[!] target file ${outfile} already exists!"
exit
fi
targetwidth="$(echo "${targetres}" | cut -d "x" -f 1 | tr -d " ")"
convert -size "${targetres}" xc:none "${tempdir}/transparent.png"
convert -resize "${targetwidth}x" "${image}" "${tempdir}/image-resized.png"
composite -gravity center "${tempdir}/image-resized.png" "${tempdir}/transparent.png" "${outfile}"
rm -r "${tempdir}"
echo "[+] All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment