如果我想让一个.png图像透明,我不能
blue = Image.open(image_path + "blue_color.png")blue.putalpha(100)
blue = Image.open(image_path + "blue_color.png")
blue.putalpha(100)
字符串但如果我这样做
blue = blue.convert('RGBA')
型现在我可以让它透明。我只想知道我用这些代码做什么
.convert('RGBA')
型enter image description here
cuxqih211#
字符串Python Imaging Library(PIL)中的convert方法Pillow用于更改图像模式。对于PNG图像,有时可能未将模式设置为“RGBA”(红色、绿色、蓝色、Alpha),其中包括用于透明度的Alpha通道。
b1zrtrql2#
字符串putalpha是从0到255,它定义了背景的透明度,但首先你必须将你的图像作为PNG引入,所以你必须对编译器说我的图像是RGBA,它有一个Alpha(红色,绿色,蓝色,Alpha(透明度))
vql8enpb3#
没有理由你不能做你想做的事情。有多种方法可以从RGB图像到RGBA图像。我将展示两种方法,用水平线分隔。
from PIL import Image# Create 64x64 RGB image in blueRGB = Image.new('RGB', (64,64), 'blue')# Convert mode to RGBARGBA = RGB.convert('RGBA')print(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 blueim = Image.new('RGB', (64,64), 'blue')# Push in an alpha channel, forcing mode change from RGB to RGBAim.putalpha(32)print(im)
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:
型
3条答案
按热度按时间cuxqih211#
字符串
Python Imaging Library(PIL)中的convert方法Pillow用于更改图像模式。对于PNG图像,有时可能未将模式设置为“RGBA”(红色、绿色、蓝色、Alpha),其中包括用于透明度的Alpha通道。
b1zrtrql2#
字符串
putalpha是从0到255,它定义了背景的透明度,但首先你必须将你的图像作为PNG引入,所以你必须对编译器说我的图像是RGBA,它有一个Alpha(红色,绿色,蓝色,Alpha(透明度))
vql8enpb3#
没有理由你不能做你想做的事情。有多种方法可以从RGB图像到RGBA图像。我将展示两种方法,用水平线分隔。
字符串
输出:
图像现在是RGBA:
的数据
输出:
图像已从RGB强制转换为RGBA:
型