Skip to content

Instantly share code, notes, and snippets.

@tcbarrett
Last active December 28, 2018 06:26
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 tcbarrett/db6aed034f6f5fef076598a41a3b44a3 to your computer and use it in GitHub Desktop.
Save tcbarrett/db6aed034f6f5fef076598a41a3b44a3 to your computer and use it in GitHub Desktop.
Drezha Image Magic
#!/bin/bash
#
# Covert phone images using Image Magic
#
while getopts ":h" opt; do
case ${opt} in
h )
echo "Usage:"
echo " ${0##*/} -h Display this help message"
echo " ${0##*/} <filename> Convert image and create thumbnail using im"
exit 0
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
;;
esac
done
shift $((OPTIND -1))
timestamp="$(date +"%Y%m%d%H%M%S")"
desktop=$HOME/Desktop
tempFile=$desktop/Temp.jpg
infile=$1
# Check a file has been specified in arguments
if [ -z "$infile" ]
then
echo "Missing argument. Please use -h for help"
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# Check file to be converted exists
if [ ! -f "$infile" ]
then
echo "Cannot find file: $infile"
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# Check that the convert command exists
convert -h >/dev/null 2>&1 || {
echo >&2 "This script requires Imagic Magic to be installed. Aborting."
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
}
# Resizes images to 1,228,800 pixels (1280×960, assuming a standard lanscape iPhone image of 4032×3024 pixels)
convert ${infile} -resize 1228800@ -format jpg ${desktop}/img_${timestamp}.jpg
# Thumbnail creation – 76,800 pixels (320 x 240)
convert ${infile} -resize 76800@ -format jpg ${tempFile}
# Creates white background so that images are all the same size (320×320)
magick convert ${tempFile} -gravity center -background white -extent 330×330 ${desktop}/Thumbnail_${timestamp}.jpg
# Removes temp file
rm ${tempFile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment