Page 1 of 1

Converting & setting the background transparent

Posted: 2009-09-13T16:23:27-07:00
by jzaun
I have several thousand images in windows bmp format. I would like to convert them to png. This in itself is easy to do but I would like to set the background to transparent. the problem is not all images use the same background color so I would like to set the color in the top left pixel to be transparent. Is there a way to accomplish this with ImageMagick?

Re: Converting & setting the background transparent

Posted: 2009-09-13T16:28:38-07:00
by fmw42
one way is as follows using the IM internal logo: image which converts all pixels will the exact color of pixel 0,0 to transparent

convert logo: -channel rgba -alpha on -fill none -draw 'color 0,0 replace' logo_transparent.png

just replace the logo: with yourimage.bmp and the output with whatever.png

Another way, if you want to make all colors near to the color at pixel 0,0 transparent is to get the color of pixel 0,0 and then use -fuzz to translate all colors near that color to transparent. Here is an example where all colors within 1% of the color at 0,0 will be made transparent

color=`convert logo: -format "%[pixel:s.p{0,0}]" info:`
convert logo: -channel rgba -alpha on -fuzz 1% -fill none -opaque $color log_transparent1.png

see -fuzz at http://www.imagemagick.org/script/comma ... s.php#fuzz
see -draw at http://www.imagemagick.org/Usage/draw/#color
see fx escapes at http://www.imagemagick.org/Usage/transform/#fx_escapes
and http://www.imagemagick.org/script/fx.php

Note I believe that you can also add -fuzz to the first method