Last active
January 24, 2020 01:41
-
-
Save timw4mail/6975595dc486e0aa98c188de8dcaad71 to your computer and use it in GitHub Desktop.
Image Optimization script
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/env bash | |
set -euo pipefail | |
declare threads=`getconf _NPROCESSORS_ONLN` | |
optimise () { | |
declare -a exts=("${!1}") | |
declare msg=$2 | |
for ext in ${exts[@]} | |
do | |
find -O3 . -type f -iname $ext | parallel "-j${threads}" "mogrify -resize 1920x1080\> -units pixelsperinch -density 72 -format webp {}" | |
done | |
echo $msg | |
} | |
delete () { | |
declare ext=$1 | |
declare msg=$2 | |
find -O3 . -iname $ext | parallel "-j${threads}" rm -f {} | |
echo $msg | |
} | |
delete_arr () { | |
declare -a exts=("${!1}") | |
declare msg=$2 | |
for ext in ${exts[@]} | |
do | |
find -O3 . -iname $ext | parallel "-j${threads}" rm -f {} | |
done | |
echo $msg | |
} | |
# Convert uncompressed formats to webp | |
bmps=("*.bmp","*.ppm","*.pgm","*.pbm","*.pnm") | |
optimise bmps[@] "Converted BMPs to WEBPs" && delete_arr bmps[@] "Deleted BMPs" | |
# Convert some more obscure formats to webp | |
heifs=("*.heic", "*.heif") | |
optimise heifs[@] "Converted HEIFs to WEBPs" && delete_arr heifs[@] "Deleted HEIFs" | |
# Convert PNGs to webp | |
png=("*.png") | |
optimise png[@] "Converted PNGs to WEBPs" && delete_arr png[@] "Deleted PNGs" | |
# Convert JPEGs to webp | |
jpegs=("*.jpeg", "*.jpg") | |
optimise jpegs[@] "Converted JPGs to WEBPs" && delete_arr jpegs[@] "Deleted JPGs" | |
# Limit density of webp images | |
webps=("*.webp") | |
optimise webps[@] "Optimized WEBPs" | |
delete "*.webp~" "Deleted Backup Files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment