.paste()
Anonymous contributor
Published Mar 24, 2025
Contribute to Docs
The .paste()
method in Pillow allows pasting one image onto another at a specified position. It is commonly used for image composition, watermarking, or creating collages. An optional mask can be provided to handle transparency.
Syntax
Image.paste(im, box=None, mask=None)
Parameters:
im
(Image | integer | float | string | tuple): The source image to paste or a solid color. If a color is provided (tuple for RGB/RGBA, integer for grayscale, or color string like"red"
), thebox
parameter must be specified.box
(tuple, optional): Specifies where to paste the image. If a 2-tuple(x, y)
is given, it defines the top-left corner. If a 4-tuple(left, upper, right, lower)
is given, it defines a bounding box, and the pasted image is resized to fit it.mask
(Image, optional): A mask image controlling transparency. Must be in mode"1"
,"L"
,"LA"
,"RGBA"
, or"RGBa"
. Only areas where the mask is nonzero are pasted.
Return Value:
This method modifies the original image in-place and does not return a new image.
Example
This is the background image that will be used for the collage:
This is the foreground image that will be pasted onto the background:
This example demonstrates how to paste one image onto another to create a collage:
from PIL import Image# Open the background and foreground imagesbackground = Image.open('background.jpeg')foreground = Image.open('foreground.jpeg')# Resize the foreground image to fit better on the backgroundforeground = foreground.resize((200, 200))# Define the position where the foreground will be pasted (top-left corner)position = (100, 100)# Paste the foreground image onto the backgroundbackground.paste(foreground, position)# Save the resultbackground.save('collage_result.jpeg')# Display the imagebackground.show()
In this example, foreground.jpeg is pasted onto background.jpeg at the position (100, 100)
.
The code above produces the image below:
All contributors
- Anonymous contributor
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours