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

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
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

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

Post 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
(using the latest IM version on linux centos 6)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
ravikum
Posts: 34
Joined: 2013-03-20T05:35:37-07:00
Authentication code: 6789

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

Post by ravikum »

Thanks a lot for your reply.
I will use your code.
(using the latest IM version on linux centos 6)
Post Reply