clear a rectangle

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
netro78
Posts: 1
Joined: 2011-03-22T01:18:21-07:00
Authentication code: 8675308

clear a rectangle

Post by netro78 »

I want to clear a rectangular region from an image (ie completely remove the contents of a rectangle and replace them with transparent pixel). My solution right now is to use the composite operation with DstIn/DstOut. But this is very slow because it requires creating an overlay image which in my case has to be really big. Is there a smarter way to that, maybe something similar to what you I would do in a conventional image editor (ie select a rectangular region and the clear the selection, this can be done in O(1) additional memory)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clear a rectangle

Post by fmw42 »

you can take your transparent rectangle and use -compose copy -composite, but that still requires you to use composite with a transparent image.

convert logo: \( -size 100x100 xc:none \) -alpha set -gravity center -compose copy -composite logo_hole.gif


you can also draw a colored rectangle (of some color that does not match any colors along its border), then do a -draw matte floodfill

cannot say which is faster, but suspect the first will be.

see http://www.imagemagick.org/Usage/draw/#color and http://www.imagemagick.org/Usage/draw/#matte
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: clear a rectangle

Post by anthony »

netro78 wrote:But this is very slow because it requires creating an overlay image which in my case has to be really big
It does not have to be.. use a smaller image and set a -geometry offset.

Code: Select all

  convert rose: -alpha set -size 20x20 xc: -geometry +20+10 \
          -compose DstOut -composite  show:
ASIDE:...
I thought that maybe you could do it with a region, but regions make transparent areas see-thru as it uses a 'Over' method to overlay the modified region images. I have not looked too deeply into regions as yet.
That is this failed...

Code: Select all

  convert rose: -alpha set    -region 20x20+20+10 -alpha transparent +region    show:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply