Convert BMP to transparent PNG

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
Markus Breitinger
Posts: 2
Joined: 2014-05-15T23:41:04-07:00
Authentication code: 6789

Convert BMP to transparent PNG

Post by Markus Breitinger »

I have a collection of Windows BMP files which use the color of upper left corner to define the transparent background. The target should be PNG files.

I tried:
convert MOT01_MOT_AP_GROSS_AUS.bmp -transparent %[pixel:p{0,0}] MOT01_MOT_AP_GROSS_AUS.png

but somehow it is wrong. Could you please help me!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert BMP to transparent PNG

Post by snibgo »

Sadly, "-transparent" needs an actual colour, and can't accept a "pixel:" expression.

I don't think this can be done in a single convert. We can do it in two converts. The first gets the colour. For example, Windows BAT format:

Code: Select all

for /F "usebackq" %%C ^
in (`%IM%convert x.bmp -format "%%[pixel:p{0,0}]" info:`) ^
do set ONE_PIXEL=%%C

%IM%convert x.bmp -transparent %ONE_PIXEL% x.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert BMP to transparent PNG

Post by fmw42 »

there is a way to do the equivalent in one command. you can even add -fuzz XX% if you want

Code: Select all

convert image.bmp -fill none -draw "matte 0,0 replace" newimage.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert BMP to transparent PNG

Post by snibgo »

Ah, brilliant, thanks. I thought I might be forgetting something.
snibgo's IM pages: im.snibgo.com
Markus Breitinger
Posts: 2
Joined: 2014-05-15T23:41:04-07:00
Authentication code: 6789

[Solved] Re: Convert BMP to transparent PNG

Post by Markus Breitinger »

Thank you very much! Works great.
Post Reply