Sunday, October 3, 2010

Mass shrinking image files on a Linux web server

This is a script I made to mass-shrink my web-based photo library. If you're able to get the libjpeg toolset installed on your server, you won't need to compile it yourself (notice I did & put the binaries in my cgi-bin). The quality setting is managed by ImageMagick. I also used OptiPNG for PNG squishing: it can also convert BMPs & GIFs.
#!/bin/sh

#JPG
for IMAGE in $(find | grep 'jpg'); do
echo $IMAGE
convert -quality 85 $IMAGE $IMAGE
~/cgi-bin/jpegtran -copy all -optimize -progressive $IMAGE > new.jpg
mv new.jpg $IMAGE
done

#JPEG
for IMAGE in $(find | grep 'jpeg'); do
echo $IMAGE
convert -quality 85 $IMAGE $IMAGE
~/cgi-bin/jpegtran -copy all -optimize -progressive $IMAGE > new.jpg
mv new.jpg $IMAGE
done

#PNG
for IMAGE in $(find | grep 'png'); do
echo $IMAGE
~/cgi-bin/optipng $IMAGE
done

ADDED: the folks at Hurricane Electric say "find -iname *.jpg" works in place of "find | grep 'jpg'"

2 comments:

  1. What were the results - how much smaller did your files yet in total?

    ReplyDelete
  2. My photo lib went from roughly 3GB to 2GB

    ReplyDelete