Image files lossless optimization

In our time, pictures that are generated by digital cameras are usually some megabytes in size, which is really big for a convenient use. This post is about a collection of command-line tools that can be used to do lossless optimization for different types of pictures, in order to keep them in a reasonable size.

PNG Optimization

PNGs is a non-lossy format that comes in several kinds, but for practical purposes, we can think of PNGs as being of two kinds: PNG8 and truecolor PNGs. 8 stands for 8 bits, or 256, the number of palette entries. The terms “PNG8″, “palette PNG” and “indexed PNG” are used interchangeably. The second type of PNGs, true color PNGs (sometimes come across the names PNG24 or PNG32) can have millions of colors, like JPEG.

PNGs is an extensible format, information is stored in so-called “chunks”, and not all of those chunks are required for display. In fact most of them are not. Here comes a list of ways to do the optimization:

  1. Stripping out “chunks” – PNG is an extensible format. Extensions come in the form of chunks and most chunks are not needed for the web.
  2. Reducing the number of colors and switching between PNG types – truecolor PNG, grayscale, palette…
  3. Chosing the best “filter”. Filters are a pre-compression step. You can compress any type of file, but when you know the file is an image, you can do better. Filters are for this purpose.
  4. Optimizing the actual DEFLATE compression algorithm

Different tools specialize in one or more of these areas. You use as many tools as you want for a better result. Here is a list of tools:

pngcrush (skip -brute to run faster)

$ pngcrush -rem alla -brute -reduce old.png new.png

optipng (skip -o7 to run faster)

$ optipng -o7 my.png

pngout – closed source, non-windows binaries here (add parameter -s2 to run faster)

$ pngout my.png

advpng (use -z2 to run faster)

$ advpng -z4 my.png

deflopt – windows only


JPEG Optimization

JPEG is a lossy format, in other words, you lose information every time you edit it. But some operations can be performed losslessly such as rotating it, modifying meta information.

To optimize a JPEG losslessly you remove the meta information and optimize the so called Huffman tables. For large JPEGs (bigger than 10K) you can also convert the image to progressive coding. The tool that we use called JPEGTran and is likely already on your unix/linux box. It also has a windows version.

strip meta and optimize

$ jpegtran -copy none source.jpg > destination.jpg

strip meta and convert to progressive coding

$ jpegtran -copy none -progressive source.jpg > destination.jpg

keep all meta but still optimize

$ jpegtran -copy all source.jpg > destination.jpg


GIF and GIF animations Optimization

GIF is a palette (also called “indexed”) type of image. It contains a palette of indexed colors, up to 256, and for every pixel of the image there is a corresponding color index. For more on the history of the GIF format, click here. GIF is a non-lossy format, which means that when you modify the image, you don’t lose quality. GIF also supports transparency, which is a sort of boolean type of transparency. A pixel in a GIF image is either fully transparent or it’s fully opaque.

What’s the best way to improve a GIF? Convert it to a PNG. It’s true. Most of the time you get a smaller file size from a PNG and the same quality and browser support. You can use many tools to turn your GIFs to PNGs, including ImageMagick and OptiPNG.

Using ImageMagick to convert GIF to PNG

$ convert logo.gif logo.png

ImageMagick again: Convert all files in current directory

$ mogrify -format png *.gif

# option 3: OptiPNG

$ optipng *.gif

Mind that the PNGs can be optimized with PNG optimization tools after the conversion.

GIF also support animations. Since the animations consist of frames and some parts of the image don’t change from one frame to another, we can pass the replicate pixel information. One tool that can help you is called GIFsicle. which doesn’t carry over the duplicated pixel information. The way to run it is:

$ gifsicle -O2 src.gif > dest.gif

Finally make sure you own the rights for and have permission to the image files that you are about to optimize, in case of legal issues.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *