
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - ImageChops.composite() Function
The ImageChops.composite() function is used to create a composite image by blending two images using a transparency mask. This function is an alias for the composite function in the main Image module.
Syntax
Following is the syntax of the method −
PIL.ImageChops.composite(image1, image2, mask)
Or
PIL.Image.composite(image1, image2, mask)
Parameters
Here are the details of its parameters −
image1 − The first input image to composite.
image2 − The second input image to composite. It must have the same mode and size as the first image.
mask − A mask image. This image can have modes "1", "L", or "RGBA", and it must have the same size as the other two images.
Return Value
The return type of this function is an Image.
Examples
Example 1
In this example, image1, image2, and mask are the input images, and the composite() function is used to blend them together based on the transparency information in the mask.
from PIL import Image, ImageChops # Open the first image image1 = Image.open('Images/Light.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/TP-W.jpg') # Open the mask image with mode "L" mask = Image.open("Images/mask.jpg").convert("L") # Use the composite function from ImageChops to blend the images using the mask composite_image = ImageChops.composite(image1, image2, mask) # Display or save the resulting composite image image1.show() image2.show() mask.show() composite_image.show()
Output
Input Image 1

Input Image 2

Input Mask

Output Composite Image

Example 2
The provided example creates a solid gray image with a pixel value of 128 and uses it as a mask to blend two images together.
from PIL import Image, ImageChops # Open the first image image1 = Image.open('Images/TP-W.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/rose.jpg').resize(image1.size) # Create a solid gray mask image with a pixel value of 128 mask = Image.new("L", image1.size, 128) # Use the composite function to blend the images using the mask composite_image = Image.composite(image1, image2, mask) # Display the input and resulting images image1.show() image2.show() mask.show() composite_image.show()
Output
Input Image 1

Input Image 2

Input Mask

Output Composite Image

Example 3
This example creates a mask with a white rectangle on a black background and uses it to blend two images together.
from PIL import Image, ImageDraw # Open the first image image1 = Image.open('Images/TP-W.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/rose.jpg').resize(image1.size) # Create a mask image with a white rectangle on a black background mask = Image.new("L", image1.size, 0) draw = ImageDraw.Draw(mask) draw.rectangle((245, 45, 445, 345), fill=255) # Use the composite function to blend the images using the mask composite_image = Image.composite(image1, image2, mask) # Display the input and resulting images image1.show() image2.show() mask.show() composite_image.show()
Output
Input Image 1

Input Image 2

Input Mask

Output Composite Image
