Skip to content

Instantly share code, notes, and snippets.

@sendyputra
Forked from fosterdill/optimize_images.sh
Created October 17, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sendyputra/63b8188a9911c21a53df28dcdd77a039 to your computer and use it in GitHub Desktop.
Save sendyputra/63b8188a9911c21a53df28dcdd77a039 to your computer and use it in GitHub Desktop.
Find png files that don't have transparency and convert to jpg, also recompress jpg files for the web
#!/bin/sh
echo You should have the project tracked by version control in case something goes wrong.
printf 'Press any key to continue...'
read -r
path=${1:-.}
echo Optimizing images in "$path"
if [ -z "$(command -v jpeg-recompress)" ] || [ -z "$(command -v ladon)" ] || [ -z "$(command -v mogrify)" ]; then
echo You need ladon, ImageMagick, and jpeg-archive to be able to run this script
exit 1
fi
transparency_test() {
if [ "$(identify -format '%[channels]' "$1")" = "srgb" ]; then
echo "$1"
fi
}
export -f transparency_test
find_pngs_with_no_tranparency() {
find "$path" -type f -name "*.png" -exec sh -c 'transparency_test "$1"' _ {} \;
}
pngs=$(find_pngs_with_no_tranparency)
if [ "$pngs" ]; then
echo "$pngs" | xargs mogrify -format jpg
echo "$pngs" | xargs rm
ladon "$path/**/*.jpg" -- jpeg-recompress FULLPATH FULLPATH
echo "Done!"
else
echo "No PNG files to compress!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment