Last modified: Oct 19, 2024 By Alexander Williams

Combining and Merging Images using Pillow

Combining and merging images can be very useful for creating collages, blending images, or overlaying text and logos. The Pillow library in Python provides several functions to make this process easy. In this tutorial, we’ll cover various techniques for merging images.

Getting Started with Pillow

Before diving into image merging, make sure you have Pillow installed. If not, check out our guide on Pillow: Installation and getting started (Python) to get set up.

Merging Images Using the Paste Method

The paste() method allows you to overlay one image onto another. Here’s how you can use it:


from PIL import Image

# Open the background and overlay images
background = Image.open('background.jpg')
overlay = Image.open('overlay.png')

# Paste the overlay onto the background at position (50, 50)
background.paste(overlay, (50, 50))

# Save the combined image
background.save('merged_image.jpg')

This method is useful for placing logos or adding watermarks. For more on adding text or logos as watermarks, refer to our guide on Adding Text and Watermarks to Images using Pillow.

Blending Images with blend()

The blend() method allows you to merge two images with a specified alpha value, which controls the opacity of the images:


from PIL import Image

# Open the two images
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')

# Blend the images with an alpha value of 0.5
blended_image = Image.blend(image1, image2, alpha=0.5)

# Save the blended image
blended_image.save('blended_image.jpg')

Using blend() is a great way to create fade effects between images. The alpha parameter controls the balance between the two images, with values ranging from 0.0 to 1.0.

Combining Images Using ImageOps

The ImageOps module in Pillow offers methods for extending, adding borders, and combining images. For instance, you can add a border to an image before merging:


from PIL import Image, ImageOps

# Open the image and add a border
image = Image.open('image.jpg')
bordered_image = ImageOps.expand(image, border=20, fill='black')

# Save the image with a border
bordered_image.save('bordered_image.jpg')

This can be combined with pasting or blending methods for more customized image merges.

Combining Images Using Alpha Compositing

The composite() function allows for more complex image merges using masks. This method is particularly useful for creating effects where certain parts of an image remain transparent:


from PIL import Image

# Open the base images and mask
image1 = Image.open('image1.jpg')
image2 = Image.open('image2.jpg')
mask = Image.open('mask.png')

# Use composite to merge images using a mask
composite_image = Image.composite(image1, image2, mask)

# Save the composite image
composite_image.save('composite_image.jpg')

The mask image controls which parts of image1 and image2 are visible in the resulting image, allowing for smooth transitions and detailed effects.

Conclusion

Using Python Pillow, you can easily merge and combine images in various ways, whether it's simple pasting or complex blending and compositing. These techniques can be used to create unique image compositions, collages, or graphics. For further enhancements and filters, check out our article on Image Filters and Enhancements using Python Pillow.

Now that you have learned about merging images, you can start creating your own custom image effects with Python and Pillow!