114

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.

6
  • You're saying that there was about 10KB worth of EXIF in an optimized JPG before EXIF was removed?!?!?! I wonder what's in it!
    – netrox
    Dec 20, 2009 at 3:57
  • It was the whole EXIF block from a digital camera, including every little detail about this photograph (exposure, shutter time, etc...)
    – chris166
    Jan 8, 2010 at 11:33
  • @netrox chris166 Those details don't take much space, what takes a lot of space is previews (binary data).
    – Ariel
    Jul 29, 2012 at 6:06
  • 1
    Two interesting comparison articles, that list utilities listed below: Comparison of JPEG Lossless Compression Tools, June 2014; JPEG optimization tools benchmark, Feb 2013.
    – dma_k
    Nov 21, 2016 at 9:55
  • @Ariel: ICC profile also tends to take a large space. I have seen over 500KB of ICC profile binary data. Dec 27, 2016 at 12:45

14 Answers 14

72

I wrote a GUI for all image optimization tools I could find, including MozJPEG and jpegoptim that optimize Huffman tables, progressive scans, and (optionally) remove invisible metadata.

ImageOptim smushing it

If you don't have a Mac, I also have a basic web interface that works on any platform.

6
  • 5
    Excellent! You, sir, need a Flattr button. Or join Gittip.
    – Thilo
    Jul 18, 2012 at 1:25
  • 2
    ImageOptim and ImageAlpha are great!
    – Jon z
    Jan 21, 2014 at 19:45
  • Great tool - minor suggestion. Why restrict JPEGoptim to 80% quality - large background images often you'd want to go to 60% or even lower. thx
    – niico
    Sep 5, 2014 at 21:29
  • why remove EXIF data? :/
    – Adriano
    Apr 7, 2017 at 11:28
  • @AdrienBe 1. Privacy. You may not want everyone on the net know your GPS location. 2. because in files saved from Photoshop there can be megabytes of XML garbage embedded in the files.
    – Kornel
    Apr 7, 2017 at 12:31
68

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 -optimize 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?)

10
  • 5
    Wow, that sounds promising. If anyone is interested, the download for Windows is at gnuwin32.sourceforge.net/packages/jpeg.htm
    – chris166
    Jun 12, 2009 at 13:40
  • 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
    Jun 12, 2009 at 13:51
  • 2
    progressive works fine in basically everything, the only drawback is it uses more CPU to decode (not enough extra to matter).
    – Ariel
    Jul 29, 2012 at 6:07
  • 1
    the progressive can often be bigger than baseline for files less than 9k Dec 13, 2012 at 20:06
  • 3
    Progressive needs a lot more memory to decode. Regular jpegs can be streamed: you only need to decode in small sections. To decode a progressive jpg you need to hold the entire decompressed image in memory at once.
    – jcupitt
    Oct 7, 2013 at 12:34
12

[WINDOWS ONLY]

RIOT(Radical Image Optimization Tool) This is the greatest image optimization tool I have found!

http://luci.criosweb.ro/riot/

You can easily get a 10MB image down to 800KB through sub-sampling. It supports PNG, GIF, and JPEG. It even integrates into context menus so you can send pictures straight there. Allows you to rotate, re-size, compress to specified KB's, and more. Also has plugins for GIMP and IrfanView and other things.

There is also a DLL available if you want to incorporate it into your own programs or java script / c++ program.

Another alternative is http://pnggauntlet.com/ PNGGAUNTLET takes forever but it does a pretty good job.

[WINDOWS ONLY]

6
  • 7
    Warning: RIOT looks good but it installs TuneUp and shit without asking you,.. I'd classify this as Malware.
    – Daniel W.
    Feb 19, 2014 at 8:59
  • 1
    @DanFromGermany is right. See reviews here: download.cnet.com/RIOT/3000-12511_4-10911908.html#summaryList Mar 17, 2014 at 17:08
  • 1
    It never did until recently.
    – Ben
    Apr 1, 2014 at 20:34
  • Malware alert! Chrome download manager warns RIOT is malware
    – ruhong
    Dec 10, 2014 at 10:31
  • 8
    FYI: There is a portable .zip file for RIOT that contains no malware or installers of any kind. Just scroll down on their downloads page. luci.criosweb.ro/riot/download
    – Goyuix
    Feb 4, 2015 at 16:08
12

A new service called JPEGmini produces incredible results. A shame that it's online only. Edit: It's available for Windows and Mac now

