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

From Wikipedia, the free encyclopedia
  (Redirected from Wikipedia:HRCS)
Jump to: navigation, search
Shortcut:

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 color 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[edit]

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. Using the posterize option of your graphics editor with a high setting (like 40) may further reduce filesize and eliminate more artifacts without visibly reducing image quality.

To further reduce file size, you can use one of the optmization programs mentioned at the Wikipedia graphics tutorial page.

Delete similar colors[edit]

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
Tutorial-reducing-colors 01.png
Tutorial-reducing-colors 02.png
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.

 
Tutorial-reducing-colors 04.png
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 exactly the 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.

Flood-fill method: Many image editors support a flood-fill tool that allows for a tolerance setting, i.e. similar colors to the one under the tool at time of execution will also be included. To create single-color areas, the flood fill tool is often the easiest tool. First select an odd color (e.g. pink if there is no pink in the image) and experiment to find the right tolerance setting. Undo each time. Now you pick a main area color (try control-click while flood-fill is activated) and fill by clicking into each contiguous area. Note: this works well for areas, but less so for lines. Careful reduction of the number of colors after treating the areas will often give best results.

Special methods and tips[edit]

Using Color to Alpha[edit]

If an image is made up of only two discrete colors (or one color and transparency), it is possible to even remove artifacts at the edges with GIMP's "Color to Alpha" tool. First make sure your image has an alpha channel. Duplicate the original layer, but make it invisible. If the background should not be transparent when you are finished, create another new layer, and choose "Alpha to Selection" from the layer menu. Deselect any part that is supposed to remain transparent, and then shrink the selection by one. Use the color picker and fill with the background color.

Either way, use the color picker to select the foreground color (being sure sample an average) and select the background color with your favorite tool and press delete. Then either grow the selection by one or two pixels (using the Selection menu) or select the main color and invert the selection. The idea is to select both the newly transparent portions and the fringe around the foreground color. Use the color to alpha tool with the background color to remove the fringe.

Now cancel the selection, go to the Channels panel and deselect the Alpha channel, then go back to the layers panel. Use the fill tool to fill the selection with the main color. (Since you turned off alpha, this will not affect the transparent section.) Go back and reselect the alpha channel to go back to regular editing.

Finally, compare the image with the original. Make sure there are no holes in the background layer (if you have one). If the new version is too sharp, you can utilize the blur filter or tool to fix this, or use Colors > Levels to mess with the Alpha channel. Once it looks correct, save the image as a PNG. Then, to reduce file size, run it through a program like ImageMagick that supports 8-bit PNGs with alpha transparency, in order to remove any unnecessary colors. (This may or may not be done automatically by a PNG optimizer, as well.)

Related topics[edit]