Skip to content

Instantly share code, notes, and snippets.

@GlenDC
Last active August 23, 2016 21:45
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 GlenDC/b2e6ce4989145bddbbdb8be8ffae327f to your computer and use it in GitHub Desktop.
Save GlenDC/b2e6ce4989145bddbbdb8be8ffae327f to your computer and use it in GitHub Desktop.
a shell script to resize activity images for exploor
#!/bin/bash
if [ "$#" -lt 1 ]; then
TARGET_DIR="."
else
TARGET_DIR="$1"
fi
if [ "$#" -lt 2 ]; then
ROOT_DIR="$HOME/exploor/images"
else
ROOT_DIR="$2"
fi
mkdir -p "$ROOT_DIR" 2> /dev/null
LOG_FILE="$ROOT_DIR/log_$(date +%s).txt"
function LOG {
echo "$1" >> "$LOG_FILE"
echo "$1"
}
# resize functons
function RESIZE_PORTRAIT_IMG {
# parse input params
dw=$3
dh=$4
retry=$5
original=$1
# define result name
result="${2}/${dw}_${dh}_$(basename "$original")"
# copy img
cp "$original" "$result"
# resample
(sips --resampleWidth "$dw" "$result" &>/dev/null) || \
(LOG "ERROR: couldn't resample portrait img $original")
# set offset for cropping
H=$(sips -g pixelHeight "$result" | grep pixelHeight | grep -Eo "\d+")
if [ "$H" -lt "$dh" ]; then
if [ "$retry" == "true" ]; then
RESIZE_LANDSCAPE_IMG "$1" "$2" "$3" "$4" "false"
else
LOG "ERROR: couldn't resize img $original to $result (too many atempts)"
fi
else
# crop
dx=0
dy=$(((H-dh)/2))
DIM="${dw}x${dh}+$dx+$dy"
LOG "cropping to $DIM"
(convert "$result" -crop "$DIM" "$result" &>/dev/null) || \
(LOG "ERROR: couldn't crop portrait img $original")
fi
}
function RESIZE_LANDSCAPE_IMG {
# parse input params
dw=$3
dh=$4
retry=$5
original=$1
# define result name
result="${2}/${dw}_${dh}_$(basename "$original")"
# copy img
cp "$original" "$result"
# resample
(sips --resampleHeight "$dh" "$result" &>/dev/null) || \
(LOG "ERROR: couldn't resample landscape img $original")
# set offset for cropping
W=$(sips -g pixelWidth "$result" | grep pixelWidth | grep -Eo "\d+")
# crop
if [ "$W" -lt "$dw" ]; then
if [ "$retry" == "true" ]; then
RESIZE_PORTRAIT_IMG "$1" "$2" "$3" "$4" "false"
else
LOG "ERROR: couldn't resize img $original to $result (too many atempts)"
fi
else
dy=0
dx=$(((W-dw)/2))
DIM="${dw}x${dh}+$dx+$dy"
LOG "cropping to $DIM"
(convert "$result" -crop "$DIM" "$result" &>/dev/null) || \
(LOG "ERROR: couldn't crop landscape img $original")
fi
}
INDEX=1
find "$TARGET_DIR" -type f \
-iname "*.JPG" \
-o -iname "*.JPEG" \
-o -iname "*.GIF" \
-o -iname "*.PNG" \
-o -iname "*.TIFF" | \
while read IMG ; do
W=$(sips -g pixelWidth "$IMG" | grep pixelWidth | grep -Eo "\d+")
H=$(sips -g pixelHeight "$IMG" | grep pixelHeight | grep -Eo "\d+")
OUTPUT_DIR="${ROOT_DIR}/$(dirname "$IMG")"
# ensure output dir exists
mkdir -p "$OUTPUT_DIR" 2> /dev/null
LOG "IMG#${INDEX}: $IMG (${W}x${H})"
if [ "$W" -lt "$H" ]; then
RESIZE_PORTRAIT_IMG "$IMG" "$OUTPUT_DIR" 1000 556 "true"
RESIZE_PORTRAIT_IMG "$IMG" "$OUTPUT_DIR" 600 400 "true"
else
RESIZE_LANDSCAPE_IMG "$IMG" "$OUTPUT_DIR" 1000 556 "true"
RESIZE_LANDSCAPE_IMG "$IMG" "$OUTPUT_DIR" 600 400 "true"
fi
INDEX=$((INDEX+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment