Want to clear a corner of a PNG

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
kryocentric
Posts: 2
Joined: 2019-08-07T16:27:03-07:00
Authentication code: 1152

Want to clear a corner of a PNG

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Want to clear a corner of a PNG

Post 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
kryocentric
Posts: 2
Joined: 2019-08-07T16:27:03-07:00
Authentication code: 1152

Re: Want to clear a corner of a PNG

Post by kryocentric »

The region method works perfectly, thank you!
Post Reply