Search code examples
imagemagickimagemagick-convert

Imagemagick remove Area of a Image


I want to remove a area from a image but I am not sure where to look at. Let me give you a example.

Source: enter image description here

Destination:

enter image description here

So, I've removed 100x100 a area starting at 100x100 pixels. Could someone please guide me where to look at?


Solution

  • I think you want to paint a white rectangle on an image. So if you want a white rectangle 100px wide and 20px tall, offset 10px from the left side and 20px from the top, use:

    magick input.png -fill white -draw "rectangle 10,20 110,40" result.png
    

    So, if I use a 200x100 magenta input image, I get:

    enter image description here


    Following on from the comments, here is an example if you have an image with no existing transparency:

    enter image description here

    And to "punch a transparent hole" we create a new layer the same size as the image, filled with white then draw a black hole in it, and push the result into the transparency layer of the original:

    magick image.png \( +clone -fill white -colorize 100% -fill black -draw "rectangle 20,10 300,40" \) -alpha off -compose copy-alpha -composite result.png
    

    enter image description here

    If the original image already has transparency which we don't want to replace entirely, but just modify with an extra hole, we can start from this image:

    enter image description here

    And proceed to extract the original alpha channel, modify it and push it back into the original:

    magick image.png \( +clone -alpha extract -fill black -draw "rectangle 10,40 80,80" \) -alpha off -compose copyalpha -composite result.png
    

    enter image description here