Last active
March 24, 2020 14:14
-
-
Save o3bvv/79fa3f85f05924017d25004496493adb to your computer and use it in GitHub Desktop.
Resize Big Pictures in Bulk
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 | |
CURRENT_DIR=$(pwd) | |
MAX_SIDE_SIZE=1024 # see also: https://www.imagemagick.org/script/command-line-processing.php#geometry | |
JPEG_EXTENSION_PRIMARY=jpg | |
JPEG_CONVERSION_QUALITY=85 | |
resize_JPEG () { | |
file_path=$1 | |
echo "${file_path}" | |
# do not specify JPEG quality to avoid new compression | |
mogrify \ | |
-strip \ | |
-interlace Plane \ | |
-resize "${MAX_SIDE_SIZE}x${MAX_SIDE_SIZE}"\> \ | |
"${file_path}" | |
} | |
resize_other () { | |
file_path=$1 | |
echo "${file_path}" | |
mogrify \ | |
-strip \ | |
-interlace Plane \ | |
-resize "${MAX_SIDE_SIZE}x${MAX_SIDE_SIZE}"\> \ | |
-format $JPEG_EXTENSION_PRIMARY \ | |
-quality $JPEG_CONVERSION_QUALITY \ | |
"${file_path}" | |
# delete original file if its extension in lowercase is not $JPEG_EXTENSION_PRIMARY | |
file_ext=$(echo "${file_path}" | sed 's/.*\.//' | tr '[:upper:]' '[:lower:]') | |
if [[ "$file_ext" != "$JPEG_EXTENSION_PRIMARY" ]]; then | |
rm -f "${file_path}"; | |
fi | |
} | |
find "$CURRENT_DIR" -type f -print0 | while IFS= read -r -d $'\0' file_path | |
do | |
file_info=$(file "$file_path" | awk -F: '{ print $2 }') | |
if [[ "${file_info}" == *"JPEG"* ]]; then | |
resize_JPEG "$file_path"; | |
elif [[ "${file_info}" == *"image"* ]]; then | |
resize_other "$file_path"; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment