Python PIL:如何在PNG中使区域透明? [英] Python PIL: how to make area transparent in PNG?
本文介绍了Python PIL:如何在PNG中使区域透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用PIL裁剪图片,现在我也想让某些矩形区域透明,比如说
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)
...
我非常感谢一些代码,因为我无法找到它,非常感谢提前!
I'd really appreciate some code as I haven't been able to find it, thanks a lot in advance!
干杯,
Hoff
推荐答案
import Image
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')
我在这里学习了这里。
这篇关于Python PIL:如何在PNG中使区域透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文