Page 1 of 1

how to cut two holes in a PNG one after the other

Posted: 2014-09-07T23:55:26-07:00
by ravikum
Hello,
I have this image :

Image1.png :
Image

I cut a hole in this image using the mask of same width and height :
mask1.png : (black and white image with no transparency)
Image

Code: Select all

convert image1 mask1 -alpha Off -compose CopyOpacity -composite image2
and the result :
Image2.png :
Image

I have another mask of same width and height :
Mask2.png : (black and white image with no transparency)
Image


I wish to cut another hole in image 2 using mask 2 without erasing the first hole.

I tried the same command given above and it created the second hole, but erased the first hole.

If there is any other method, please inform.

Please help.
Thanks

Re: how to cut two holes in a PNG one after the other

Posted: 2014-09-08T02:39:55-07:00
by snibgo
"CopyOpacity" replaces all the alpha values in the image with values from the mask. The trick is to manipulate the mask. There are many ways to do this. Windows BAT syntax; adjust for other shells. (For Unix, escape parentheses \( and \) and use \ instead of ^ at line ends.)

Method 1. If you have only two masks:

Code: Select all

convert ^
  Image1.png ^
  ( mask1.png ^
    mask2.png ^
    -compose Darken -composite ^
    -write x.png ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  out.png
Method 2. If you have any number of masks, and want to have separate convert commands, chained together.

Code: Select all

convert ^
  Image1.png ^
  mask1.png ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  out1.png

convert ^
  out1.png ^
  ( +clone ^
    -alpha extract ^
    mask2.png ^
    -compose Darken -composite ^
    -write x.png ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  out2.png

Re: how to cut two holes in a PNG one after the other

Posted: 2014-09-08T04:56:48-07:00
by ravikum
Thanks a lot for your reply.
I will use your code.