Python PIL 0.5不透明度,透明度,alpha [英] Python PIL 0.5 opacity, transparency, alpha
问题描述
有什么方法可以使图像半透明吗?
Is there any way to make an image half transparent?
伪代码是这样的:
from PIL import Image
image = Image.open('image.png')
image = alpha(image, 0.5)
我用Google搜索了几个小时,但找不到任何有用的信息.
I googled it for a couple of hours but I can't find anything useful.
推荐答案
我意识到这个问题确实很老,但是在当前版本的Pillow(v4.2.1)中,有一个名为putalpha
的函数.对我来说似乎很好.我不知道它是否适用于需要更改alpha的所有情况,但是它确实有效.它为图像中的每个像素设置Alpha值.似乎可以使用掩码: http://www.leancrew.com/all-this/2013/11/transparency-with-pil/.
I realize this question is really old, but with the current version of Pillow (v4.2.1), there is a function called putalpha
. It seems to work fine for me. I don't know if will work for every situation where you need to change the alpha, but it does work. It sets the alpha value for every pixel in the image. It seems, though that you can use a mask: http://www.leancrew.com/all-this/2013/11/transparency-with-pil/.
像这样使用putalpha
:
from PIL import Image
img = Image.open(image)
img.putalpha(128) # Half alpha; alpha argument must be an int
img.save(dest)
这篇关于Python PIL 0.5不透明度,透明度,alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!