Composit 3 images using a single RGB 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?".
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Composit 3 images using a single RGB mask

Post by blurymind »

Is that possible to do with imagemagick? If so how?

basically I have three images which I want to composit together - image1.png, image2.png and image3.png.
And I have an extra RGB mask image that contains three masks - the red, the green and the blue channels are each used as a mask - rgbMask.png

so 4 images in total!

Out of that I want to create a result - Result.png that is composited of the three images like this:

image1.png uses the red channel of rgbMask.png
image2.png uses the blue channel of rgbMask.png
image3.png uses the green channel of rgbMask.png

How do I go about doing that?
The examples of the website show only how to do it with a single black and white mask
http://www.imagemagick.org/Usage/compose/#mask
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composit 3 images using a single RGB mask

Post by fmw42 »

try (unix syntax)

Code: Select all

convert \
\( rgbmask.png -channel r -separate +channel image1.png \
+swap -alpha off -compose copy_opacity -composite \) \
\( rgbmask.png -channel g -separate +channel image2.png \
+swap -alpha off -compose copy_opacity -composite \) \
\( rgbmask.png -channel b -separate +channel image3.png \
+swap -alpha off -compose copy_opacity -composite \) \
-flatten result.png
If order is important arrange them appropriately. Last image will be composited over the previous ones or use -swap or -reverse to switch images.
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

Hi,
Is it possible to do this in normal windows CMD terminal?

I cant use unix terminal on this system. :(

The syntax doesnt work here
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Composit 3 images using a single RGB mask

Post by Bonzo »

Probably but you would need to convert it. I would try this and see what happens:

Code: Select all

convert ( rgbmask.png -channel r -separate +channel image1.png +swap -alpha off -compose copy_opacity -composite ) ( rgbmask.png -channel g -separate +channel image2.png +swap -alpha off -compose copy_opacity -composite ) ( rgbmask.png -channel b -separate +channel image3.png +swap -alpha off -compose copy_opacity -composite ) -flatten result.png
This is all one long line; from memory you can use ^ to concate lines if you want to?
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

@Bonzo That worked and actually produced a file - but the file was an empty white canvas instead of what I expected.
Also there was an error message:

Code: Select all

convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `
result.png' @ warning/png.c/MagickPNGWarningHandler/1683.
I do feel like we are getting closer to this :D
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Composit 3 images using a single RGB mask

Post by Bonzo »

I am afraid any more is out of my area of expertise !

There is a thread here which may help: viewtopic.php?t=27056 Possibly the mask is the wrong format of png? Although snibgo and fmw42 both say it is a warning and should work.

I think you will have to post some links to your images.
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

I dont think its a colorspace issue. I tried saving it as result.jpg and got no errors, however instead of composite I got a white empty canvas again.

So the syntax must be wrong, or the files that I am mixing must be difficult for imagemagick
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

Here are the images:
http://imgur.com/a/d8eAR
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

The images were created in Krita and saved as png with the default settings!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composit 3 images using a single RGB mask

Post by snibgo »

So you have three images (colour or grayscale?), also three masks (in a single RGB image). You want to composite the three images according to the masks, but you haven't said what the rules are. What should happen where the mask image is exactly red? Or black? Or white?

Is your third image your mask? Is it smaller than the three images? Why? What should the output look like?

Also say what version of IM you are using.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composit 3 images using a single RGB mask

Post by fmw42 »

Please post your images (to some place such as dropbox.com and put the URLs here). Also always provide your IM version and platform, since as you see syntax differs. See http://www.imagemagick.org/Usage/windows/

Try this fix. I think I know what the issue is. We need to reset the compose method before the -flatten. My mistake.

Code: Select all

convert ( rgbmask.png -channel r -separate +channel image1.png +swap -alpha off -compose copy_opacity -composite ) ( rgbmask.png -channel g -separate +channel image2.png +swap -alpha off -compose copy_opacity -composite ) ( rgbmask.png -channel b -separate +channel image3.png +swap -alpha off -compose copy_opacity -composite ) -compose over -flatten result.png
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

That did it - however the result was messed up a bit because the mask image had a different size in pixels.
Can I equalize the size of all images to the same size of the first image somehow? I was wondering if imagemagick has a command for that!
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

Thank you guys - I learned a couple of valuable tricks in imagemagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composit 3 images using a single RGB mask

Post by fmw42 »

blurymind wrote:That did it - however the result was messed up a bit because the mask image had a different size in pixels.
Can I equalize the size of all images to the same size of the first image somehow? I was wondering if imagemagick has a command for that!
It can be done, but you need to tell us what version of IM and platform you have. Are all the images and mask different sizes. If so, which ones differ? Are the aspect ratios different?

However, if it is not just a scaling difference, then just resizing may still not allow the mask to composite the images aligned properly. You may have to pick control points and do -distort SRT to get them properly aligned.

Best if you can provide you input images so we can test with them. See my comments above about how to do that.
blurymind
Posts: 22
Joined: 2016-07-23T07:04:15-07:00
Authentication code: 1151

Re: Composit 3 images using a single RGB mask

Post by blurymind »

I'm on windows, but also linux. The images have the same aspect ratio, but different sizes.

I guess this time I am looking for a separate command that takes the first specified image's pixel size and sets all the next images to that size
Post Reply