Created
May 1, 2018 02:52
-
-
Save jokester/132e0ba10e2e4e298b685f8973dc0965 to your computer and use it in GitHub Desktop.
normalize size of smartphone screenshots
This file contains hidden or 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/env bash | |
set -ue | |
set -o pipefail | |
if [[ $# -ne 3 ]]; then | |
cat 1>&2 <<END | |
USAGE: $0 SRC_FILE PHYSICAL_SIZE NEW_FILE | |
size of DST_FILE (in pixels) will be proportional to PHYSICAL_SIZE (160 DPI) | |
END | |
exit 1 | |
elif ! command -v convert; then | |
echo 1>&2 "'convert' program not available'" | |
exit 2 | |
fi | |
input_file="$1" | |
physical_size="$2" | |
new_file="$3" | |
orig_width=$(identify -format "%w" "$input_file") | |
orig_height=$(identify -format "%h" "$input_file") | |
orig_dpi=$(echo "sqrt( $orig_width * $orig_width + $orig_height * $orig_height ) / $physical_size " | bc -l) | |
ratio=$(echo "160.0000 / $orig_dpi" | bc -l) | |
new_size="$(echo "$ratio * $orig_width" | bc)x$(echo "$ratio * $orig_height" | bc)" | |
echo "input: ${orig_width}x${orig_height} @ $orig_dpi DPI" | |
echo "output: $new_size @ 160 DPI" | |
convert "$input_file" -resize "$new_size" "$new_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment