Skip to content

Instantly share code, notes, and snippets.

@fosterdill
Last active October 17, 2017 16:57
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fosterdill/a38a5fc6715a446c480a86049bb0a0f1 to your computer and use it in GitHub Desktop.
Find png files that don't have transparency and convert to jpg, also recompress all 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