简体   繁体   English

Python PIL-将多层图像合并为一个

[英]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). 我有多张带有白色背景的PNG图像,并且图像的某些部分填充有图案(可能是不同的颜色,黑色,蓝色,红色,黄色等)。

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? 如何使用Python PIL库将所有这些图像合并到一个图像中,以便所有非白色部分都出现在一个图像上?

As an example, 举个例子,

I have following 3 PNG images: 我有以下3个PNG图片:

图片#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: 例如,我选择了2张图像并尝试了以下操作:

#! /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. 据我所知,您可以使用Pillow轻松地将图像的白色像素转换为透明像素,并将它们逐层遮盖。

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. 您可以使用安装在大多数Linux发行版中且可用于macOS和Windows的ImageMagick轻松地完成此操作。 So, assuming your images are called a.png , b.png and c.png , you can go in the Terminal and run: 因此,假设您的图像称为a.pngb.pngc.png ,则可以在终端中运行:

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 " . 那就是说... “将图像a.png作为具有白色背景的基本图像,加载b.png并使所有白色像素透明并合成第一幅图像的顶部。然后对c.png进行相同c.png并将输出保存为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. 请注意,我还添加了黑色边框,以便您可以在StackOverflow的白色背景上辨别图像的范围。


Note that if you are using ImageMagick v7 or newer, the command becomes: 请注意,如果您使用的是ImageMagick v7或更高版本,该命令将变为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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