Flattening composed image

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
honkifyoulikeme
Posts: 3
Joined: 2016-04-15T13:12:17-07:00
Authentication code: 1151

Flattening composed image

Post by honkifyoulikeme »

Hi there,

I'm using ImageMagick 6.8.9-9 on Linux. What I'm trying to do is compose two images, one with pixel data and the other with an alpha channel data (grayscale image) together. That works fine:

convert src.pnm \( alpha.pnm -colorspace gray -alpha off \) -compose copy-opacity -composite out.png

However, I also want to flatten the image afterwards with a background color and I cannot get that to work for the life of me. What works is if I run two convert commands (one writes a PNG, the other flattens). However, I want to directly write a PNM (no alpha). This all doesn't work (they all produce all-red images):

convert src.pnm \( alpha.pnm -colorspace gray -alpha off \) -compose copy-opacity -composite -background red -flatten out.pnm
convert src.pnm \( alpha.pnm -colorspace gray -alpha off \) -compose copy-opacity -background red -flatten out.pnm
convert -background red src.pnm \( alpha.pnm -colorspace gray -alpha off \) -compose copy-opacity -flatten out.pnm

Any ideas? Thank you so much. Cheers!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Flattening composed image

Post by fmw42 »

Put -compose over before -flatten. The -flatten is use the last compose method (copy_opacity) rather than over. It needs to be reset after the compose copy_opacity.

Note you also need -composite after copy_opacity.

Code: Select all

convert src.pnm \( alpha.pnm -colorspace gray -alpha off \) -compose copy-opacity -composite -background red -compose over -flatten out.pnm
Post Reply