Alex Rivera | Logout

PIL: How to make area transparent in PNG?

Asked 2010-12-07T18:03:33.460
15

I've been using PIL to crop Images, now I also want to make certain rectangular areas transparent, say

from PIL import Image
im = Image.open("sample.png")
transparent_area = (50,80,100,200)
...
Edit
Report

1 Answer

4

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.

answered 2010-12-07T18:47:08.830

Your Answer