I want to remove a area from a image but I am not sure where to look at. Let me give you a example.
Destination:
So, I've removed 100x100 a area starting at 100x100 pixels. Could someone please guide me where to look at?
I want to remove a area from a image but I am not sure where to look at. Let me give you a example.
Destination:
So, I've removed 100x100 a area starting at 100x100 pixels. Could someone please guide me where to look at?
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:
Following on from the comments, here is an example if you have an image with no existing transparency:
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
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:
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
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:
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:
Result:
Download the resulting image to see that it is transparent and not white.