Mask usage

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?".
Post Reply
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Mask usage

Post 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 :

Image

Any code sample on how to get to point 4 ?

would that work equally with a mask that has a gradient ?

Image


thanks in advance
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Mask usage

Post 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.
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Re: Mask usage

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Mask usage

Post 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.
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Re: Mask usage

Post 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
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Mask usage

Post 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.
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Re: Mask usage

Post by jordi »

GREAT !! thanks again !
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Re: Mask usage

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

Re: Mask usage

Post by fmw42 »

Just draw multiple polygons to the same mask image. You can string different polygons in the same -draw or use multiple draws.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Mask usage

Post 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"
snibgo's IM pages: im.snibgo.com
jordi
Posts: 7
Joined: 2018-04-19T03:49:17-07:00
Authentication code: 1152

Re: Mask usage

Post by jordi »

thanks great !
Post Reply