Removing Alpha 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?".
Post Reply
6bit
Posts: 8
Joined: 2016-11-21T11:02:09-07:00
Authentication code: 1151

Removing Alpha Mask

Post by 6bit »

Hi,

I have a png with alpha which is used as a mask. The R,G,B,A channels are all identical, mainly zero, ones on the mask and a short ramp between.
I extract the mask from certain areas to other pngs then I want to clear these areas in the original image.
I tried many approaches but this should work "-region blah -alpha transparent +region".
It does clear the region but corrupts the image in areas outside the region.
By corrupt I mean non-zero pixels seem to have the R,G,B values molested but the A seems unchanged.
I would have thought that -region would protect the other areas.

Since the RGBA are all identical I had to protect from greyscale conversion by outputting via PNG32:xxx.png

How can I clear (make RGBA all zero) these areas?

I'm running command line magick Version: ImageMagick 7.0.9-17 Q16 x86_64 2020-01-24 via brew on MacOS Mojave.

Thx

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

Re: Removing Alpha Mask

Post by fmw42 »

I am having trouble understanding what you want to do. Post an example image to some free hosting service that won't change the format and then describe or post an image of what you want. Put the images URL(s), here to display your images. This forum does not support direct image uploads.

Note that in IM 7 -region does not work as it did in IM 6.

If I understand, you want to make transparent other areas that are in your image. You do that by creating a new mask for the extra areas. Then extract the alpha channel from the image. The multiply the two masks. Then put the new mask back into the alpha channel of the input completely replacing the old alpha channel.

Mixing of grayscale and color images in IM 7 may need you to add -colorspace sRGB to your command line to force the output to be color and not grayscale.

6bit
Posts: 8
Joined: 2016-11-21T11:02:09-07:00
Authentication code: 1151

Re: Removing Alpha Mask

Post by 6bit »

Fred,

Thx for quick response!
I don't have immediate access to a file server so I'll try this again with words :-(
The big picture: I have a "mask" image in a png. I want to extract some parts to smaller image masks - no problem, works.
Then I want to clear these areas in the original leaving any other areas untouched - problem RGB has changed.
For eg. a pixel never mentioned in the clearing process has a value (253,253,253,253) afterwards it is (205,205,205,253)
The command is like 'magick leftMFD-lighting.png -fill none -region 93x92+338+20 -alpha transparent +region <more regions x 30> PNG32:temp.png'
I also tried one region at a time instead of all at once. There are about thirty regions I wish to extract/clear.

I'm reading documentation on the main website - I suppose it's for IM7??

Your extract/multiply/replace method leaves me confused :-(

My images are RGBA incoming but IM seems to want to make them greyscale when it sees all pixels have identical RGBA. Not what I want.
PNG32: seems to fix this.

Thx.

6bit
Posts: 8
Joined: 2016-11-21T11:02:09-07:00
Authentication code: 1151

Re: Removing Alpha Mask

Post by 6bit »

One more thing. My thinking is that this is a bug as I would hope there would be no changes to an image outside the regions I change.
If it is then I need some alternative method, like extract/multiply/replace.

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

Re: Removing Alpha Mask

Post by fmw42 »

Regions works differently if at all in IM 7. See the section on behavioral changes in the porting guide at https://imagemagick.org/script/porting.php#cli

If you provide an example image and description of what you want to happen, then I can provide the command line to do that.

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

Re: Removing Alpha Mask

Post by fmw42 »

Here is an example:

Transparent Input:
Image

Separate Mask:
Image


1) read the transparent image
2) extract the alpha
3) read the mask and multiply it with the alpha
4) turn alpha off in both and use copy_opacity to put the product into the input replacing the old alpha
5) save the output

Code: Select all

convert logot.png \
\( -clone 0 -alpha extract +write alpha.png \
mask.png -compose multiply -composite +write product.png \) \
-alpha off -compose copy_opacity -composite \
logot2.png

I added +write for the extracted alpha and the product with the mask.

Alpha:
Image

Product:
Image

Result:
Image

6bit
Posts: 8
Joined: 2016-11-21T11:02:09-07:00
Authentication code: 1151

Re: Removing Alpha Mask

Post by 6bit »

Hi Fred,

I've reproduced my problem as a short sequence of magick commands and filed it as a bug under "region not restricting modification".
I will work on your example (thanks!) in my case to see if it's appropriate.

Thanks

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

Re: Removing Alpha Mask

Post by fmw42 »

Please read the porting guide reference that I provided. -region is changed in IM 7 and does not work the same way as in IM 6.

Post Reply