This article mainly introduces the python implementation of multiple image superposition output in detail, the article sample code introduction is very detailed, has a certain reference value, interested partners can refer to it. The example of this article is to share the python implementation of multiple image overlay output for your reference. The details are as follows
code
from PIL import Image def add_alpha_channel(img): Img = image.open (img) img = img.convert('RGBA') # change Image transparency # 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: Putpixel ((I, j), (0, 0, 256, 200)) # Paste (layer1, (0, 0), 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) # map operating base. The save (save_name save_path + "/" + + ". PNG ") # image save if __name__ = = "__main__" : image = add_alpha_channel(r"") layer1 = add_alpha_channel(r"") layer2 = add_alpha_channel(r"") image_together(image, layer1, layer2, r"", "")Copy the code
The results
Before running
After the operation
This article is from the SDK community: www.sdk.cn