Page 1 of 1

How to make background color transparent for palette type im

Posted: 2007-06-18T19:31:14-07:00
by daroepfin
I have many bmp images that need to be converted to palette type png images. They have different background colors, and I want that color to be transparent in the png images. Here's what I tried:

In Java, load the image and lookup the color of the top left pixel. Then run:

Code: Select all

convert image.bmp -type Palette -channel Index -transparent-color "lookedUpPixelColor" image.png
I tried calling this, with an image whose background color is #585C38, and the result is a Palette type png with no transparency.

Any ideas for what I'm doing wrong?

Any ideas for how to do this without using Java to read the first pixel? I read other posts and found that I can get the first pixel by cropping and then calling -text:- or something, but that wouldn't save me time because then I need to write code to parse out the pixel color and input that text into the above line.

There must be an easier way to set an image's background as transparent :)

Much thanks in advance,
Jeff

Re: How to make background color transparent for palette type im

Posted: 2007-06-18T19:55:03-07:00
by anthony
-transparent-color does not modify an image! All it does is define what color should be used in the 'pallete' in the 'transparent color index. That is the color hidden behind the 'transparent areas'.

To actually make a color transparent use -transparent

Anything that is transparent will assigned the 'transparent color index' which has the color defined by the transparent-color.

Note you can quite easilly have a transparent-white color and a opaque-white color in the exact same image, which is why this operators are does what they do.

These are related functions but very different.

See IM Examples, Common color Formats, GIF transparency color.
http://www.imagemagick.org/Usage/formats/#gif_tran

Re: How to make background color transparent for palette type im

Posted: 2007-06-18T20:01:28-07:00
by anthony
daroepfin wrote:Any ideas for how to do this without using Java to read the first pixel? I read other posts and found that I can get the first pixel by cropping and then calling -text:- or something, but that wouldn't save me time because then I need to write code to parse out the pixel color and input that text into the above line.
not text: but txt: see http://www.imagemagick.org/Usage/files/#txt

Code: Select all

   convert image.png -crop  1x1+0+0 txt:-
or you can use the pixel color escape...
convert image.png -format '%[pixel:u]' info:
Which is guranteed to be usable as a IM color input argument.
See IM Examples, FX Format escapes...
http://www.imagemagick.org/Usage/transform/#fx_escapes

Re: How to make background color transparent for palette type im

Posted: 2007-06-19T19:16:12-07:00
by daroepfin
Thanks for the replies!

I'm trying anything I can think of with no luck. Here are my trials:

Code: Select all

convert image.bmp -type Palette -channel Index -transparent "topLeftPixel" image.png
mogrify image.png -transparent-color #00000000
Result: first line generates the palette png. no transparency.
second line prints: mogrify: option requires an argument `-transparent-color'

Code: Select all

convert image.bmp -type Palette -channel Index -transparent "topLeftPixel" -transparent-color #00000000 image.png
Result: palette png. no transparency.

Code: Select all

convert image.bmp -transparent "topLeftPixel" image.png
Result: png. no transparency.

Do I need to get my first mogrify working in order to actually see the transparency?

I also tried this:

Code: Select all

convert image.bmp -transparent '%[pixel:s.p{0,0}]' image.png
Result: convert: unrecognized color `'%[pixel:s.p{0,0}]''.

Thanks,
Jeff

Re: How to make background color transparent for palette type im

Posted: 2007-06-19T19:47:17-07:00
by anthony
daroepfin wrote:Result: first line generates the palette png. no transparency.
You need to use the output of the code I provided as the input for the second command!
daroepfin wrote:second line prints: mogrify: option requires an argument `-transparent-color'
mogrify needs all image filenames at the END of the command line options.
daroepfin wrote:I also tried this:

Code: Select all

convert image.bmp -transparent '%[pixel:s.p{0,0}]' image.png
Result: convert: unrecognized color `'%[pixel:s.p{0,0}]''.
Now that is trying to do something that IM is only just starting to allow.
You can not (yet) use an argment basied on images that have been read in.

You must use two commands. one to get the color. The other to use that result as an argument for the color to make transparent.

Yes it should be posible to do in one command, but not yet.

Re: How to make background color transparent for palette type im

Posted: 2007-06-20T08:16:33-07:00
by daroepfin
(Very cool that ya'll are making this stuff possible!)

In my first command, what I actually did was open GIMP to see which color needs to become transparent, #585c38, and I used that as the input.

Code: Select all

convert image.bmp -type Palette -channel Index -transparent '#585c38' image.png
This doesn't seem to work... at home... because just now I did it again (I'm at work) with a different image, and it works fine. (yay!) Perhaps my home bmp is formatted differently... I dunno, I'll investigate.

For retreiving the color, I'll just use Java, and eventually use the FX notation when it's easily usable in one command instead of two. Using the -format option won't work because I have hundreds of images to do this to, and from what I can tell, info: spits out text that I would need to parse programmatically. (doable)

Thank you very much for the help,
Jeff

Re: How to make background color transparent for palette type im

Posted: 2007-06-20T16:16:30-07:00
by anthony
more likely one system is a Q16 system that saves images using 16 bit intergers. Compile time option.

Add a small 'fuzz factor' -fuzz 1%
http://www.imagemagick.org/Usage/color/#fuzz

The point of %[pixel:...] in -format is that it returns a color that IM can use directly
as a color argument. No need to parse it.