Opaque image to transparent PNG cmd line

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
capncrunch9834
Posts: 13
Joined: 2012-06-11T07:39:40-07:00
Authentication code: 13

Opaque image to transparent PNG cmd line

Post by capncrunch9834 »

Can someone please tell me what I'm doing wrong in the following (Windows) command line? I've read manuals, posts, and tweaked until I'm rgb(0,0,255) in the face.
The original image is a rounded rectangle (with a silver background) that I need to remove the background from, but I keep ending up with a 256color image with a black background instead of a high color transparent png.

Code: Select all

convert "ImageIN.BMP" -alpha set -channel RGBA -colorspace RGB -fuzz 50% -fill none -floodfill +0+0 rgb(192,192,192) -shave 1x1 "ImageOUT.PNG"
I've tried many variations of the above command and, at some point ACTUALLY HAD IT WORKING. It looked GREAT! Along the way I somehow BROKE IT, and didn't know it!?! :? I haven't figured out how to correct the syntax so I've gathered up enough humility to ask those who are more experienced... for help. :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

on windows % needs to be escaped to %%. you should also quote color specifications other than colornames.

-fuzz 50% --> -fuzz 50%%

You did not say what version of IM you are using. But this should be adequate

convert "ImageIN.BMP" -fuzz 50%% -fill none -floodfill +0+0 "rgb(192,192,192)" -shave 1x1 "ImageOUT.PNG"
capncrunch9834
Posts: 13
Joined: 2012-06-11T07:39:40-07:00
Authentication code: 13

Re: Opaque image to transparent PNG cmd line

Post by capncrunch9834 »

Thanks for your quick reply. I'm using version 6.7.7-6.

I tried the command you suggested, (the syntax corrections make perfect sense, thank you), however I still end up with an opaque background, at 256colors, no alpha channel.

I've reverted back to the simplest of examples, and those examples shown in the docs, and I still end up with a background. Transparency just simply is not getting set.

Code: Select all

Ex.
convert "E:\Long Folder Name\ImageIN.BMP" -transparent white "E:\Long Folder Name\ImageOUT.PNG"
What is the correct way to construct the command line to convert a flat BMP to a transparent PNG (hi-color, with alpha)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

try adding PNG32: to the beginning of your output image (no spaces) PNG32:output.png

try this.

convert logo: logo.bmp
convert logo.bmp -transparent white PNG32:logo.png


It works fine for me to make it transparent on IM 6.7.7.7 Q16 Mac OSX Snow Leopard, but it is still paletteAlpha (256 color limited). I am not sure you can force IM to make 32bit color, if your input image starts as palette (256 colors).
Last edited by fmw42 on 2012-06-11T11:44:18-07:00, edited 1 time in total.
capncrunch9834
Posts: 13
Joined: 2012-06-11T07:39:40-07:00
Authentication code: 13

Re: Opaque image to transparent PNG cmd line

Post by capncrunch9834 »

The logo test worked. And you were correct about my input image being 24bit. But what I am confused about (and frustrated with myself about) is that I had this working at one point all in one shot... with the same input image!

I'm not sure why yet, but if I hit it twice it works and I end up with a 32bit alpha transparency.

Code: Select all

convert "E:\Folder\ImageIN.BMP" -fuzz 50%% -fill none -floodfill +0+0 "rgb(192,192,192)" "PNG32:E:\Folder\ImageOUT.PNG"
convert "E:\Folder\ImageOUT.PNG" -alpha set -channel RGBA -colorspace RGB -fuzz 20%% -fill none -floodfill +0+0 black -shave 1x1 "PNG32:E:\Folder\ImageOUT2.PNG"
Above, the first pass results in the black background I've been trying to avoid, the second pass removes the black background and creates the transparency.

I did BOTH in one single command ... AND had nicely smoothed edges. The above still leaves me with jagged edges.

