Page 1 of 1

Want to clear a corner of a PNG

Posted: 2019-08-07T17:06:23-07:00
by kryocentric
Given a PNG filled with color, how do I clear the top left corner so that it's just transparent? I can achieve this in MacOS's Preview by selecting an area and pressing delete.

What I imagined would work - convert 3.png -strokewidth 0 -fill transparent -draw "rectangle 0,0 30,30 " output.png

Version: ImageMagick 6.9.9-40 Q16 x86_64 2019-08-07

Re: Want to clear a corner of a PNG

Posted: 2019-08-07T18:41:14-07:00
by fmw42
One way is to clone the image, make it completely white, then draw a black region and then put that into the alpha channel of the input.

Code: Select all

convert logo: \
\( +clone -fill white -colorize 100 \
-fill black -draw "rectangle 0,0 99,99" \) \
-alpha off -compose copy_opacity -composite \
result.png
Another way is to use -region:

Code: Select all

convert logo: -alpha set -region 100x100+0+0 -alpha transparent +region result.png
A third way is to draw a rectangle of some opaque color not in (that part of) the image, then flood fill that color with transparency.

Code: Select all

convert logo: \
-fill black -draw "rectangle 0,0 99,99" -alpha off \
-fill none -draw "matte 0,0 floodfill" \
result.png

Re: Want to clear a corner of a PNG

Posted: 2019-08-08T10:43:54-07:00
by kryocentric
The region method works perfectly, thank you!