Python PIL 0.5 不透明度、透明度、alpha

标签 python transparency python-imaging-library opacity alpha

有没有办法让图片半透明?

伪代码是这样的:

from PIL import Image
image = Image.open('image.png')
image = alpha(image, 0.5)

我用谷歌搜索了几个小时,但找不到任何有用的东西。

最佳答案

我意识到这个问题真的很老,但是在当前版本的 Pillow (v4.2.1) 中,有一个名为 putalpha 的函数。这对我来说似乎很好用。我不知道是否适用于您需要更改 alpha 的所有情况,但它确实有效。它为图像中的每个像素设置 alpha 值。看起来,虽然你可以使用面具:http://www.leancrew.com/all-this/2013/11/transparency-with-pil/ .

像这样使用putalpha:

from PIL import Image
img = Image.open(image)
img.putalpha(127)  # Half alpha; alpha argument must be an int
img.save(dest)

关于Python PIL 0.5 不透明度、透明度、alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731035/

相关文章:

python - PIL Image.size() 不工作

java - JOGL透明背景

python - 如何使用 PIL 将 1 channel 图像转换为 3 channel 图像?

python - 填充图像以在 wxpython 中使用

python - 如何在 python 中找到字符串中第一个非空白字符的索引?

css - 只使用 CSS,我可以只使用 PNG 的 alpha channel 吗?

html - 透明框上的不透明文本

python - imshow奇怪的cv2行为

python - 实现自定义层来生成二维指数图像

python - Python交换列表中的相邻元素(语法错误)