Skip to content

Instantly share code, notes, and snippets.

@tir38
Last active April 21, 2023 18:58
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 tir38/52e4e5b7c6ef85b85cac4e644234c05f to your computer and use it in GitHub Desktop.
Save tir38/52e4e5b7c6ef85b85cac4e644234c05f to your computer and use it in GitHub Desktop.
Resize and sort images for two digital picture frames: landscape and portrait
#!/bin/bash
process() {
OUTPUT_DIR="/Users/jason/Desktop/output"
mkdir -p $OUTPUT_DIR
# Image magick doesn't give me the chance to check exif *before* conversion
# so auto-orient into temp and then move to appropriate directory
TEMP="$OUTPUT_DIR/temp.png"
# the \> will prevent upsample
convert "$1" -resize 1080x1080\> -auto-orient $TEMP
# move to correct dir
# do not put output directory inside same directory as images
# or you'll infiinite loop over newly generated images
LANDSCAPE="landscape"
PORTRAIT="portrat"
mkdir -p $OUTPUT_DIR/$LANDSCAPE
mkdir -p $OUTPUT_DIR/$PORTRAIT
BASE_NAME=$(basename "$1")
NEW_NAME=$(echo "$BASE_NAME" | cut -f 1 -d '.')
HEIGHT=$(identify -format "%h" "$TEMP")
WIDTH=$(identify -format "%w" "$TEMP")
if [[ $HEIGHT -gt $WIDTH ]] # also put square images in landscape dir
then
FULL_PATH="$OUTPUT_DIR"/"$PORTRAIT"/"$NEW_NAME".png
else
FULL_PATH="$OUTPUT_DIR"/"$LANDSCAPE"/"$NEW_NAME".png
fi
if [ -e $FULL_PATH ]
then
echo "File already exists with this name. Not copying $1"
exit
fi
mv $TEMP $FULL_PATH
}
export -f process
echo "Resizing images from $1"
if ! command -v convert &> /dev/null
then
echo "convert cannot be found, did you install Imagemagick?"
exit
fi
find "$1" -iname '*.jpg' -exec bash -c 'process "$@"' bash {} \;
find "$1" -iname '*.jpeg' -exec bash -c 'process "$@"' bash {} \;
find "$1" -iname '*.png' -exec bash -c 'process "$@"' bash {} \;
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment