Python:Pillow .alpha_composite()
Published Mar 20, 2025
Contribute to Docs
In Pillow, the .alpha_composite() method merges two images by overlaying the foreground onto the background using their alpha (transparency) channels. It returns a new image with the composite result.
Syntax
PIL.Image.alpha_composite(image1, image2)
Parameters:
image1(Image): The background image, which must be inRGBAmode.image2(Image): The foreground image, which must also be inRGBAmode.
Return value:
Returns a new RGBA image with the composite result.
Note: The
.alpha_composite()method requires bothimage1andimage2parameters to have the same dimensions (width and height); otherwise, it won’t work as expected.
Example
The following example demonstrates the use of the .alpha_composite() method:
from PIL import Image# Open background and foreground imagesbackground = Image.open("ocean.png").convert("RGBA")foreground = Image.open("ship.png").convert("RGBA")# Composite the imagesresult = Image.alpha_composite(background, foreground)# Display and save the resultresult.show()result.save('ship-on-ocean.png')
Original Background Image:

Original Foreground Image:

Result Composite Image:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours