views:

928

answers:

6

Do you know of any tools (preferrably command-line) to automatically and losslessly optimize JPEGs that I could integrate into our build environment? For PNGs I'm currently using PNGOUT, and it generally saves around 40% bandwidth/image size.

At the very least, I would like a tool that can strip metadata from the JPGs - I noticed a strange case where I tried to make thumbnail from a photograph, and couldn't get it smaller than 34 kB. After investigating more, I found that the EXIF data was still part of the image, and the thumbnail was 3 kB after removing the metadata.

And beyond that - is it possible to further optimize JPGs losslessly? The PNG optimizer tries different compression strategies, random initialization of the Huffmann encoding etc.

I am aware that most savings come from the JPEG quality parameter, and that it's a rather subjective measure. I'm only looking for a tool that can be run as a build step and that losslessly squeezes a few bytes from the images.

+2  A: 

I would try Imagemagick. It has tons of command line options, its free and have a nice license. http://www.imagemagick.org

There seems to be an option called Strip that may help you: http://www.imagemagick.org/script/command-line-options.php#strip

borjab
Thanks! Didn't know ImageMagick could do that.
chris166
+1  A: 

Here's an app I wrote that will strip metadata for you.

plinth
+1  A: 

I too would recommend ImageMagick. It has a command line option to remove EXIF metadata

mogrify -strip image.jpg

There are plenty of other tools out there that do the same thing.

As far as recompressing JPEGs go, don't. JPEGs are lossy to start with, so any form of recompression is only going to hurt image quality. However, if you have losslessly encoded images, some encoders do a better job than others. I have noticed that JPEGs done with Photoshop consistently look better than when encoded with ImageMagick (despite the same file size) due to complicated reasons. Furthermore (and this is relevant to you), I know that at least Photoshop can save JPEGs as optimized which means they drop compatibility with some stuff that you probably don't care about to save a couple of KB. Also, make sure you don't have any colour profiles embedded and you may be able to save another couple of KB.

David Johnstone
Interesting. I always thought 90% quality is the same across all image tools. Is Photoshop the only tool that uses optimized DCT coefficients?
chris166
I though 90% was going to be the same across various tools until I tried saving the same images in Photoshop and Imagemagick. I found that about 70% in Save for Web in Photoshop made files that were about the same size as 92% in Imagemagick, but the Photoshop files were noticeably better quality. I have no idea how other tools do it.
David Johnstone
Do you know how do perform the EXIF meta-data stripping using the PHP version of ImageMagick?
Sonny
@Sonny Not really, but you might be interested in this function: http://www.php.net/manual/en/function.imagick-stripimage.php
David Johnstone
Thanks David, that's what I ended up finding. Here's my thread: http://stackoverflow.com/questions/2540068
Sonny
+6  A: 

I use libjpeg for lossless operations. It contains a command-line tool jpegtran that can do all you want. With the commandline option -copy none all the metadata is stripped, and -optimise does a lossless optimization of the Huffmann compression. You can also convert the images to progressive mode with -progressive, but that might cause compatibility problems (does anyone know more about that?)

OutOfMemory
Wow, that sounds promising. If anyone is interested, the download for Windows is at http://gnuwin32.sourceforge.net/packages/jpeg.htm
chris166
I'm going for this one. It reduces my background image from 62 kB to 49 kB in progressive mode. Another image (27 kB) was reduced to 23 kB. That's 15-20% savings without loss in quality!
chris166
+2  A: 

I wrote GUI for all image optimization tools I could find, including jpegoptim and jpegtran that remove EXIF data.

porneL
A: 

In case anyone's looking, I've written an offline version of Yahoo's Smush.it. It will losslessly optimise pngs, jpgs and gifs (animated and static):

http://github.com/thebeansgroup/smush.py

timmy