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)
...
PNG images can contain alpha (transparency) information. Unlike GIF, which requires a particular color to be designated fully transparent, PNG allows the transparency of all pixels to take any value from fully transparent though partial transparency to full opacity.
The GIF and PNG formats also both support transparency. If you need any level of transparency in your image, you must use either a GIF or a PNG. GIF images (and also PNG) support 1-color transparency. This basically means that you can save your image with a transparent background.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With