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
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.
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.
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".
Full article ▸