Last active
October 17, 2017 16:57
-
-
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
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/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