Converting & setting the background transparent

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
jzaun

Converting & setting the background transparent

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting & setting the background transparent

Post 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
Post Reply