Skip to content

Commit b36fc65

Browse files
authored
[Term Entry] Python:Pillow Image: .merge()
* hoping it works * trying to get main to the basics * delete unwanted * hopedully not messing up * the folder and subsequent folder for merge created * have started inputing * continuing with the work * Updated the file * Nearly completed * ready for review * Is it working * updated the page per recommendation * Fix file changes * Update merge.md * Minor changes ---------
1 parent 66fde8c commit b36fc65

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
Title: '.merge()'
3+
Description: 'Merges set of single-band images into a new multi-band image.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Computer Vision'
9+
- 'Images'
10+
- 'Pillow'
11+
- 'Values'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.merge()`** function in Pillow combines multiple single-band images into a new multi-band image. This is particularly useful when working with multispectral or multichannel images, such as RGB or CMYK images, where individual channels can be processed separately before merging them into a final image.
18+
19+
## Syntax
20+
21+
```pseudo
22+
Image.merge(mode, bands)
23+
```
24+
25+
- `mode`: The mode of the new multi-band image (e.g., `"RGB"`, `"CMYK"`, etc.).
26+
- `bands`: A tuple containing the individual image bands to be merged. Each band should be a single-channel (grayscale) image.
27+
28+
## Example
29+
30+
The image to be used for this example is:
31+
32+
![Boston Skyline](https://raw.githubusercontent.com/Codecademy/docs/main/media/Boston.jpg)
33+
34+
The code below splits the above image into its individual bands and then merges them in a different order:
35+
36+
```py
37+
from PIL import Image
38+
39+
# Open the image
40+
image1= Image.open('media/Boston.jpg')
41+
im = image1.resize((400,400))
42+
43+
# Split the image into its RGB channels
44+
r,g,b = im.split()
45+
46+
# Merge the bands in a different order
47+
new_image= Image.merge('RGB', (b,r,g))
48+
new_image.show()
49+
```
50+
51+
The output will be:
52+
53+
![Merged Boston Skyline](https://raw.githubusercontent.com/Codecademy/docs/main/media/merged-boston.png)

media/Boston.jpg

3.01 MB
Loading

media/merged-boston.png

279 KB
Loading

0 commit comments

Comments
 (0)