Page 1 of 1

clear a rectangle

Posted: 2011-03-22T01:27:00-07:00
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)?

Re: clear a rectangle

Posted: 2011-03-22T09:48:20-07:00
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

Re: clear a rectangle

Posted: 2011-03-22T20:12:19-07:00
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: