本文实例为大家分享了python实现多图像叠置输出,供大家参考,具体内容如下
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
from pil import image def add_alpha_channel(img): img = image. open (img) img = img.convert( 'rgba' ) # 更改图像透明度 # factor = 0.7 # img_blender = image.new('rgba', img.size, (0, 0, 0, 0)) # img = image.blend(img_blender, img, factor) return img def image_together(image, layer1, layer2, save_path, save_name): layer1 = layer1 layer2 = layer2 base = image bands = list (layer1.split()) heigh, width = layer1.size for i in range (heigh): for j in range (width): r, g, b, a = layer1.getpixel((i, j)) if r = = 0 : layer1.putpixel((i, j), ( 0 , 0 , 0 , 0 )) # 背景透明显示 else : layer1.putpixel((i, j), ( 0 , 0 , 256 , 200 )) # 非背景区域显示为红色 layer2.paste(layer1, ( 0 , 0 ), layer1) # 贴图操作 base = image bands = list (layer2.split()) heigh, width = layer2.size for i in range (heigh): for j in range (width): r, g, b, a = layer2.getpixel((i, j)) if r = = 0 : layer2.putpixel((i, j), ( 0 , 0 , 0 , 0 )) elif r = = 128 and g = = 128 and b = = 128 : layer2.putpixel((i, j), ( 128 , 128 , 128 , 200 )) else : layer2.putpixel((i, j), ( 255 , 0 , 0 , 200 )) base.paste(layer2, ( 0 , 0 ), layer2) # 贴图操作 base.save(save_path + "/" + save_name + ".png" ) # 图片保存 if __name__ = = "__main__" : image = add_alpha_channel(r"") layer1 = add_alpha_channel(r"") layer2 = add_alpha_channel(r"") image_together(image, layer1, layer2, r" ", " ") |
运行结果
运行前
运行后
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44813407/article/details/115412429