Optimizing Graphics Files

Clear, small, fast (icon)

It's important that all websites optimize their images – Google does, and so should you! This section details how to optimize web images so that they reproduce well, take minimal space and load as quickly as possible.

Choose the Appropriate File Format

Knowing the right file format for the type of image is probably the most important factor in optimizing graphics. As explained in more detail on my Computer Graphic File Formats page, the most common types of graphic files – GIF, JPG and PNG – have two fairly distinct uses: GIF or PNG for “illustrations”, JPG for “photos”.

The right format

Ian logo (icon)

GIF or PNG files are typically used for:
Logos, headings, buttons, borders, diagrams, charts, animations.

Uluru photo (icon)

JPG files are typically used for:
Photos, products, scenery, faces, textures, backgrounds.

The wrong format

Choosing the wrong file format for the type of image won't fail completely – you'll still get an image file that works. However, the result will be inferior – typically a much larger filesize plus much lower image quality.

If you've unfortunately got it wrong and have created images in the incorrect file format, there's no easy solution – the damage has been done. In other words, you can't simply use a graphics program to convert an inferior GIF file into a JPG file (or vice versa) and expect to regain the quality that was lost. The only way to “undo” the damage and get the images into the correct file format is to re-create them from the originals.

Logo Comparison

The following sample logo has been saved in each of the three common file formats: PNG, GIF and JPG. Compare the sharp detail and compact filesize of the 4-bit PNG or GIF files with the fuzzy detail and much greater filesize of the JPG file.

Ian logo – 4-bit PNG format

Ian Logo, 4-bit PNG format

Filename: ian-logo-4bit.png
Settings: 4-bit = 16 colors
Filesize: 2,610 bytes
Compression: 91%

Zoomed-in detail

Ian Logo, 4-bit PNG format - Detail

The above 4-bit PNG file has sharp detail and solid, consistent colors. The compression is excellent, with the 4-bit PNG file only 1/11th the size of the original image.

For this logo, 4-bit PNG is the clear winner.

Ian logo – 4-bit GIF format

Ian Logo, 4-bit GIF format

Filename: ian-logo-4bit.gif
Settings: 4-bit = 16 colors
Filesize: 3,447 bytes
Compression: 88%

Zoomed-in detail

Ian Logo, 4-bit GIF format - Detail

The above 4-bit GIF file has the same sharp detail and solid, consistent colors as the 4-bit PNG file. The compression is still okay, with the 4-bit GIF file around 1/8th the size of the original image.

For this logo, 4-bit GIF ranks second place.

Ian logo – low-quality JPG format

Ian Logo, low-quality JPG format

Filename: ian-logo-q50.jpg
Settings: Save For Web Quality 50
Filesize: 12,457 bytes
Compression: 57%

Zoomed-in detail

Ian Logo, low-quality JPG format - Detail

The above low-quality JPG file has fuzzy detail and inconsistent colors – particularly around areas with sharp contrasts. The compression is also not great, with the low-quality JPG file nearly half the size of the original image.

For this logo, low-quality JPG ranks equal last.

Ian logo – high-quality JPG format

Ian Logo, high-quality JPG format

Filename: ian-logo-q90.jpg
Settings: Save For Web Quality 90
Filesize: 25,514 bytes
Compression: 12%

Zoomed-in detail

Ian Logo, high-quality JPG format - Detail

The above high-quality JPG file still has some defects, but they're now barely visible. However, the compression is now much worse, with the high-quality JPG file now around 7/8th the size of the original image.

For this logo, high-quality JPG ranks equal last.

Photo Comparison

The following sample photo has been saved in each of the three common file formats: JPG, PNG and GIF. Compare the smooth shading and compact filesize of the JPG file with the spotty shading and 50% larger filesize of the 8-bit PNG or GIF files – or the almost 5× larger filesize of the 24-bit PNG file.

Uluru photo – JPG format

Uluru photo, JPG format

