python實現多圖像疊置輸出

本文實例為大傢分享瞭python實現多圖像疊置輸出,供大傢參考,具體內容如下

代碼

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"", "")

運行結果

運行前

運行後

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found