Page 1 of 1

Flattening composed image

Posted: 2016-11-17T10:07:30-07:00
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!

Re: Flattening composed image

Posted: 2016-11-17T10:29:10-07:00
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