Filename: uluru-photo-q70.jpg
Settings: Save For Web Quality 70
Filesize: 26,838 bytes
Compression: 84%

Zoomed-in detail

Uluru photo, JPG format - Detail

The above JPG file has smooth shading – particularly throughout the sky. The compression is excellent, with the JPG file only 1/6th the size of the original image.

For this photo, JPG is the clear winner.

Uluru photo – 24-bit PNG format

Uluru photo, 24-bit PNG format

Filename: uluru-photo-24bit.png
Settings: 24-bit = 1.7M colors
Filesize: 122,426 bytes
Compression: 29%

Zoomed-in detail

Uluru photo, 24-bit PNG format - Detail

The above 24-bit PNG file is an absolutely pristine image that is identical to the original. The compression, however, is much worse than any of the other formats, with the 24-bit PNG file around 5/7 the size of the original image.

For this photo, 24-bit PNG ranks equal second place.

Uluru photo – 8-bit PNG format

Uluru photo, 4-bit PNG format

Filename: uluru-photo-8bit.png
Settings: 8-bit = 256 colors
Filesize: 38,380 bytes
Compression: 78%

Zoomed-in detail

Uluru photo, 8-bit PNG format - Detail

The above 8-bit PNG file has spotty shading due to GIFs color reduction down to 256 colors. The compression is okay, with the 8-bit PNG file only 2/9 the size of the original image.

For this photo, 8-bit PNG ranks equal second place.

Uluru photo – 8-bit GIF format

Uluru photo, 8-bit GIF format

Filename: uluru-photo-8bit.gif
Settings: 8-bit = 256 colors
Filesize: 41,598 bytes
Compression: 76%

Zoomed-in detail

Uluru photo, GIF format - Detail

The above 8-bit GIF file has the identical spotty shading to the 8-bit PNG file due to the identical color reduction. The compression is fractionally worse, with the 8-bit GIF file closer to 1/4 the size of the original image.

For this photo, 8-bit GIF ranks last.

File Format Summary

The above has shown the most appropriate file formats for the two main image types (illustrations or photos). Choosing to save an illustration as a PNG or GIF file, or choosing the save a photo as a JPG file, is probably the most important step when creating images.

In the next section, we'll cover the optimization of the images themselves. Not surprisingly, the type of optimization also depends on the type of image. These separate optimizations will be covered in separate sections.

Optimization – PNG or GIF Colors

Let's begin with how to optimize illustrations, which are typically saved as PNG or GIF files.

The main optimization step for such files is to choose an appropriate number of colors. GIF files are an older format and are limited to a maximum “palette” of 256 colors. The newer PNG files can contain as many as 16.7 million colors (known as “full color” or “true color”), but reducing the palette to 256 or fewer colors will greatly reduce the filesize.

Generally, one selects the maximum size of the color palette, typically choosing from 2, 4, 8, 16, 32, 64, 128 or 256 colors. The graphics software then selects the best colors to suit that image, “dithering” (spreading around dots of color) from that palette to approximate any unavaliable colors.

Limited color palette

2 to 16.7M

Imagine a limited-color image consisting of a small blue square containing a white number with a black outline and soft shadow. Here's nine versions of that image showing how it would look with different numbers of colors.

For this image, the bare minimum is four colors: Three for the main elements (Blue, White, Black) plus one for the shadow (Dark Blue). Choosing sixteen colors allows some intermediate shades of blues and greys to give a smooth outline plus a wide, soft shadow – at the expense of doubling the filesize.

Choosing more than sixteen colors is probably a waste. The filesize will be even greater, but the visible quality will be hardly any better.

Extensive color palette

2 to 16.7M

Imagine instead a similar image with a full rainbow-colored background. Once again, here's nine versions of that image with different numbers of colors.

In this case, the bare minimum is probably sixteen colors: The eight “primary” colors (Red, Yellow, Green, Cyan, Blue, Magenta, Black, White) plus eight intermediate colors (Orange, Lime-Green, Greenish-Cyan, Light Blue, Purple, Pink, Dark Grey, Light Grey). The result, however, still looks pretty rough and “spotty”.

For a smooth result, this image really does need the maximum 256 colors (or even 16.7 million) – once again at the expense of doubling (or tripling) the filesize.

Too few or too many colors

As seen in the above examples, choosing too few colors results in loss of image quality because the dithering will be more severe, resulting in a noticeably “spotty” image – and sometimes missing certain key colors.

Choosing too many colors instead results in wasted storage. I've seen plenty of full-color PNG files or 256-color GIF files that would have been perfectly fine with a 16-color palette.

GIF Optimization Example

The following example uses an early (circa 2006) shoelace knot diagram from my popular Ian's Shoelace Site. Back then, this image was typically viewed more than 1,000 times per day, making it well worth optimizing.

Can you spot the difference between the three versions?

Colors = 256, filesize = 8,064 bytes

Ian Knot diagram - 256 colors

The “full” version uses the maximum GIF file allowance of 256 colors. This is more than enough colors: Black, Grey, White, Blue and Yellow, plus intermediate shades of each, producing a clear diagram with fine detail and smooth shading.

Colors = 64, filesize = 5,633 bytes

Ian Knot diagram - 64 colors

This optimized version came after experimenting with 128, 64, 32 and even 16 colors, eventually settling on 64 colors as the best balance of reasonable image quality and compact filesize.

Colors = 16, filesize = 3,366 bytes

Ian Knot diagram - 16 colors

Taking the optimization further, I created a custom palette of 16 carefully chosen colors: 8 × Greyscale, 4 × Blue, 4 × Yellow. The resultant image is almost indistinguishable, while the GIF file was another 40% smaller!

NOTE: Creating a custom color palette is a lot of work for one-off image optimization. But in cases such as my Ian's Shoelace Site, where there are hundreds of images with identical palettes, the long-term savings are well worth the up-front effort.

JPG Optimization – Quality Setting

Let's now look at optimizing photos, which are typically saved as JPG files.

05 / 90 (icon)

The first most important optimization step is to choose an appropriate quality setting when saving the file. Almost all graphics software uses some sort of linear scale. The following examples were created with Photoshop's “Save For Web” scale of 0 to 100.

JPG Quality 10

Uluru photo – quality 10

Uluru photo, Quality 10

Filename: uluru-photo-q10.jpg
Filesize: 5,632 bytes
Compression: 97% (31:1)

Zoomed-in detail

Uluru photo, Quality 10 - Detail

This minimal quality image is missing detail and is noticeably chopped up into 8 × 8 pixel blocks.

JPG Quality 30

Uluru photo – quality 30

Uluru photo, Quality 30

Filename: uluru-photo-q30.jpg
Filesize: 9,864 bytes
Compression: 94% (18:1)

Zoomed-in detail

Uluru photo, Quality 30 - Detail

Fairly blurry detail, plus sharp contrasts between the branches and sky introducing “artifacts”.

JPG Quality 50

Uluru photo – quality 50

Uluru photo, Quality 50

Filename: uluru-photo-q50.jpg
Filesize: 15,099 bytes
Compression: 91% (11:1)

Zoomed-in detail

Uluru photo, Quality 50 - Detail

Everything looking pretty clear, though still some noticeable artifacts around the branches.

JPG Quality 70

Uluru photo – quality 70

Uluru photo, Quality 70

Filename: uluru-photo-q70.jpg
Filesize: 26,838 bytes
Compression: 84% (6:1)

Zoomed-in detail

Uluru photo, Quality 70 - Detail

Excellent image overall. Smooth shades, fine details, few artifacts.

JPG Quality 90

Uluru photo – quality 90

Uluru photo, Quality 90

Filename: uluru-photo-q90.jpg
Filesize: 55,364 bytes
Compression: 68% (3:1)

Zoomed-in detail

Uluru photo, Quality 90 - Detail

Artifacts almost non-existent. Sky is less smooth as fine detail retained from the original image emerges.

JPG Quality Setting Summary

Quality setting too low

It's pretty clear from the above that a JPG quality setting of 50 is probably the practical minimum, below which just about any image will be noticeably degraded. A very low setting might be useful for something like a huge, faint background image, in which the image degradation would be outweighed by filesize and bandwidth savings.

Quality setting too high

Choosing a JPG quality setting higher than 90 gives diminishing returns. It results in much larger files with very little extra increase in visible quality. It's generally agreed that anything above 95 is pretty much a waste.

Ian's recommended setting

I generally use a JPG quality setting of between 60 and 70 for the photos on this website. This provides a reasonable balance between sufficient image quality and modest filesize.

Again, the quality setting depends on the graphics software used, so you may have to experiment with your software to find a similar “balanced” setting.

Why not 100% quality?

Don't mistake the above scale for a percentage! A setting of 100 does NOT mean “100% quality”, as there is still some tiny loss of detail. Also, a setting of 75 does NOT mean either “75% of the quality” or “75% of the filesize”. The settings merely adjust the aggressiveness of the compression, and the results will vary from one image to the next.

NOTE: The quality scale can also vary between different programs, with some programs even using a reverse scale, such that a higher number means higher compression and thus results in lower quality.

JPG Optimization – Block Size

JPG blocks (icon)

Part of the JPG compression algorithm is to break down the image into 8×8 pixel “blocks”. If you zoom into a low-quality JPG image until you can make out the individual pixels, those 8×8 blocks will become quite apparent – even without the faint lines that I've added to this sample image.

Note that the blocks will only really be noticeable at very low quality settings (eg. 10 or 20). At normal high-quality settings (eg. 60 or 70), the blocks will typically blend together seamlessly – but they nonetheless have an impact on the image.

One common situation in which the 8×8 block size should be taken into account is when a simple solid-colored border is added to a photo, then saved as a JPG file. The hard boundary between the border and the photo can wreak havoc with JPG's block-based compression algorithm.

Border Comparison – 5 Pixel vs 8 Pixel

The following examples show the same photo with either a 5 pixel border (which has the hard boundary running 5/8 of the way through each 8×8 block) or an 8 pixel border (which has the hard boundary aligned perfectly on the 8×8 block boundaries).

5 Pixel Border

Koala photo – 5 pixel border

Koala photo, 5 pixel border, JPG Quality 60

Filename: koala-5px-q60.jpg
Filesize: 22,793 bytes

Zoomed-in detail

Koala photo, 5 pixel border, JPG Quality 60 – Detail

What should be a solid green border instead contains spots and banding of lighter and darker shades of green. The areas of photo immediately adjacent to the border also contain some faint banding. The net effect is a messy looking border.

8 Pixel Border

Koala photo – 8 pixel border

Koala photo, 8 pixel border, JPG Quality 60

Filename: koala-8px-q60.jpg
Filesize: 18,867 bytes

Zoomed-in detail

Koala photo, 5 pixel border, JPG Quality 60 – Detail

This border is pristine, with every single pixel the exact same color of green. As a bonus, the border data compresses extremely well, resulting in a 17% smaller JPG file. This is the clear winner.

JPG Block Size – Extreme Example

Finally, here's an extreme example of block-based image data being compressed with JPG's block-based compression, using an extremely low quality setting to exaggerate the compression artifacts.

JPG checkers – 5×5 vs 8×8 blocks

JPG checkers

Filename: jpg-checkers.jpg

The top-left half of the image is built out of 5×5 pixel blocks, whereas the bottom-right half is built out of 8×8 pixel blocks.

Zoomed-in detail

JPG checkers – Detail

The 5×5 image blocks – which are out of sync with JPG's compression blocks – have become a blurry, spotty mess. By contrast, the 8×8 image blocks – which are perfectly aligned with JPG's compression blocks – remain perfect.

JPG Block Size Summary

Aligning on 8×8 blocks

When adding solid-color elements to a photo – such as a border or an embedded rectangular area – try to align such elements with JPG's 8×8 compression blocks.

For a border to be aligned means that the border thickness must be an exact multiple of 8 pixels. It also means that the width and height of the whole image must each be an exact multiple of 8 pixels (eg. the 240×240 pixel examples above).

For an embedded rectangular area to be aligned means that the width and height of the rectangle must each be an exact multiple of 8 pixels (eg. the 120×72 pixel “8×8” rectangle in the image above). It also means that the top-left corner of the rectangle must begin at (x,y) coordinates that are each an exact multiple of 8 pixels (eg. x=104, y=152).

Avoiding 16×16 blocks

Assuming that you've carefully added elements aligned on 8×8 blocks, it's pointless if you then save the JPG file with a low quality setting that uses 16×16 blocks.

When using Photoshop's “Save For Web” to create JPG files, my recommended quality setting of 60 or 70 will result in 8×8 blocks. With lower quality settings of 50 or below, Photoshop further bumps up the compression by using “4:2:0 Chroma Subsampling” – which results in 16×16 blocks.

Unless you're prepared to use elements aligned on those larger blocks, stick with Photoshop's “Save For Web” quality setting of 51 or higher.

If using other software, check for an option to switch off “Chroma Subsampling”. For example, when saving an image using IrfanView, tick the “Show options dialog” checkbox, then tick the “Disable chroma color subsampling” checkbox.

JPG Optimization – Metadata Removal

Extras within 240x320 JPG file

The next most important optimization step for JPG files is to eliminate any “hidden extras” (or “metadata”). I've decoded the innards of thousands of JPGs. The image at right is a representation of the innards of a fairly typical file.

The two blocks with green borders are the only necessary portions. The seven blocks with yellow borders are all “extras”.

  • JFIF Header: Indicates the file format.
  • Exif data: Date and time photographed, shutter speed, aperture and other photographic settings, even the make and model of camera.
  • Thumbnails: Miniature versions of the photo for fast previews. The digital camera added one thumbnail and the image editing software, Adobe Photoshop, added the second.
  • Photoshop data: Various other info added by Adobe Photoshop.
  • Adobe XAP data: More info from Photoshop, this time in XML format, and often containing only 25% data and 75% blank space.
  • ICC profile: Color profile for more accurate color rendition on different systems (eg. screens, printers). Ignore the actual “rainbow”, which I added to show that this block has something to do with color.
  • Adobe data: Tiny bit of additional data added by Adobe Photoshop.
  • Main image: Finally, after all of the “extras”, we reach the actual image data.

This JPG file contains about 44% of “extras”, the bulk of which are downloaded and then simply discarded by most web browsers. It's a real waste!

There are several ways to ensure that your JPG files contain fewer “extras”, but the simplest way to ensure that they contain NONE is to run them through a “metadata removal” program. Luckily, I've created just such a program!

JPGExtra to the rescue

JPGExtra (icon)

I've written a tiny but extremely powerful – and FREE – metadata removal program called JPGExtra, which optimizes JPG files by cleanly removing all the hidden extras, reducing their overhead to an absolute minimum.

In addition, JPGExtra removes those extras without any re-compression of the JPG image data, which means that there will be no further loss of image quality.

JPGExtra Program on Ian's Software Site.

PNGExtra for PNG files

JPGExtra (icon)

In the same way that my JPGExtra program optimizes JPG files with no loss of image quality, my PNGExtra program similarly optimizes PNG files by cleanly removing “extras” (or “metadata”).

PNGExtra Program on Ian's Software Site.

Graphics Optimization Summary

This page has covered the following:

  • Selecting the right file format for the type of image;
  • Choosing a suitable number of colors (for PNG or GIF files);
  • Choosing a quality setting (for JPG files);
  • Ensuring that solid areas in JPG files align on 8×8 blocks;
  • Removing “extras” (or “metadata”) from the completed files.

In future, I intend to delve into the complexities of optimizing animated GIFs.