How to make an image's background transparent using ImageMagick

magick convert FILE.png -fuzz 4% -transparent "#ffffff" TRANSPARENT.png

How to make an image's background transparent using ImageMagick

Let us start with a canvas:

Onto our canvas, let us superimpose an image:
Blue diamonds with white background

The image's white background blocks much of our canvas. What if we wanted a transparent background?

It is easy with ImageMagick, a free image editing software package available under relatively permissible license terms.

The following tutorial and commands summary will help you install ImageMagick on your PC and make an image's white background transparent. Non-PC instructions (Mac OS X, iOS, Unix) are similar.

Step-by-step Instructions for PC

Time required: about 10 minutes

STEP 1 - Download and install ImageMagick. Go to the ImageMagick download page and install the release of your choice. If you are on a PC, it is easiest to use the Windows installer. Be sure to select the option "Add application directory to your system path" because it will be needed for the rest of the tutorial: imagemagick-options

STEP 2 - Download this image. Right-click the image below. Save it to your folder of choice. For the tutorial, we will save it to the default folder: Downloads. Blue diamonds with white background

STEP 3 - Open the Windows command prompt. To bring up your command prompt, open your Windows Start menu and search for "command prompt". (Alternatively, hit WINDOWS+R and enter "cmd.exe".) You should see something like this: Windows command prompt

STEP 4 - Navigate to your folder. Type this command into your Windows command prompt, then hit enter: cd Downloads
Windows command prompt with cd command
This command says, "Open the downloads folder." cd stands for change directory. Indicently, as an alternative to typing commands, you can also paste commands into the command prompt using right-click.

STEP 5 - Run ImageMagick. Run this command inside your Windows command prompt: magick convert orig-2.png -transparent "#ffffff" transparent.png Windows command prompt with magick command
This command says, "Use magick to convert convert our file orig-2.png. Invoke the -transparent option so that all white pixels, which are all pixels which have the the hexidecimal color #ffffff, become transparent. Name the new file transparent.png." Dashes (-) are commonly used to specify as options within command-line programs. Check ImageMagick's documentation for more details about convert.

STEP 6 - Alternatives. If you have trouble remembering #ffffff, use the equivalent keyword white. And if you ever get uneven results, try the -fuzz option, which will also make off-white areas transparent based on whatever percentage value you choose. If 4% does not work for you, experiment with 2%, 6%, or some other value. Here's an example:
magick convert FILE.png -fuzz 4% -transparent "#ffffff" TRANSPARENT.png

STEP 7 - Success. Check your folder. You should now see a file entitled "transparent.png." Here is the final product:
Blue diamonds with transparent background


This will make an image's white areas transparent:
magick convert FILE.png -transparent "#ffffff" TRANSPARENT.png

This will make an image's white and off-white areas transparent:
magick convert FILE.png -fuzz 4% -transparent "#ffffff" TRANSPARENT.png

You can also use the "white" keyword (synonym for "#ffffff") and play around with the percentage value to make more or fewer shades go transparent:
magick convert FILE.png -transparent white TRANSPARENT.png
magick convert FILE.png -fuzz 5% -transparent white TRANSPARENT.png

To reverse the effect, try this:
magick convert -flatten TRANSPARENT.png FILE.png


How did it go? Please do not hesitate to connect with me and share your feedback.

Lots of software programs could have done this job. Why did we choose ImageMagick? While ImageMagick is free to use for a wide range of purposes, that is not the end of the story.

Our next post will discuss "Why ImageMagick?" Using this post as a case study, we will dive into some of the other available options. We will also explore some of the implications of choosing open source versus proprietary software.

Wizard image used with permission from ImageMagick. Connect with them on Twitter at @ImageMagick.