Page 1 of 2

composite command line masking not working

Posted: 2012-12-31T16:05:09-07:00
by chaoscarnage
I've been using the composite command to mask images and have run into a strange issue where an image I have is not being masked properly. I have tested the mask with other images and the mask has worked fine. Because of this, I think the issue lies with the source item being masked. I cannot for the life of me figure out why I get a blank image on output with this combination.


The command I'm using to test

Code: Select all

composite item.png canvas.png mask.png out.png
Any help would be greatly appreciated as I have no clue as to why it is failing. Thank you for your time.

Re: composite command line masking not working

Posted: 2012-12-31T18:00:23-07:00
by snibgo
"Item" has a mean alpha of < 1%, and canvas has alpha=0. So you are compositing a very nearly transparent image over an entirely transparent image. I would expect the output to be almost entirely transparent.

Re: composite command line masking not working

Posted: 2012-12-31T18:34:05-07:00
by chaoscarnage
How can you tell and how can I fix it?

Re: composite command line masking not working

Posted: 2012-12-31T18:45:44-07:00
by snibgo
I know because I read your verbose listings. For example, near the top of "Item", "Alpha" mean is 1.84609 (out of 255). The colours are about 253. So the image is nearly white, but also close to totally transparent.

"Canvas" is a very boring image. All the pixels are rgba(255,255,255,0). (I can tell by looking at the min and max of the four channels.) Every pixel is transparent white.

How can you fix it? Well, I don't know what you are trying to do.

Re: composite command line masking not working

Posted: 2012-12-31T19:34:09-07:00
by chaoscarnage
snibgo wrote:I know because I read your verbose listings. For example, near the top of "Item", "Alpha" mean is 1.84609 (out of 255). The colours are about 253. So the image is nearly white, but also close to totally transparent.

"Canvas" is a very boring image. All the pixels are rgba(255,255,255,0). (I can tell by looking at the min and max of the four channels.) Every pixel is transparent white.

How can you fix it? Well, I don't know what you are trying to do.
The desired result is the boot with a little of its edge clipped by the mask. Right now I am getting a blank image. Thank you for the quick response and explanation.

Re: composite command line masking not working

Posted: 2012-12-31T21:00:26-07:00
by snibgo
I rarely use the "composite" command; only when "convert" won't do the job I want. In this case, convert may do what you want:

Code: Select all

convert canvas.png item.png mask.png -composite out.png
(Note the order of canvas and item are inverted, compared to the "composite" command.)

This makes the top-left corner (about 3 pixels) of the boot transparent.

Re: composite command line masking not working

Posted: 2013-06-14T13:11:42-07:00
by chaoscarnage
This works for strict on off masking. What about gradient masking? Ie Instead of black and white for 0 or 100% I want to include gray for the inbetweens(alpha) like 33%, 27%, 77% etc. That Way we could accomplish something similar to this post Here but in command line.

Re: composite command line masking not working

Posted: 2013-06-14T13:18:32-07:00
by fmw42
You can add a third (mask) image in the composition. see
http://www.imagemagick.org/Usage/compose/#compose

Re: composite command line masking not working

Posted: 2013-06-14T13:57:12-07:00
by chaoscarnage

Code: Select all

convert canvas.png item.png mask.png -composite out.png
We already have a mask in place but it does not account for graysacle in the mask image only strict on or off.

Re: composite command line masking not working

Posted: 2013-06-14T14:04:14-07:00
by GreenKoopa
The method needed would depend on the images you are working on. Looking at that stackoverflow post, there is a source/background image in RGB and a alpha/mask image in RGBA. The mask image is entirely black, only the alpha channel varies. Are these images the actual ones you are trying to combine? Knowing the character of all input images is necessary to give advice.

Re: composite command line masking not working

Posted: 2013-06-14T14:14:45-07:00
by chaoscarnage
GreenKoopa wrote:The method needed would depend on the images you are working on. Looking at that stackoverflow post, there is a source/background image in RGB and a alpha/mask image in RGBA. The mask image is entirely black, only the alpha channel varies. Are these images the actual ones you are trying to combine? Knowing the character of all input images is necessary to give advice.
Either way is fine we're just trying to get one that works, currently we use a RGB source and a black and white only image as the mask. If possible we would like to use a white->black gradient or transparent to black mask. We would prefer whichever way works or is better.

Re: composite command line masking not working

Posted: 2013-06-14T14:33:48-07:00
by GreenKoopa
Since you didn't provide input images, I'll generate my best understanding of them.

Code: Select all

convert rose: -resize 300x200! source.png
convert -size 400x400 radial-gradient:white-black -gravity center -crop 300x200+0+0 mask.png

convert source.png mask.png -compose CopyOpacity -composite result.png

Re: composite command line masking not working

Posted: 2013-06-14T14:56:16-07:00
by chaoscarnage
GreenKoopa wrote:Since you didn't provide input images, I'll generate my best understanding of them.

Code: Select all

convert rose: -resize 300x200! source.png
convert -size 400x400 radial-gradient:white-black -gravity center -crop 300x200+0+0 mask.png

convert source.png mask.png -compose CopyOpacity -composite result.png
Will it still work if we keep the canvas.png or is that not possible?

Re: composite command line masking not working

Posted: 2013-06-14T15:10:31-07:00
by GreenKoopa
canvas.png is a background? I am assuming that all of these input images are the same size.

Code: Select all

convert -size 300x200 gradient:silver-blue canvas.png
convert rose: -resize 300x200! source.png
convert -size 400x400 radial-gradient:white-black -gravity center -crop 300x200+0+0 mask.png

convert source.png mask.png -compose CopyOpacity -composite canvas.png +swap -compose  Over -composite result.png

Re: composite command line masking not working

Posted: 2013-06-14T15:35:13-07:00
by snibgo
A grey-scale mask should work, eg:

Code: Select all

convert -size 100x100 xc:red xc:green gradient: -composite c.png
The result should be a gradient from red to green.