Composite images with a mask

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
tcdaly
Posts: 3
Joined: 2013-07-10T08:58:34-07:00
Authentication code: 6789

Composite images with a mask

Post 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.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite images with a mask

Post by GreenKoopa »

convert -size 128x128 canvas:none canvas:#231F20 mask.png -composite output.png
tcdaly
Posts: 3
Joined: 2013-07-10T08:58:34-07:00
Authentication code: 6789

Re: Composite images with a mask

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite images with a mask

Post 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.
snibgo's IM pages: im.snibgo.com
tcdaly
Posts: 3
Joined: 2013-07-10T08:58:34-07:00
Authentication code: 6789

Re: Composite images with a mask

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite images with a mask

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply