![Python Pillow Tutorial](/python_pillow/images/python-pillow-mini-logo.jpg)
- Python Pillow Tutorial
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - Image Blending
Image blending is a process of combining or mixing two images to create a new image. One common method for image blending is to use alpha blending. In alpha blending, each pixel in the result is computed based on a weighted sum of the corresponding pixels in the input images. The alpha channel, representing transparency, is used as the weight factor.
This technique is commonly used in graphics, image processing, and computer vision to achieve various visual effects.
The Python Pillow library provides the blend() function within its Image module to perform the blending operation on images.
The Image.blend() function
This function provides a convenient way to create a smooth transition between two images by specifying a blending factor (alpha). The function creates a new image by interpolating between two input images using a constant alpha value. The interpolation is performed according to the formula −
out=image1×(1.0−alpha)+image2×alpha
Following is the syntax of the function −
PIL.Image.blend(im1, im2, alpha)
Where −
im1 − The first image.
im2 − The second image. It must have the same mode and size as the first image.
alpha − The interpolation alpha factor. If alpha is 0.0, a copy of the first image is returned. If alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range.
Example
Let's see a basic example of blending two images using the Image.blend() method.
from PIL import Image # Load two images image1 = Image.open("Images/ColorDots.png") image2 = Image.open("Images/pillow-logo-w.png") # Blend the images with alpha = 0.5 result = Image.blend(image1, image2, alpha=0.5) # Display the input and resultant iamges image1.show() image2.show() result.show()
Input Images
![color dots](/python_pillow/images/color_dots.jpg)
![pillow logo](/python_pillow/images/pillow_logo.jpg)
Output
![resultant images](/python_pillow/images/resultant_images.jpg)
Example
Here is an example that demonstrates the use of PIL.Image.blend() with alpha values 2.
from PIL import Image # Load two images image1 = Image.open("Images/ColorDots.png") image2 = Image.open("Images/pillow-logo-w.png") # Blend the images with alpha = 2 result = Image.blend(image1, image2, alpha=2) # Display the input and resultant iamges image1.show() image2.show() result.show()
Input Images
![color dots](/python_pillow/images/color_dots.jpg)
![pillow logo](/python_pillow/images/pillow_logo.jpg)
Output
![image blend](/python_pillow/images/image_blend.jpg)
Example
Here is an example that demonstrates the use of PIL.Image.blend() with alpha value 1.0. It will return a copy of the second image.
from PIL import Image # Load two images image1 = Image.open("Images/ColorDots.png") image2 = Image.open("Images/pillow-logo-w.png") # Blend the images with alpha = 2 result = Image.blend(image1, image2, alpha=1.0) # Display the input and resultant iamges image1.show() image2.show() result.show()
Input Images
![color dots](/python_pillow/images/color_dots.jpg)
![pillow logo](/python_pillow/images/pillow_logo.jpg)
Output
![pillow logo](/python_pillow/images/pillow_logo.jpg)