Skip to content

Commit da2049d

Browse files
[Term Entry] Python Pillow - Image: .composite() (#6375)
* [Term Entry] Python Pillow - Image: .composite() * Add files via upload * corrected links * Update content/pillow/concepts/image/terms/composite/composite.md * Update content/pillow/concepts/image/terms/composite/composite.md * added doc links and applied suggestions * fixed formatting ---------
1 parent 8b01f1d commit da2049d

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
Title: '.composite()'
3+
Description: 'Creates a composite image by blending two images using a transparency mask.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Images'
9+
- 'Libraries'
10+
- 'Pillow'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.composite()`** method creates composite images by blending two images using a transparency mask. This powerful feature allows selective combination of visual elements from different sources using a third image as a mask to control the blending.
17+
18+
`.composite()` method is particularly useful for photo manipulation, creative design work, and generating custom visuals where precise control over image composition is needed. Common applications include creating collages, applying watermarks selectively, generating special visual effects, or combining elements from multiple images.
19+
20+
## Syntax
21+
22+
```pseudo
23+
Image.composite(image1, image2, mask)
24+
```
25+
26+
**Parameters:**
27+
28+
- `image1`: The first image to be composited.
29+
- `image2`: The second image to be composited. Must have the same mode and size as the first image.
30+
- `mask`: A grayscale or transparency mask that determines how image1 and image2 are blended. The mask can have:
31+
- `"L"` mode (grayscale): Uses pixel values (0–255) to control blending.
32+
- `"1"` mode (binary): Only allows fully visible (white) or fully hidden (black) areas.
33+
- `"RGBA"` mode: Uses the alpha channel (A) for blending but requires correct transparency values.
34+
35+
**Return value:**
36+
37+
The method returns a new composite image.
38+
39+
## `.composite()` Vs. `.paste()` Vs. `.alpha_composite()`
40+
41+
Each of these Pillow methods is used for combining images but works differently:
42+
43+
- `.composite()` blends two images using a separate mask, allowing selective transparency.
44+
- `.paste()` directly overlays one image onto another at a specified position, optionally using a mask.
45+
- `.alpha_composite()` merges images based on their alpha channels, ensuring smooth transparency handling.
46+
47+
For a deeper dive into each method, refer to their respective entries:
48+
49+
- [`.paste()`](https://www.codecademy.com/resources/docs/pillow/image/alpha-composite)
50+
- [`.alpha_composite()`](https://www.codecademy.com/resources/docs/pillow/image/alpha-composite)
51+
52+
## Example 1: Basic Image Compositing Using a Mask
53+
54+
This example demonstrates how to composite two images using a binary mask. The mask determines which parts of each image will appear in the final result:
55+
56+
```py
57+
from PIL import Image, ImageDraw
58+
59+
# Open the two images to be composited
60+
image1 = Image.open('mountain.jpg')
61+
image2 = Image.open('ocean.jpg').resize(image1.size)
62+
63+
# Create a simple mask (grayscale mode 'L')
64+
mask = Image.new('L', image1.size, 0)
65+
draw = ImageDraw.Draw(mask) # Create a drawing object
66+
67+
# Draw a white circle in the center (visible area for image1)
68+
draw.ellipse((150, 100, 350, 300), fill=255)
69+
70+
# Composite the images using the mask
71+
result = Image.composite(image1, image2, mask)
72+
73+
# Display the result
74+
result.show()
75+
76+
# Save the result
77+
result.save('composite_result.jpg')
78+
```
79+
80+
The output for this example will be as follows:
81+
82+
![A circular masked composite image blending a mountain and ocean scene](https://raw.githubusercontent.com/Codecademy/docs/main/media/composite_output1.jpg)
83+
84+
This example creates a composite where `image1` appears inside the white circle area of the mask, while `image2` appears in the black areas. The result is a seamless blend of both images according to the mask pattern.
85+
86+
## Example 2: Creating Smooth Transitions with Gradient Masks
87+
88+
This example shows how to create a gradient mask for a smoother transition between images, ideal for creative blending effects:
89+
90+
```py
91+
import numpy as np
92+
from PIL import Image, ImageDraw, ImageFilter
93+
94+
# Open the source images
95+
image1 = Image.open('mountain.jpg')
96+
image2 = Image.open('ocean.jpg').resize(image1.size)
97+
98+
# Create a horizontal gradient mask (black to white)
99+
mask_array = np.tile(np.linspace(0, 255, image1.width), (image1.height, 1))
100+
mask = Image.fromarray(mask_array.astype('uint8'))
101+
102+
# Apply a slight blur to make the transition smoother
103+
mask_blur = mask.filter(ImageFilter.GaussianBlur(20))
104+
105+
# Composite the images
106+
result = Image.composite(image1, image2, mask_blur)
107+
108+
# Display the result
109+
result.show()
110+
111+
# Save the result
112+
result.save('gradient_composite.jpg')
113+
```
114+
115+
The output for this example will be as follows:
116+
117+
![A gradient-blended composite image transitioning from ocean to mountain](https://raw.githubusercontent.com/Codecademy/docs/main/media/composite_output2.jpg)
118+
119+
This creates a gradual transition from `image2` on the left to `image1` on the right. The blur applied to the mask softens the transition, resulting in a more natural blend between the images.
120+
121+
## Example 3: Creating a Shape-Based Composite for Custom Masks
122+
123+
This example demonstrates using a custom shape as a mask to create visually interesting compositions:
124+
125+
```py
126+
from PIL import Image, ImageDraw, ImageFilter
127+
128+
# Open the source images
129+
portrait = Image.open('mountain.jpg')
130+
scene = Image.open('ocean.jpg').resize(portrait.size)
131+
132+
# Create a custom shape mask
133+
mask = Image.new('L', portrait.size, 0)
134+
draw = ImageDraw.Draw(mask)
135+
136+
# Create a heart shape
137+
heart_points = [
138+
(200, 100), (150, 50), (100, 100),
139+
(100, 150), (200, 250), (300, 150),
140+
(300, 100), (250, 50), (200, 100)
141+
]
142+
draw.polygon(heart_points, fill=255)
143+
144+
# Apply a blur to soften the edges
145+
mask_blur = mask.filter(ImageFilter.GaussianBlur(5))
146+
147+
# Composite the images
148+
result = Image.composite(portrait, scene, mask_blur)
149+
150+
# Display the result
151+
result.show()
152+
153+
# Save the result
154+
result.save('heart_composite.jpg')
155+
```
156+
157+
The output for this example will be as follows:
158+
159+
![A heart-shaped masked composite image blending an ocean and mountain scene](https://raw.githubusercontent.com/Codecademy/docs/main/media/composite_output3.jpg)
160+
161+
This example creates a heart-shaped window where the portrait image appears within the heart shape, while the landscape image fills the rest of the canvas. The blurred mask creates soft edges for a polished look.

media/composite_output1.jpg

16 KB
Loading

media/composite_output2.jpg

19.9 KB
Loading

media/composite_output3.jpg

15.7 KB
Loading

0 commit comments

Comments
 (0)