Page 1 of 1

Composite images with a mask

Posted: 2013-07-10T09:17:00-07:00
by tcdaly
Hello,

I'm trying to create a variation of the command that can composite an image over a background, through a mask, as described here:

http://www.imagemagick.org/Usage/compose/#mask

Instead of use two images saved on disc as the source images, I would like to create the source images in memory. I would like the
background to be a transparent canvas, and the foreground to be a solid canvas that shows through my mask (which is saved on disc
as 'mask.png'. This is what I have tried, but it generates a 'convert: missing an image filename' error:

convert -size 128x128 -alpha transparent canvas: \
-size 128x128 -background '#231F20' canvas: \
mask.png -composite output.png

Any help gratefully received.

Re: Composite images with a mask

Posted: 2013-07-10T09:36:48-07:00
by GreenKoopa
convert -size 128x128 canvas:none canvas:#231F20 mask.png -composite output.png

Re: Composite images with a mask

Posted: 2013-07-11T02:28:24-07:00
by tcdaly
Thank you for your reply. I get the following errors from the command given:

convert: unable to open image `canvas:none': @ error/blob.c/OpenBlob/2489.
convert: unable to open image `canvas:#231F20': @ error/blob.c/OpenBlob/2489.
convert: missing an image filename `output.png' @ error/convert.c/ConvertImageCommand/2940.

I have ImageMagick version 6.6.0-4.

Re: Composite images with a mask

Posted: 2013-07-11T03:24:21-07:00
by snibgo
Your version of IM is very old and doesn't recognise "canvas:". I suggest you upgrade. However, you can use "xc:" instead of "canvas:".

But that command may not do what you want. Perhaps ...

Code: Select all

convert -size 128x128 xc:#231F20 mask.png -compose CopyOpacity -composite c.png
... does.

Re: Composite images with a mask

Posted: 2013-07-14T09:31:26-07:00
by tcdaly
Thank you. The command I used, essentially yours, which works is

convert -size 128x128 xc:#231F20 mask.gif -alpha Off -compose CopyOpacity -composite c.png

This assumes mask.gif is a 128x128 greyscale image. Not sure if 'alpha Off' is actually necessary, but the result is a shape on a transparent background, aliased with semi-transparent pixels, which is what I wanted.

Re: Composite images with a mask

Posted: 2013-07-14T10:18:06-07:00
by snibgo
That's good.

"-compose CopyOpacity -composite" will use the mask's opacity, if has opacity. If it doesn't have opacity, it will use the tones to create opacity.

Using "-alpha off" switches off any opacity (if the file has any), thus forcing "-compose CopyOpacity -composite" to use the tones.