How i apply a mask to a grayscale image ?

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
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

How i apply a mask to a grayscale image ?

Post by nars »

Hi,

I want apply a mask to an image. (image and mask have the same dimensions)

input.png (grayscale)
mask.png

I used the next code:

convert input.png mask.png -alpha off -compare CopyOpacity -composite out.png

The mask was applied correctly, but the out.png lost the grayscale (obviously), i think..

Suggestions for applying the mask and not lose the grayscale ?

thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How i apply a mask to a grayscale image ?

Post by snibgo »

Use "-compose" instead of "-compare":

Code: Select all

convert input.png mask.png -alpha off -compose CopyOpacity -composite out.png
If that doesn't help, upload inputs and output to somewhere like dropbox.com and paste the URLs here.
snibgo's IM pages: im.snibgo.com
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: How i apply a mask to a grayscale image ?

Post by nars »

The same :/

Mask image:
Image

Input image:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How i apply a mask to a grayscale image ?

Post by snibgo »

Code: Select all

convert 2z4er1s.png 14j5cmw.png -alpha off -compose CopyOpacity -composite outMasked.png
This is the result:
Image
It looks fine to me. What result do you get? What is wrong with it?
snibgo's IM pages: im.snibgo.com
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: How i apply a mask to a grayscale image ?

Post by nars »

yes, it looks fine.. Sorry, bad example :/

With the next images, you can see the differences.

input:
Image

mask:
Image

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How i apply a mask to a grayscale image ?

Post by snibgo »

Again, the result looks fine to me. What is wrong with it?

Code: Select all

convert elcqxy.jpg.png 2ueigev.jpg.png -alpha off -compose CopyOpacity -composite outMasked2.png
Image
snibgo's IM pages: im.snibgo.com
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: How i apply a mask to a grayscale image ?

Post by nars »

My result:

Image

My code:

Code: Select all

convert input2.png mask2.png -alpha off -compose CopyOpacity -composite outMasked_2.png
Why is different?

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How i apply a mask to a grayscale image ?

Post by fmw42 »

What version of IM and platform are you using?

Nothing is wrong with it. Your two examples are on different color backgrounds, which show through in the transparent areas.
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: How i apply a mask to a grayscale image ?

Post by nars »

Version: ImageMagick 6.7.7-10 2014-03-06

Platform: Linux - Ubuntu

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How i apply a mask to a grayscale image ?

Post by snibgo »

Your result is darker than mine, because your old version of ImageMagick 6.7.7-10 was confused between linear and non-linear grayscale. I suggest you upgrade to a recent version.
snibgo's IM pages: im.snibgo.com
nars
Posts: 8
Joined: 2015-07-17T04:27:32-07:00
Authentication code: 1151

Re: How i apply a mask to a grayscale image ?

Post by nars »

Yes, I update ImageMagick to a recent version and solved my problem.

Thanks ;)
Post Reply