Clip PNG with an image 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
be00k
Posts: 3
Joined: 2016-07-07T07:57:22-07:00
Authentication code: 1151

Clip PNG with an image mask

Post by be00k »

I'm trying to clip an PNG image with an image mask.

I tried the following command:

Code: Select all

convert .\src.png .\mask.png -alpha Off -compose CopyOpacity -composite dst.png
When i use a JPEG as source image, everything is working fine. But if i use a PNG image, the output file turns black.

Source Image:
Image

Mask Image:
Image

Output:
Image

Anyone an idea what i'm doing wrong?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip PNG with an image mask

Post by snibgo »

You have "-alpha Off".

This turns off alpha in both images. What colour is revealed when you turn alpha off in the source image?
snibgo's IM pages: im.snibgo.com
be00k
Posts: 3
Joined: 2016-07-07T07:57:22-07:00
Authentication code: 1151

Re: Clip PNG with an image mask

Post by be00k »

Ok, my fault! ;-) But if i remove the command "alpha -Off" the transparent part in the source image is getting black. So how can i preserve the transparency in the source image?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip PNG with an image mask

Post by snibgo »

You haven't said what result you want.

Your source has transparency. Your mask is greyscale. What pixels in the result should be transparent?
snibgo's IM pages: im.snibgo.com
be00k
Posts: 3
Joined: 2016-07-07T07:57:22-07:00
Authentication code: 1151

Re: Clip PNG with an image mask

Post by be00k »

This is what i want to achieve:
Image


The transparent pixels in the source image should stay transparent

(Sorry for my vague descriptions before)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip PNG with an image mask

Post by snibgo »

This nearly gives what you want:

Code: Select all

convert src.png ( +clone -alpha extract mask.png -compose Darken -composite ) -compose CopyOpacity -composite out.png
It's not exact. Perhaps the offsets in src.png are not correct.
snibgo's IM pages: im.snibgo.com
Post Reply