1

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?

theozh
  • 22,244
  • 5
  • 28
  • 72
Ben Schmidt
  • 67
  • 1
  • 6

2 Answers2

4

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

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Although the OP seems to be satisfied with this answer, this just covers an area with a white rectangle. With "remove area" I would have expected a "see-through hole" (transparency) in the image, e.g. seeing a checkerboard behind it. Is there maybe a simple way to do this with ImageMagick which I haven't found yet? Like fmw42's solution but without OpenCV? – theozh Jul 30 '22 at 12:55
  • @theozh Sure, there are lots of possibilities. If you wanted the white area transparent in my answer, you can just add `-transparent white` before the output filename. You can draw other shapes than rectangles, e.g. circles or polygons or use masks... anything you like. If you want something specific, just ask a new question and add a link to it here so I get notified if I don't happen to see it - which is fairly unlikely. Just say - no problems. – Mark Setchell Jul 30 '22 at 16:15
  • thanks for your quick reply. Wouldn't `-transparent white` set all existing white pixels to transparent? I am thinking about an arbitrary colored image. In the meantime, I also found other answers of you to the same/similar topic. I am just surprised that ImageMagick can't simply set, e.g. within a rectangle region like `-region 10x20+110+40` the alpha channel of all pixels to `0xff`. – theozh Jul 30 '22 at 16:22
  • 1
    @theozh You can draw a transparent rectangle in the existing alpha channel of an image very similarly to how you suggest `magick YOURIMAGE.PNG \( +clone -alpha extract -fill black -draw "rectangle 10,40 80,80" \) -alpha off -compose copyalpha -composite result.png` – Mark Setchell Jul 30 '22 at 17:00
  • thanks again. I see, duplicating, drawing black rectangle on alpha channel, merge again. Although somehow straightforward, but not obvious for a ImageMagick-beginner. I was searching for a simple boolean operation on the alpha channel within a region. Could you maybe add your above solution to your answer, then I can upvote. Thank you! – theozh Jul 31 '22 at 07:43
  • Actually, here is a similar question, but the simple solution there doesn't work for me (version 7.1) https://stackoverflow.com/q/23082308/7295599 – theozh Jul 31 '22 at 07:46
  • 1
    @theozh I have added some extra material to my answer which hopefully will cover your use case better. – Mark Setchell Jul 31 '22 at 14:25
1

I assume you want a transparent region. Here is how to do that in Imagemagick and Python/OpenCV.

Convert the image to include and opaque alpha channel. Then create a mask that is a white background with a black rectangle. White for where the result should be opaque and black where it should be transparent. Then put the mask into the alpha channel of the image.

Input:

enter image description here

Imagemagick

convert barbara3.jpg \( +clone -fill white -colorize 100 -fill black -draw "rectangle 50,50 250,150" \) -alpha off -compose copy_opacity -composite result.png

Python/OpenCV

import cv2
import numpy as np

# read image
img = cv2.imread('barbara3.jpg')
hh, ww = img.shape[:2]

# convert to image with opaque alpha channel
imgA = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)

# create mask of filled black rectangle on white background
# white for opaque and black for transparent
mask = np.full((hh,ww), 255, dtype=np.uint8)
mask = cv2.rectangle(mask, (50,50), (250,150), 0, -1)

# put mask into alpha channel of image
result = imgA.copy()
result[:,:,3] = mask

cv2.imwrite('barbara3_mask.png', mask)
cv2.imwrite('barbara3_transparent_box.png', result)

cv2.imshow("img", img)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
cv2.waitKey(0)

Mask:

enter image description here

Result:

enter image description here

Download the resulting image to see that it is transparent and not white.

fmw42
  • 46,825
  • 10
  • 62
  • 80