python 使png图像透明

30byixjq  于 11个月前  发布在  Python
关注(0)|答案(3)|浏览(151)

如果我想让一个.png图像透明,我不能

blue = Image.open(image_path + "blue_color.png")
blue.putalpha(100)

字符串
但如果我这样做

blue = blue.convert('RGBA')


现在我可以让它透明。
我只想知道我用这些代码做什么

.convert('RGBA')


enter image description here

cuxqih21

cuxqih211#

blue = blue.convert('RGBA')

字符串
Python Imaging Library(PIL)中的convert方法Pillow用于更改图像模式。对于PNG图像,有时可能未将模式设置为“RGBA”(红色、绿色、蓝色、Alpha),其中包括用于透明度的Alpha通道。

b1zrtrql

b1zrtrql2#

blue.putalpha(100)

字符串
putalpha是从0到255,它定义了背景的透明度,但首先你必须将你的图像作为PNG引入,所以你必须对编译器说我的图像是RGBA,它有一个Alpha(红色,绿色,蓝色,Alpha(透明度))

vql8enpb

vql8enpb3#

没有理由你不能做你想做的事情。有多种方法可以从RGB图像到RGBA图像。我将展示两种方法,用水平线分隔。

from PIL import Image

# Create 64x64 RGB image in blue
RGB = Image.new('RGB', (64,64), 'blue')

# Convert mode to RGBA
RGBA = RGB.convert('RGBA')

print(RGBA)

字符串

输出

图像现在是RGBA:

<PIL.Image.Image image mode=RGBA size=64x64>
from PIL import Image

# Create 64x64 RGB image in blue
im = Image.new('RGB', (64,64), 'blue')

# Push in an alpha channel, forcing mode change from RGB to RGBA
im.putalpha(32)

print(im)

的数据

输出:

图像已从RGB强制转换为RGBA:

<PIL.Image.Image image mode=RGBA size=64x64>

相关问题