Page 2 of 3

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T15:41:48-07:00
by fmw42
What version of Imagemagick. Different code is needed if on IM 6 vs IM 7.

Please read the topmost post in this forum at viewtopic.php?f=1&t=9620

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T15:47:15-07:00
by snibgo
Be aware that the mask image has pixels like RGB(50%,50%,0). Fred's command will create pixels from one image and alpha 50%, over pixels of another image with alpha 50%, over a "-background" colour. This may or may not be the desired effect. If it is, don't forget to set the required background colour.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T16:23:45-07:00
by fmw42
snibgo wrote:Be aware that the mask image has pixels like RGB(50%,50%,0). Fred's command will create pixels from one image and alpha 50%, over pixels of another image with alpha 50%, over a "-background" colour. This may or may not be the desired effect. If it is, don't forget to set the required background colour.
Yes, snibgo is correct. I forgot about that. So perhaps it would be better to use -compose atop. Snibgo, do you have a better method? Also to be sure there is no unfilled color, then he is also correct about setting the background color or using one image as the background. Fill in what ever color you want for "somecolor"

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 ) -background somecolor -compose atop -composite result.png

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T16:35:26-07:00
by blurymind
Ah I see. This is good to know as a precaution.
I guess at this point I am more interested in using the size of image one as target size for images 2,3,4 and so on - I can use that as a common operation to make images the same size before doing operations that wouldnt work with them being different sizes.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T16:54:44-07:00
by snibgo
For the resizing: in a Windows BAT command:

Code: Select all

for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h" ^
  %SRC%`) do set %%L
Then:

Code: Select all

-resize "%WW%x%HH%^!"
for each other image.

For the mask problem: Any of the opaque inputs can be used as the "bottom-layer". Then apply each of the others, as Fred's command, with copy-opacity. However, that may not be the desired result.

If the mask has, for every pixel, R+G+B=100%, and the rule for compositing is "each output pixel should be R% of the first image, plus G% of the second, plus B% of the third" then we multiply each image by the appropriate channel and add the results. Something like (Windows BAT syntax, IM v6):

Code: Select all

convert
mask.png +write mpr:MASK +delete ^
( mpr:MASK -channel R -separate +channel image1.png -compose Multiply -composite ) ^
( mpr:MASK -channel G -separate +channel image2.png -compose Multiply -composite ) ^
( mpr:MASK -channel B -separate +channel image3.png -compose Multiply -composite ) ^
-compose Add -layers Merge ^
out.png

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T17:10:59-07:00
by fmw42
Snibgo's method is better than mine. Just to point a mistake with my method posted earlier. You can only do -compose ... -composite with two images at a time. So my method should more properly be as follows assuming that your mask will have no gaps between images:

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 ) ^
-compose atop -composite ^
( rgbmask.png -channel b -separate +channel image3.png +swap -alpha off -compose copy_opacity -composite ) ^
-compose atop -composite ^
result.png

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T17:13:01-07:00
by blurymind
@snibgo - your solution is so far the best one! It seems to resize the three images to the size of the mask.

Is there a function in imagemagick's arsenal to resize a number of images to the size of a target image? Without the need to encompass it in a script.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T17:19:28-07:00
by fmw42
Resize to a common size. You can specify the common size or get it from for example image 1

Code: Select all

convert image1 -format "%wx%h" info:
convert image1 image2 ... imageN -resize WxH resultimage
Result image will either be one image with multiple layers such as for TIFF or MIFF or multiple images with -0, -1 ... -N appended to the filename supplied for resultimage, such as for PNG or JPG.

But if the aspect ratios are not the same then you would need to either crop or pad the result using -extent WxH. Or force the images to be distorted to that exact size using the ! modifier. See http://www.imagemagick.org/script/comma ... p#geometry

In Imagemagick 7 this can be done in one command line.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T17:41:06-07:00
by blurymind
@fmw42 How do you do it in one command line? The example code gives me errors and nothing happens to the images

Code: Select all

>convert out.png -format "%wx%h" info: convert b.png 2m.png ... imageN -resize WxH resultimage

convert: no decode delegate for this image format `INFO' @ error/constitute.c/Re
adImage/508.
convert: unable to open image 'convert': Permission denied @ error/blob.c/OpenBl
ob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadIm
age/508.
convert: unable to open image '...': Permission denied @ error/blob.c/OpenBlob/2
695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadIm
age/508.
convert: unable to open image 'imageN': No such file or directory @ error/blob.c
/OpenBlob/2695.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadIm
age/508.
convert: invalid argument for option '-resize': WxH @ error/convert.c/ConvertIma
geCommand/2578.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T18:03:58-07:00
by fmw42
You have to be using IM 7 and then there is a different syntax that needs to be used.

In IM 7, you can do

Code: Select all

magick image1 -set option:mysize "%wx%h" image2 image3 ... imageN -resize "%[mysize]" result.png
You cannot do this in IM 6.

You still have not provided your IM version!

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T18:16:57-07:00
by blurymind
I am Using IM 7 ! Sorry :)

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T18:20:27-07:00
by blurymind
I still get errors and no result unfortunately :

Code: Select all

magick out.png -set option:mysize "%wx%h" b.png 2m.png ... imageN -resize "%[mysize]" result.png

magick: unable to open image '...': Permission denied @ error/blob.c/OpenBlob/2695.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadImage/508.

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T18:21:51-07:00
by blurymind
Ok It worked after I removed the '...' and 'imageN'

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T19:38:33-07:00
by blurymind
@fmw42 How do you also tell the result images to have the name of their target counterparts? Yes, I want to overwrite the originals

Re: Composit 3 images using a single RGB mask

Posted: 2016-07-23T23:30:52-07:00
by fmw42
blurymind wrote:@fmw42 How do you also tell the result images to have the name of their target counterparts? Yes, I want to overwrite the originals
Generally, magick or convert processes one input to one output. But mostly it can only have one output name.

So, get the size of the first image. Then write a loop over each input image and resize to the size of the first image and then save to the same input name.

Or get the size of the first image and then use mogrify to resize all the images in a folder. See http://www.imagemagick.org/Usage/basics/#mogrify

In Unix there are some special ways to handle this similarly. See http://www.imagemagick.org/Usage/basics/#mogrify_not