title: Python PIL - Merge Multiple Layers of Images into One
body: <p>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).</p>
  
  <p>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?</p>
  
  <p>As an example,</p>
  
  <p>I have following 3 PNG images:</p>
  
  <p><a href="https://i.stack.imgur.com/KkKcH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KkKcH.png" alt="Image #1"></a>
  <a href="https://i.stack.imgur.com/wGfTJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wGfTJ.png" alt="Image #2"></a>
  <a href="https://i.stack.imgur.com/PqItV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PqItV.png" alt="Image #3"></a></p>
  
  <p>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.</p>
  
  <p>As an example, I chose 2 images and tried the following:</p>
  
  <pre><code>#! /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()
  </code></pre>
  
  <p>But it merges the images in such a way that only the contents of one of the images is visible.</p>
  
  <p>I need to do this for a large set of images where each image has a small part of the final image.</p>