简体   繁体   中英

Python PIL - Merge Multiple Layers of Images into One

I have multiple PNG images with white background and some parts of the images are filled with patterns (it could be different colors, black, blue, red, yellow and so on).

How can I use Python PIL library to merge all these images together into one image such that all the non white portions appear on one single image?

As an example,

I have following 3 PNG images:

图片#1 图片#2 图片#3

Now, I want to merge all those images into one image such that the background is still white, however all the patterns appear on one single image.

As an example, I chose 2 images and tried the following:

#! /usr/bin/python

from PIL import Image

background = Image.open("check00001.png")
foreground = Image.open("check00002.png")

background.paste(foreground, (0, 0), foreground)
background.show()

But it merges the images in such a way that only the contents of one of the images is visible.

I need to do this for a large set of images where each image has a small part of the final image.

As far as I see it, you can easily transform the white pixels of your image to transparent with Pillow and them mask them layer upon layer.

To convert white pixels to transparent , you need to first convert the image data to buffer and then re-create it from the buffer, here is a sample code:

from PIL import Image 
# your loop here
img = Image.open('img.png') 
img = img.convert("RGBA") 
datas = img.getdata() 
newData = [] 
for item in datas: 
    if item[0] == 255 and item[1] == 255 and item[2] == 255: 
        newData.append((255, 255, 255, 0)) 
    else: 
        newData.append(item) 

img.putdata(newData) 
img.save("mod_img1.png", "PNG")

Then do your usual paste as you are doing in your code.

background = Image.open("mod_img1.png") 
foreground = Image.open("mod_img2.png") 

background.paste(foreground, (0, 0), foreground) 
background.show()

You can do that quite simply with ImageMagick which is installed on most Linux distros and is available for macOS and Windows. So, assuming your images are called a.png , b.png and c.png , you can go in the Terminal and run:

convert a.png                                \
   \( b.png -transparent white \) -composite \
   \( c.png -transparent white \) -composite result.png

在此处输入图片说明

That says... "Take image a.png as the basic image with its solid white background, load b.png and make all its white pixels transparent and composite that on top of the first image. Then do the same with c.png and save the output as result.png " .

Note that I also added a black border so you can make out the extent of the image on StackOverflow's white background.


Note that if you are using ImageMagick v7 or newer, the command becomes:

magick a.png                                 \
   \( b.png -transparent white \) -composite \
   \( c.png -transparent white \) -composite result.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM