Skip to content

[Term Entry] Python Pillow - Image: .merge() #6355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions content/pillow/concepts/image/terms/merge/merge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
Title: '.merge()'
Description: 'Merges set of single-band images into a new multi-band image.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Computer Vision'
- 'Images'
- 'Pillow'
- 'Values'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

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.

## Syntax

```pseudo
Image.merge(mode, bands)
```

- `mode`: The mode of the new multi-band image (e.g., `"RGB"`, `"CMYK"`, etc.).
- `bands`: A tuple containing the individual image bands to be merged. Each band should be a single-channel (grayscale) image.

## Example

The image to be used for this example is:

![Boston Skyline](https://raw.githubusercontent.com/Codecademy/docs/main/media/Boston.jpg)

The code below splits the above image into its individual bands and then merges them in a different order:

```py
from PIL import Image

# Open the image
image1= Image.open('media/Boston.jpg')
im = image1.resize((400,400))

# Split the image into its RGB channels
r,g,b = im.split()

# Merge the bands in a different order
new_image= Image.merge('RGB', (b,r,g))
new_image.show()
```

The output will be:

![Merged Boston Skyline](https://raw.githubusercontent.com/Codecademy/docs/main/media/merged-boston.png)
Binary file added media/Boston.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/merged-boston.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.