PIL: How to make area transparent in PNG?

from PIL import Image
from PIL import ImageDraw
im = Image.open("image.png")
transparent_area = (50,80,100,200)

mask=Image.new('L', im.size, color=255)
draw=ImageDraw.Draw(mask) 
draw.rectangle(transparent_area, fill=0)
im.putalpha(mask)
im.save('/tmp/output.png')

I learned how to do this here.


No source code, but this is the general approach that should work: Create an alpha channel for the image in "L" (grayscale) mode as a separate image object. Fill the alpha channel with white (full opacity) and draw the rectangle on the alpha channel image in black (full transparency). Convert the image to which you want to apply the transparency to RGBA and use the image object putalpha() method to copy the alpha channel you created into the image's alpha channel. Save as PNG.