Last active
August 26, 2020 04:53
-
-
Save mocanuga/2765e05d67a58318452aa90f9cfbddcc to your computer and use it in GitHub Desktop.
Simple bash script to optimize images with guetzli
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
#!/bin/bash | |
################################################ | |
# # | |
# Don't forget to make this file executable # | |
# (chmod +x optimize_dir) # | |
# # | |
# Improvements are always welcomed and # | |
# encouraged # | |
# Added a "sizes" parameter to only get image # | |
# dimensions # | |
################################################ | |
# get passed directory | |
dirName=$1 | |
cmd=$2 | |
# if it's not a valid directory, print message and exit | |
if [[ ! -d $dirName ]]; then | |
echo "$dirName is not valid directory" | |
exit 1 | |
fi | |
# count the valid files in the passed path | |
files=`find $dirName -name "*.jpg" -type f | wc -l` | |
# if no files are found, print message then exit | |
if test "$files" == "0" | |
then | |
echo "No files found in $dirName" | |
exit 1 | |
fi | |
# show the number of valid files found | |
printf "Files found: %d\n" "$files" | |
# processing start time | |
start=$(date -u -d "$(date +%H:%M:%S.%N)" +"%s.%N") | |
if [ -n "$cmd" ] && [ "$cmd" == "sizes" ]; then | |
echo "Showing image sizes" | |
# get size info about the images in the passed directory | |
find $dirName -name "*.jpg" -type f -exec identify -format "%w x %h" {} \; | |
else | |
echo "Converting images. Please wait..." | |
# recursively optimize images | |
find $dirName -name "*.jpg" -type f -exec guetzli {} {} \; | |
fi | |
# processing end time | |
end=$(date -u -d "$(date +%H:%M:%S.%N)" +"%s.%N") | |
# processing end time in human readable format | |
elapsed=`date -u -d "0 $end sec - $start sec" +'%H:%M:%S.%N'` | |
# print time report | |
echo "Time elapsed: $elapsed" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment