Set a transparent background using ImageMagick and the command line.

Suppose you have an image (PNG or JPG). This image has a white background, and I need to make this background transparent.

I tried with these examples:

  • convert original.png -background none transparent.png
  • convert original.png -background white -flatten -alpha off transparent.png

but without the desired results.

How can i do this?

IMPORTANT: using the convert command line.

+66
imagemagick png alpha-transparency
Feb 06 2018-12-12T00:
source share
8 answers

I am using ImageMagick 6.6.9-7 on Ubuntu 12.04.
What worked for me:

 convert test.png -transparent white transparent.png 

This changed all whites in test.png to transparent.

+79
Jun 20 '12 at 8:15
source share

I had the same problem. In which I need to remove a white background from jpg / png format using ImageMagick.

What worked for me:

1) Convert image format to png: convert input.jpg input.png

2) convert input.png -fuzz 2% -transparent white output.png

+45
May 10 '13 at 11:19
source share

This works for me:

 convert original.png -fuzz 10% -transparent white transparent.png 

where the less fuzz%, the closer to true white or vice versa, the more%, the greater the deviation from white becomes transparent.

+23
Jul 01 '14 at 1:07
source share

You can use this to make background transparent

 convert test.png -background rgba(0,0,0,0) test1.png 

The above gives a prefect transparent background

+9
Jan 29 '14 at 7:09
source share

Decision

 color=$( convert filename.png -format "%[pixel:p{0,0}]" info:- ) convert filename.png -alpha off -bordercolor $color -border 1 \ \( +clone -fuzz 30% -fill none -floodfill +0+0 $color \ -alpha extract -geometry 200% -blur 0x0.5 \ -morphology erode square:1 -geometry 50% \) \ -compose CopyOpacity -composite -shave 1 outputfilename.png 

Explanation

It is a little longer than simple answers, but it gives better results. : (1) The quality is superior to smooth alpha, and (2) only the background is removed as against the same color. (A "background" is defined as approximately the same color as the top left pixel, using a fill from the edge of the image.)

In addition, the alpha channel is also destroyed by half a pixel to avoid the halo. Of course, ImageMagick's morphological operations (yet?) Work at the subpixel level, so you can see that I inflate the alpha channel to 200% before blurring.

Comparison of Results

Here is a comparison of a simple approach ("-fuzz 2% -transparent white") compared to my solution when the ImageMagick logo . I flattened both transparent images onto a saddle brown background to make differences in the differences (click for originals).

The simple replacement of white as transparent doesn't always work Antialiased alphachannel and floodfill looks much better

Note that the beard of the master has disappeared in a simple approach. Compare the faces of the wizard to see how smooth alpha helps smoothly move in the background.

Of course, I fully admit that there are times when you might want to use a simpler solution. (For example: it’s a lot easier to remember, and if you convert to GIF, you are limited to 1-bit alpha anyway.)

mktrans shell script

Since it is unlikely you will need to re-type this command, I recommend wrapping it in a script. You can download the BASH script shell from github , which executes my proposed solution. It can run on multiple files in and contains useful comments if you want to customize things.

bg_removal script

By the way, ImageMagick actually comes with a script called "bg_removal" which uses the fill in the same way as my solution. However, the results are small because it still uses 1-bit alpha. Also, the bg_removal script is slower and a bit more difficult to use (it requires you to specify two different fuzz values). Here is an example of output from bg_removal.

The bg_removal script: has beard, but lacks antialiasing

+7
Jun 14 '17 at 10:57
source share

If you want to control the level of transparency, you can use rgba. where a is alpha. 0 for transparent and 1 for opaque. Make sure the final output file must have a .png extension for transparency.

 convert test.png -channel rgba -matte -fuzz 40% -fill "rgba(255,255,255,0.5)" -opaque "rgb(255,255,255)" semi_transparent.png 
+2
Jan 23 '15 at 19:52
source share

Using ImageMagick, this is very similar to the code and result of hackerb9, but this is a slightly simplified command line. It assumes the top left pixel is the background color. I just fill the background with transparency, then select the alpha channel and blur it and remove half the blurred area using a level of 50x100%. Then turn back all the channels and press it to brown. Level -blur 0x1-level 50x100% acts on the antialiases of alpha channel transparency boundaries. You can adjust the fuzz value, blur value and 50% level value to change the degree of smoothing.

 convert logo: -fuzz 25% -fill none -draw "matte 0,0 floodfill" -channel alpha -blur 0x1 -level 50x100% +channel -background saddlebrown -flatten result.jpg 

enter image description here

+2
Jun 14 '17 at 16:42 on
source share

Yeah. With the same problem too. Here is the command I ran and it worked perfectly: convert transparent-img1.png transparent-img2.png transparent-img3.png -channel Alpha favicon.ico

+1
Oct 14 '15 at 20:36
source share



All Articles