Page 1 of 1
Mask usage
Posted: 2018-04-19T04:31:01-07:00
by jordi
Hi,
this is my first post here, after looking around the web I couldn't find the answer to this issue, which most probably have a very basic solution.
I want to merge 2 images by using a mask so I will have :
1.- original image a.jpg ( background )
2.- new image b.jpg ( over the background)
3.- mask image mask.jpg
explained graphically would be something like :
Any code sample on how to get to point 4 ?
would that work equally with a mask that has a gradient ?
thanks in advance
Re: Mask usage
Posted: 2018-04-19T06:45:36-07:00
by GeeMack
jordi wrote: ↑2018-04-19T04:31:01-07:00I want to merge 2 images by using a mask so I will have :
1.- original image a.jpg ( background )
2.- new image b.jpg ( over the background)
3.- mask image mask.jpg
[...]
Any code sample on how to get to point 4 ?
would that work equally with a mask that has a gradient ?
When you have a question here you need to give us more information. At the very least you should always let us know which version of IM you're using and what platform or OS you're working on. The answers can be a bit different – or a lot – depending on your set-up. Read the instructions
at THIS link to learn how to form a proper question.
That said, the simplest composition with a mask should work with a command like this...
Code: Select all
convert image1.png image2.png mask.png -composite result.png
The white areas of your mask will determine which parts of the overlay image will be composited onto the background image. To see how it works with a gradient mask, try this experiment...
Code: Select all
convert image1.png image2.png \( mask.png -blur 0x10 \) -composite result2.png
That isolates the mask image within the parentheses and applies a blur, then composites the overlay with a a softened edge determined by the blur of the mask.
The command above is in *nix shell command syntax. To use the same command in Windows CMD you need to remove the backslashes that escape the parentheses. Change this "\(...\)" to this "(...)". If you're using ImageMagick v7, use "magick" instead of "convert".
The page
at THIS link describes the usage of the "-composite" operator in detail.
Re: Mask usage
Posted: 2018-04-19T08:29:50-07:00
by jordi
Thanks so much for the answer !
it works perfect !
sorry for not pointing my version, which is ImageMagick 7.0.7-14 Q16 x86_64 2017-12-07 working on mac.
I wanted to try the blur using a mask but, seems to works in a inverse way than what suggested above.
Code: Select all
convert original.jpg : -mask mask.jpg -alpha off \
-blur 0x8 +mask original_blured.jpg
So I found a way to invert the colors of the mask
Code: Select all
convert mask.jpg -colorspace HSI -channel B -level 100,0% +channel -colorspace sRGB mask_reversed.jpg
But I think there must be a way to avoid having to invert the mask, isn't it ?
Thanks again !
Kind regards
Re: Mask usage
Posted: 2018-04-19T16:32:15-07:00
by GeeMack
jordi wrote: ↑2018-04-19T08:29:50-07:00But I think there must be a way to avoid having to invert the mask, isn't it?
The mask is doing something a bit different with "-mask" and "-composite". If your mask needs to be inverted to work in a particular command, you can read in the mask inside some parentheses and negate it, blur it, or whatever before continuing like this...
Code: Select all
... \( mask.jpg -alpha off -negate \) ...
Then continue the command as you need.
Re: Mask usage
Posted: 2018-04-20T02:07:45-07:00
by jordi
Hi, thanks and sorry because I did try code above merged with the one I was using to blur, and could not make it work
Code: Select all
convert original.jpg : \( mask.jpg -alpha off -negate \)
-blur 0x8 +mask original_blured.jpg
Re: Mask usage
Posted: 2018-04-20T17:55:54-07:00
by GeeMack
jordi wrote: ↑2018-04-20T02:07:45-07:00
Hi, thanks and sorry because I did try code above merged with the one I was using to blur, and could not make it work
Code: Select all
convert original.jpg : \( mask.jpg -alpha off -negate \)
-blur 0x8 +mask original_blured.jpg
That looks like a badly formed command. If you're using "-mask" you need to specify a file or a memory register "mpr:". The command would be assembled something like these...
Code: Select all
convert input.jpg -mask mask.jpg -blur 0x8 +mask result.jpg
Code: Select all
convert input.jpg \( mask.jpg -negate -write mpr:test123 +delete \) -mask mpr:test123 -blur 0x8 +mask result.jpg
You can find out more about the "mpr:" memory register
at THIS link.
Re: Mask usage
Posted: 2018-04-22T04:49:01-07:00
by jordi
GREAT !! thanks again !
Re: Mask usage
Posted: 2018-04-22T15:35:08-07:00
by jordi
Just last question about masking, this time using polygons
Code: Select all
convert \
logo.jpg \
\( -clone 0 -fill white -colorize 100 -fill black \
-draw "polygon 332,180 427,105 481,238 399,279" \
-alpha off -write mpr:mask +delete \) \
-mask mpr:mask -blur 0x5 +mask logo_blur.jpg
I've seen this method which seems very interesting in some cases, I have been looking for a method to declare more than one polygon at a time, but could find any tip on the issue.
Is there some method top declare multiple polygon to operate some blur or fill ?
thanks again , and kind regards
Re: Mask usage
Posted: 2018-04-22T15:45:44-07:00
by fmw42
Just draw multiple polygons to the same mask image. You can string different polygons in the same -draw or use multiple draws.
Re: Mask usage
Posted: 2018-04-22T15:50:23-07:00
by snibgo
jordi wrote:... a method to declare more than one polygon at a time
As Fred says, just draw as many polygons as you want, for example:
Code: Select all
-draw "polygon 332,180 427,105 481,238 399,279 polygon 0,0 0,200 200,0"
Re: Mask usage
Posted: 2018-04-23T01:17:07-07:00
by jordi
thanks great !