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.

chris166
  • 4,769
  • 4
  • 24
  • 25
  • 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 '09 at 03:57
  • It was the whole EXIF block from a digital camera, including every little detail about this photograph (exposure, shutter time, etc...) – chris166 Jan 08 '10 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 '12 at 06:06
  • 1
    Two interesting comparison articles, that list utilities listed below: [Comparison of JPEG Lossless Compression Tools, June 2014](https://blarg.co.uk/blog/comparison-of-jpeg-lossless-compression-tools); [JPEG optimization tools benchmark, Feb 2013](http://blog.jsdelivr.com/2013/02/jpeg-optimization-tools-benchmark.html). – dma_k Nov 21 '16 at 09:55
  • @Ariel: ICC profile also tends to take a large space. I have seen over 500KB of ICC profile binary data. – saurabheights Dec 27 '16 at 12:45
  • What is wrong with you StackOverflow? A question with 110 votes gets closed? – Gabriel Feb 05 '19 at 08:44

14 Answers14

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.

Kornel
  • 97,764
  • 37
  • 219
  • 309
  • 5
    Excellent! You, sir, need a Flattr button. Or join Gittip. – Thilo Jul 18 '12 at 01:25
  • 2
    ImageOptim and ImageAlpha are great! – jrz Jan 21 '14 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 05 '14 at 21:29
  • why remove EXIF data? :/ – Adriano Apr 07 '17 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 07 '17 at 12:31
  • @Kornel right. That makes sense. Didn't know Photoshop did that. Should I be surprised (*shaking my head*) – Adriano Apr 11 '17 at 11:18
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?)

Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
OutOfMemory
  • 199
  • 3
  • 3
  • 5
    Wow, that sounds promising. If anyone is interested, the download for Windows is at http://gnuwin32.sourceforge.net/packages/jpeg.htm – chris166 Jun 12 '09 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 '09 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 '12 at 06:07
  • 1
    the progressive can often be bigger than baseline for files less than 9k – Collin Anderson Dec 13 '12 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 07 '13 at 12:34
  • Older versions of adoble flash couldn't load progressive JPEGs. We are talking version 6 and older, so that shouldn't be a concern by now. – NiloVelez Dec 02 '13 at 21:53
  • This python script uses libjpeg, should do the trick if you want to run it in a batch: https://github.com/joedicastro/img4web – radtek Mar 02 '15 at 18:20
  • @CollinAnderson definitely agree. Every format has it's use and purpose. Progressive Jpeg is not a begin all end all format and should not be used for everything, as a desktop background it is near useless. For webdesign however it has many. rule of thumb: small images with minimal colour complexity such as used for buttons, banners "PNG/PNG-8" ftw. for small images ie (less than 10-15kb) GIF/PNG/JPG, for larger images with colour complexity such as photo's and such Progressive Jpeg especially if you aren't using a CDN for content. – LokizFenrir Jul 15 '15 at 08:00
  • psst @NiloVelez, flash is dead for the web and should stay that way, viva la HTML5,JS and SVG. the security issues, lack of mobile support, lack of responsiveness not providing anything that cannot be done better than standard html,css and js. more effort than progress... valid point though for the flashlovers ... – LokizFenrir Jul 15 '15 at 08:06
  • Note that Rockbox (for MP3 players/hacked ipods) doesn't support progressive JPEGs either. – Wyatt Ward Mar 25 '16 at 03:05
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]

Ben
  • 2,122
  • 2
  • 28
  • 48
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

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
  • We now have a Mac app available (disclosure: I'm on the development team) – DanJ Jun 11 '12 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](http://www.hackerfactor.com/src/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 '14 at 00:47
  • @onetrickpony: I'm not affiliated with them in any way. – Eduardo Molteni Jan 23 '14 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 04 '17 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 04 '17 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

Graham Butcher
  • 111
  • 1
  • 4
  • 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. – Camilo Martin Mar 22 '16 at 02: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

borjab
  • 11,149
  • 6
  • 71
  • 98
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
Community
  • 1
  • 1
bsy-web
  • 83
  • 1
  • 4
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

AJ Slater
  • 61
  • 1
  • 3
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

timmy
  • 760
  • 1
  • 7
  • 13
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.

bluegray
  • 600
  • 8
  • 18
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.

David Johnstone
  • 24,300
  • 14
  • 68
  • 71
  • 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 '09 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. – David Johnstone Jun 12 '09 at 14:30
  • Do you know how do perform the EXIF meta-data stripping using the PHP version of ImageMagick? – Sonny Mar 30 '10 at 17:11
  • @Sonny Not really, but you might be interested in this function: http://www.php.net/manual/en/function.imagick-stripimage.php – David Johnstone Mar 30 '10 at 23:00
  • Thanks David, that's what I ended up finding. Here's my thread: http://stackoverflow.com/questions/2540068 – Sonny Mar 31 '10 at 12:58
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

agonist_
  • 4,890
  • 6
  • 32
  • 55
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.

Pono
  • 11,298
  • 9
  • 53
  • 70
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.

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154