|
| 1 | +--- |
| 2 | +Title: '.paste()' |
| 3 | +Description: 'Pastes an image onto another image at a specified position.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Images' |
| 9 | + - 'Libraries' |
| 10 | + - 'Methods' |
| 11 | + - 'Pillow' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +Image.paste(im, box=None, mask=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `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"`), the `box` parameter must be specified. |
| 28 | +- `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. |
| 29 | +- `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. |
| 30 | + |
| 31 | +**Return Value:** |
| 32 | + |
| 33 | +This method modifies the original image in-place and does not return a new image. |
| 34 | + |
| 35 | +## Example |
| 36 | + |
| 37 | +This is the background image that will be used for the collage: |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +This is the foreground image that will be pasted onto the background: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +This example demonstrates how to paste one image onto another to create a collage: |
| 46 | + |
| 47 | +```py |
| 48 | +from PIL import Image |
| 49 | + |
| 50 | +# Open the background and foreground images |
| 51 | +background = Image.open('background.jpeg') |
| 52 | +foreground = Image.open('foreground.jpeg') |
| 53 | + |
| 54 | +# Resize the foreground image to fit better on the background |
| 55 | +foreground = foreground.resize((200, 200)) |
| 56 | + |
| 57 | +# Define the position where the foreground will be pasted (top-left corner) |
| 58 | +position = (100, 100) |
| 59 | + |
| 60 | +# Paste the foreground image onto the background |
| 61 | +background.paste(foreground, position) |
| 62 | + |
| 63 | +# Save the result |
| 64 | +background.save('collage_result.jpeg') |
| 65 | + |
| 66 | +# Display the image |
| 67 | +background.show() |
| 68 | +``` |
| 69 | + |
| 70 | +In this example, **foreground.jpeg** is pasted onto **background.jpeg** at the position `(100, 100)`. |
| 71 | + |
| 72 | +The code above produces the image below: |
| 73 | + |
| 74 | + |
0 commit comments