Created
June 1, 2016 16:25
-
-
Save jameslzhu/9138a21c413f77ea0ca3991947f2737c to your computer and use it in GitHub Desktop.
Recrops images to 16:9 resolution, either in 1080p (1920 x 1080), 1440p (2560 x 1440) or 4K (3840 x 2160).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/#!/usr/bin/bash | |
# Recrops images in working directory to 16:9 resolution. | |
# Picks the smallest resolution which fits the image with minimal cropping. | |
OUTPUT_DIR="recropped" | |
EXT="jpg" | |
mkdir -p "$OUTPUT_DIR" | |
for file in *.$EXT; do | |
echo "$OUTPUT_DIR/$file" | |
touch "$OUTPUT_DIR/$file" | |
WIDTH=`identify -ping -format "%w" "$file"` | |
HEIGHT=`identify -ping -format "%h" "$file"` | |
let "IDEAL_WIDTH = $HEIGHT * 16 / 9" | |
let "IDEAL_HEIGHT = $WIDTH * 9 / 16" | |
echo $WIDTH, $HEIGHT, $IDEAL_WIDTH, $IDEAL_HEIGHT | |
# Max height / width restrictions | |
if [ "$WIDTH" -gt 3840 ] || [ "$HEIGHT" -gt 2160 ]; then | |
convert "$file" -background black -gravity center -extent 3840x2160 -flatten "$OUTPUT_DIR/$file" | |
# Too wide -> add black bars on top / bottom | |
elif [ $IDEAL_WIDTH -ge $WIDTH ]; then | |
convert "$file" -background black -gravity center -extent "${IDEAL_WIDTH}x${HEIGHT}" -flatten "$OUTPUT_DIR/$file" | |
# Too narrow -> add black bars on sides | |
else | |
convert "$file" -background black -gravity center -extent "${WIDTH}x${IDEAL_HEIGHT}" -flatten "$OUTPUT_DIR/$file" | |
fi | |
## Old algorithm: resize to standard sizes of 1080p, 1440p, 4K | |
# if [ "$WIDTH" -gt 2560 ] || [ "$HEIGHT" -gt 1440 ]; then | |
# convert "$file" -background black -gravity center -extent 3840x2160 -flatten "$OUTPUT_DIR/$file" | |
# elif [ "$WIDTH" -gt 1920 ] || [ "$HEIGHT" -gt 1080 ]; then | |
# convert "$file" -background black -gravity center -extent 2560x1440 -flatten "$OUTPUT_DIR/$file" | |
# else | |
# convert "$file" -background black -gravity center -extent 1920x1080 -flatten "$OUTPUT_DIR/$file" | |
# fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment