Wikipedia:How to reduce colors for saving a JPEG as PNG

This MedLibrary.org supplementary page on Wikipedia:How to reduce colors for saving a JPEG as PNG is provided directly from the open source Wikipedia as a service to our readers. Please see the note below on authorship of this content, as well as the Wikipedia usage guidelines. To search for other content from our encyclopedia supplement, please use the form below:

Shortcut:
WP:HRCS

Sometimes, you find a drawing or similar image useful for a Wikipedia article, that was saved in the JPEG format while it should have been saved in the PNG format. JPEG is good for images where the colour changes fluidly throughout the image (e.g., a photograph), whereas PNG is good for images where there are relatively few colors and that should be uniform throughout (e.g., a drawing of a flag, a chart, or a map; note that sometimes SVG is better).

Just automatically converting to PNG will almost never produce a good image. It is likely that the resulting file will be even larger than the original JPEG, and will maintain its poor quality.

But if you have a good bitmap graphic editing program with which you are reasonably skilled, you can produce a good result. This tutorial uses the GIMP, but it can be used with most raster graphics editors.

Some background about the formats

When you need to save an image with thousands of colors or more (as is typical in "photographic" images with fine gradations of shade and tone) and you are concerned about filesize, your best bet is probably to save it as a JPEG file. JPEG's compression method takes advantage of the fact that most natural scenes have smooth gradients and soft edges, and so is optimized for that.

PNG, however, uses lossless compression, which means that it saves every pixel exactly as it was drawn, which is ideal for iconic images such as graphs, flags, maps, and cartoons and for images likely to undergo further editing. Its compression is optimized for images with large areas of identical color with sharp edges.

In addition, though, PNG format is very versatile. You can store images as PNG files while retaining more colors than the human eye can possibly distinguish, and can represent more colors and finer details than even JPEG, but only when saved in PNG's "true color" mode. However, when you save an image as a true color PNG, you tend to get overly large files, since its compression is not optimized for such images.

If you merely convert a JPEG to a PNG, your graphics program will likely produce one of these true color PNGs, and make a large file. You should convert such PNGs to "indexed" or "palette-based" mode before you upload them. Most raster graphics editors will let you convert from true color mode (also called "full color" or "RGB"), and to decide the number of colors you want in the resulting indexed image. How many colors you choose will depend upon the image--it must be fewer than 256, but often 16 or even fewer will work well. They may also ask if you want to use something called "error diffusion" or "dithering". You will probably want to turn this option off. Experiment until you get it a result you like. Some tools may let you count the actual number of different colors in a file.

For images containing smooth gradients, or a lot of detail, avoid moving to indexed mode, as this can lose information and cause banding artifacts. Instead, just eliminate JPEG artifacts as much as possible and convert to 24-bit PNG.

Delete similar colors

Once an image such as a map has been saved as JPEG inadvertently, it often contains neighboring colors that look the same to the human eye, but which are in fact different. PNG's compression method needlessly preserves these differences, and the result will be that saving that image may not yield the reduction in file size that you hoped for. To remedy this, you can use the method described below to make sure that there are no more colors in the image than you actually need.

Making an image that was incorrectly saved as JPEG fit for saving as PNG
Image:Tutorial-reducing-colors 01.png Image:Tutorial-reducing-colors 02.png Image:Tutorial-reducing-colors 03.png
Suppose you have got a map for an island that was inadvertently saved as JPEG. Looks OK, if a bit fuzzy. 1. In your bitmap graphics editor, set your fuzzy selection ("magic wand") tool so that it only will select pixels of exactly the same color. (Here: Threshold = 0) 2. Click on the main land mass: see how it does not select every pixel of the land mass? This is the result of saving as JPEG, and usually hardly noticeable to the human eye.
 
Image:Tutorial-reducing-colors 04.png Image:Tutorial-reducing-colors 05.png
3. Go back to the fuzzy select tool, and make it less restrictive. 4. As you can see, more of the land mass will be selected. Experiment to get all of the land, but none of the edge surrounding the island. You may have to manually clean up visible "speckles".

After you have the selection as shown above, choose an appropriate color for the land mass, and make sure the entire selected area is filled. How to do this may differ between applications. Once the image consists of the color of the sea, the color of the land and the intermediate colors of the edge between sea and land, you can reduce the number of colors to about sixteen and save as an indexed PNG. Again, experiment until you get it right.

The result of all of this effort is that you should get an image file that is smaller than the original image (and thus takes less download time) and which will be sharper and clearer (JPEG often leaves blurry edges and speckles in what should be smooth areas of color).

Of course, forcing your raster graphics editor to reduce the number of colors in this example to 16 may have the exact same result and take less time to do. This doesn't work, though, if there are visible defects from the JPEG process that you have to clean up first.

This article is here in the first place to make you aware that such tiny color differences exist, and show you how to deal with them. If you just count the number of colors in a JPEG and then base the number of PNG palette entries on this count, you will leave unnecessary information in the image.

Second, not all image editors will let you force the number of colors using the correct colors! Those that do often have a bewildering array of options to set before you get the right colors.

Related topics

Wikipedia content modification information:

  • This page was last modified on 18 April 2008, at 08:26.

Wikipedia Authorship and Review

Wikipedia content provided here is not reviewed directly by MedLibrary.org. Wikipedia content is authored by an open community of volunteers and is not produced by or in any way affiliated with MedLibrary.org.

Wikipedia Usage Guidelines

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article on "Wikipedia:How to reduce colors for saving a JPEG as PNG".

The URL for this specific entry is:

All Wikipedia text is available under the terms of the GNU Free Documentation License. (See Copyrights for details). Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.