User:Trlkly/essays/How to remove solid backgrounds from images

How to remove solid-colored backgrounds from images
Using the Gimp, magic select all the background areas (hold down shift to get more than one). Use Select > Grow by one pixel, and see if only the opaque parts of the image are unselected. If there's still a fringe around it, you can grow the selection again, or use the magic select or quick mask to select better.

Once only the opaque portions are NOT in the selection, use the Eye dropper to pick up the background color, and double click on the palette to see the html color number. Close the dialog, and open Color > Color to Alpha. Click on the color to get a dialog, and change the html color number to the one you got from the eye dropper. Click the button, and, voila! the background is transparent.

If Color to Alpha is greyed out, you are dealing with an index colored picture. Click Image > Mode > RGB to fix this.

If you find that, after using magic select, you have lots of pixels in the background left, you are likely dealing with a poorly compressed JPEG. You can try increasing the magic select threshold, or you can magic select the images, or use the quick mask. Then, before you grow anything, right click on the layer and add an alpha channel, then delete this portion of the image. Then continue as above, followed by using whatever technique you want to correct other JPEG artifacts.

Before going to all this trouble, be sure you have the best version available of the image for PNG conversion. Quite often there's a larger image, high color PNG, or even 98% quality JPEG available that will work a lot better. And don't be afraid to resize the image before doing the work; shrinking it will remove color artifacts, making a GIF usable and increasing the usability of a bad JPEG. Consider the size of the image as actually used in the article (especially if it's a fair use image). Note, if it's a thumbnail, then only reduce the image to 300px width or greater, as that is the maximum thumbnail size.

File size reduction

So you've fixed that unsightly background color of that JPEG, and even removed perceiveable artifacts. But the resulting PNG is still much larger, to the point that it will slow down page load significantly for some users. Don't fret: you still can apply file size reduction methods.

Before you begin, make sure your image is the size used in the article. This is important, as, if it is not, Wikimedia will make its own PNG, and you'll lose much of the optimizations you have made.

If the file is only, say, 10K too large, your first step should be to try to optimize it. There are several programs out there to help, but I find the easiest is to let someone else do it. Go to http://pngoptimizer.com and upload your image.

If the file is larger than that, you'll need to take other methods, saving optimization for last. The easiest method is to posterize the image with your image editor. A free choice is the GIMP. Make sure there is nothing selected, and go to Color > Posterize and increase the levels until the difference from the original is acceptable. I usually shoot for looking imperceptible at 150% magnification, other than maybe a stray pixel or two. Other programs have the option elsewhere, but most decent image editors have it.

If this does not reduce the file size enough, we can get more drastic. A free image editor known as Image Analyzer has the ability to do lossy PNG compression, at the cost of losing some color definition. The editor's interface is a little different than usual, so allow me to walk you through how to use it for this purpose.

Use File > Open or drag your PNG onto the window to open. You will see a prompt saying, "Merge alpha channel with picture?" Choose "No," which will result in another image opening, containing your the alpha transparency channel. Neitehr window will look entirely like the original image.

Making sure the one that the first window you saw is the active one (it's the colorful one unless you are dealing with a black and white image), click File > File Format Options or press F12. In the dialog box that appears, check "Use Alpha transparency." The other image should be in the dropdown. Then adjust the slider to the left and click the Test button to try out the different levels, as well as be told how big the file will be. When you've found a suitable option, click the Close button, then save the file. I suggest saving it with a different file name, and then comparing the new file with the older one in another image editor or viewer.

There's another method of file size reduction I haven't mentioned yet, one that is really good for images that don't have too many colors in them. This is convert the image to indexed colors. Unfortunately, few programs out there handle this well in images that have alpha transparency. I have found two command line programs that work well.