5
  • We now have a Mac app available (disclosure: I'm on the development team)
    – DanJ
    Jun 11, 2012 at 10:52
  • 4
    I don't know if you're the author or not, but that app is misleading users. If you run jpegquality.c on their "mini" images to detect the compression amount, and then compress the originals with imagemagick with the same quality => you get the same results, same JPEG artifacts in both images. Imagemagick may be open-source, but that doesn't make it ok to use it and then claim you developed a new compression technology
    – nice ass
    Jan 23, 2014 at 0:47
  • @onetrickpony: I'm not affiliated with them in any way. Jan 23, 2014 at 17:12
  • @niceass How do you know that JPEGmini is using ImageMagick code ? Knowing the quality factor is the same as output from IM is not proof. My guess is that they recompress the image at reducing quality factors perhaps with a little bit of filtering and they use some quality metric to decide when to stop.
    – koan
    May 4, 2017 at 12:07
  • I ran the latest JPEGmini on an image I had previously exported from ImageMagick and it was reported that JPEGmini had already been run on it - which I don't remember doing. Running jpegtran -optimize -copy none on the same image reduced size by 12 KB.
    – koan
    May 4, 2017 at 12:13
10

Tried a number of the suggestions above - I personally was after lossless compression.

My sample image had an original size of 67,737 bytes.

Using kraken.io, it went down to 64,718 Using jpegtran, it went down to 64,718 Using yahoo smush-it, it went down to 61,746 Using imagemagick (-strip), it went down to 65,312

The smush.py option looks promising, but the installation was too complex for me to do quickly

jpegrescan looks promising too, but seems to be unix and I'm using windows

jpegmini is NOT lossless, but I can't tell the difference (down to 22,172)

plinth's Altrasoft jpegstripper app does not work on my windows 7

jpegoptim is not windows - no good for me

Riot (keeping quality at 100%) got it down to 63,416 and with chroma subsampling set to high, it got it down to 61,912 - I don't know if that is lossless or not though, and I think it looks lighter than the original.

So my verdict is yahoo smushit if it must be lossless

1
  • jpegrescan works on Windows if you're stubborn. It shaves off size like magic (5-15% with no loss in quality), which is amazing, because it's just re-ordering the progressive scan ordering AFAIK (you might notice the same in other programs, files sometimes get marginally smaller in Photoshop if you choose different number of scans, for no apparent reason). Also, it was made by one of the guys that made x264, he's such a prodigy in the field of compression. Mar 22, 2016 at 2:52
9

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

1
  • Thanks! Didn't know ImageMagick could do that.
    – chris166
    Jun 12, 2009 at 13:41
7

ImageOptim is really slick. The command line option posted by the author will populate the GUI and show progress. I used jpegtran for optimizing and converting to progressive, then ImageOptim for further progressive optimizations and for other file types.

Reuse of script code also found in this forum (all files replaced in place):

jpegtran

for file in $(find $DIR -type f \( -name "*.jpg" -or -name "*.jpeg" -or -name "*.JPG" \)); do
    echo found $file for optimizing...
    jpegtran -copy comments -optimize -progressive -outfile $file $file
done

ImageOptim

for file in $(find $DIR -type f \( -name "*.jpg" -or -name "*.png" -or -name "*.gif" \)); do
do
    echo found $file for optimizing...
    open -a ImageOptim.app $file
done
4

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

1
  • It works fine but there is a problem with png compression :(
    – user546774
    Oct 26, 2014 at 14:59
4

You can use jpegoptim which will losslessly optimize jpeg files by default. The --strip-all option strips all extra embedded info. You can also specify a lossy mode with the --max switch which is useful when you have images saved with a very high quality setting, which is not necessary for eg. web content.

You get similar optimization as with jpegtran (see answer by OutOfMemory) but jpegoptim can't save to progressive jpegs.

4

I've written a command line tool called 'picopt' (similar to ImageOptim) that uses external programs to optimize JPEGs, PNGs, GIFS, animated GIFS and even comic book archive contents (CBR/CBZ).

This is suitable for use with homebrew on OS X or Linux systems where you have installed tools like jpegrescan, jpegtran, optipng, gifsicle, etc.

https://github.com/ajslater/picopt

2
  • Is there a windows version of this? Jul 2, 2015 at 18:00
  • if you can install its posix dependencies in a windows environment like cygwin it might. You'd be the first to test it, though.
    – AJ Slater
    Jul 15, 2015 at 19:41
3

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.

5
  • Interesting. I always thought 90% quality is the same across all image tools. Is Photoshop the only tool that uses optimized DCT coefficients?
    – chris166
    Jun 12, 2009 at 13:43
  • 1
    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. Jun 12, 2009 at 14:30
  • Do you know how do perform the EXIF meta-data stripping using the PHP version of ImageMagick?
    – Sonny
    Mar 30, 2010 at 17:11
  • @Sonny Not really, but you might be interested in this function: php.net/manual/en/function.imagick-stripimage.php Mar 30, 2010 at 23:00
  • Thanks David, that's what I ended up finding. Here's my thread: stackoverflow.com/questions/2540068
    – Sonny
    Mar 31, 2010 at 12:58
2

I would recommend using http://kraken.io It's ultra-fast webapp which will optimize your PNG and JPEG files far better than smush.it does.

2

I recommend to use JpegOptim, it's free and really nice, you can specify the quality, the size you want ... And easy to use in command line.

JpegOptim

1

May I recommend this for near-transparency:

convert 'yourfile.png' ppm:- | jpeg-recompress -t 97 -q veryhigh -a -m smallfry -s -r -S disable - yourfile.jpg

It uses imagemagick's convert and jpeg-recompress from jpeg-archive.

Both are open-source and work on Windows, Mac and Linux. You may want to tweak the options above for different quality expectations.

Not the answer you're looking for? Browse other questions tagged or ask your own question.