prinsen
prinsen

Reputation: 778

Imagemagick: apply cutout-region to another image

I have an PNG image with a transparent background, but the content is opaque. The transparent section is a mask that I would like to apply to another PNG image, so that they have identical transparent sections but may differ in the opaque section.

I have tried

convert mask.png image.png -composite mask.png -compose copyopacity -composite image.png

but that seems to alter the opaque section in image.png.

Upvotes: 1

Views: 79

Answers (1)

fmw42
fmw42

Reputation: 53089

In Imagemagick, assuming the two images are the same size, then try

(Unix syntax)

convert \
opaque_image.png \
\( transparent_image.png -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result.png


(Windows syntax)

convert ^
opaque_image.png ^
( transparent_image.png -alpha extract ) ^
-alpha off -compose copy_opacity -composite ^
result.png

It is possible that your transparent image does not have a binary alpha channel. For example, the alpha values may be 0 (fully transparent) in the areas you want to discard, but may be say only 80% in the areas you want to preserve. In that case, it will modify the opaque regions you want to preserve. One solution would be the threshold the mask before applying it to the opaque image.

Upvotes: 2

Related Questions