(I'm sooo frustrated with my @#$% self. I had a "jewel" of a command line... AND LOST IT??? ) :lol:

Do you have any suggestions as to how I can combine the separate commands above into one and how to deal with the anti-aliasing?

THANKS
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

Post a link to your example bmp, so I can test it. If your bmp is 24-bits (and not palette) to start, then IM should not down-convert it to palette. So by making it transparent, you should get 32-bits. Let me see what is going on. You can also use -draw to floodfill transparency. see http://www.imagemagick.org/Usage/draw/#matte
capncrunch9834
Posts: 13
Joined: 2012-06-11T07:39:40-07:00
Authentication code: 13

Re: Opaque image to transparent PNG cmd line

Post by capncrunch9834 »

Hey, thanks!

Here are 2 images, One with a silver background, the other white...

ImageIN.bmp
Image2IN.bmp

I appreciate your help... Enormously!

(I thought matte was deprecated by -alpha set ??)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

Note that since IM 6.7.5.5 probably through 6.7.6.7, colorspaces RGB and sRGB have undergone changes. Before that, RGB meant sRGB (non-linear) and sRGB (meant linear). Afterwards it was corrected so RGB was linear and sRGB was non-linear. So if you are now using a current IM version and change to -colorspace RGB you are now converting your image to linear colors. But rgb(192,192,192) is likely sRGB (non-linear), and so may not match your image any longer since you made it linear.

By using -draw, you don't have to specify the color to change, only its location and IM will determine the color. So it is a bit simpler to use than -floodfill.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

Both your images are palette with colors less than 256. So IM is going to make them paletteAlpha and thus jagged. The only way to make it smooth is to create a binary mask from your image and

This works for me to get the outside area around the blue to be transparent.

convert ImageIN.bmp -fuzz 10% -fill none -draw "matte 0,0 floodfill" ImageIN.png

You can adjust the -fuzz value and be sure to make it %% on windows.


One way to make it smooth, I think, is to create a binary mask from your image and feather it using -blur X -level 50x100% (or 0x50% depending upon where is white and black) and then apply the mask as an 8-bit alpha channel.

Another way is to force your image to be 24-bit by converting to jpg (though it has some loss).

convert ImageIN.bmp -quality 100 ImageIN.jpg
convert ImageIN.jpg -fuzz 10% -fill none -draw "matte 0,0 floodfill" ImageIN.png

You can do it all in one step as

convert ImageIN.bmp -quality 100 JPG:- | convert - -fuzz 10% -fill none -draw "matte 0,0 floodfill" ImageIN.png

Though this is unix with PIPE and I do not know if Windows supports piping.
capncrunch9834
Posts: 13
Joined: 2012-06-11T07:39:40-07:00
Authentication code: 13

Re: Opaque image to transparent PNG cmd line

Post by capncrunch9834 »

You're a Rock Star, Fred! That works...!!!

Code: Select all

convert ImageIN.bmp -quality 100 JPG:- | convert - -fuzz 10%% -fill none -draw "matte 0,0 floodfill" ImageOUT.png
I wish I knew what I had stumbled onto (before I butchered it like a n00b), but even if I could remember, it was no where close to as elegant as your single line command. Pure genius. You made my day.

Thank You!!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Opaque image to transparent PNG cmd line

Post by fmw42 »

Your original method should work, but I think the colorspace change is what may have been the issue, so that your background color is not the value you think after you put in -colorspace RGB. But I have not tested it. Nevertheless, I don't see how it would have converted from palette to 24 bit color, so your transparency would have had a jagged edge, unless you did some other processing, such as feathering a mask.

Testing this:

convert Image2IN.bmp -fuzz 50% -fill none -floodfill +0+0 "rgb(192,192,192)" Image2INa.png

I also get a black background. But the following two commands work. The above should work also and may be a bug as the developers have been trying to enable transparency when using -fill none.

convert Image2IN.BMP -alpha set -channel rgba -fuzz 50% -fill none -floodfill +0+0 "rgb(192,192,192)" Image2Inb.png

convert Image2IN.BMP -channel rgba -fuzz 50% -fill none -floodfill +0+0 "rgb(192,192,192)" Image2Inc.png
Post Reply