Skip to content

Instantly share code, notes, and snippets.

@mocanuga
Last active August 26, 2020 04:53
Show Gist options
  • Save mocanuga/2765e05d67a58318452aa90f9cfbddcc to your computer and use it in GitHub Desktop.
Save mocanuga/2765e05d67a58318452aa90f9cfbddcc to your computer and use it in GitHub Desktop.
Simple bash script to optimize images with guetzli
#!/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