composite command line masking not working

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?".
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

composite command line masking not working

Post 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.
Last edited by chaoscarnage on 2013-01-01T08:38:50-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: composite command line masking not working

Post 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.
snibgo's IM pages: im.snibgo.com
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

Post by chaoscarnage »

How can you tell and how can I fix it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: composite command line masking not working

Post 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.
snibgo's IM pages: im.snibgo.com
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

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

Re: composite command line masking not working

Post 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.
snibgo's IM pages: im.snibgo.com
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite command line masking not working

Post by fmw42 »

You can add a third (mask) image in the composition. see
http://www.imagemagick.org/Usage/compose/#compose
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

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

Re: composite command line masking not working

Post 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.
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

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

Re: composite command line masking not working

Post 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
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: composite command line masking not working

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

Re: composite command line masking not working

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

Re: composite command line masking not working

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