
- 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 - ImageMath.eval() Function
The Pillow library provides a function called eval() within its ImageMath module, to evaluate image expressions. It allows you to perform operations, such as arithmetic, bitwise, and logical operations, on images. The module supports standard Python expression syntax with additional functions provided by the Pillow library.
The PIL.ImageMath.eval() function evaluates an expression in a given environment. This function supports standard Python expression syntax along with additional functions for image manipulation.
It is important to note that, the ImageMath module currently gives support for single-layer images only. For processing multi-band images, you should use the split() method or merge() function.
Syntax
Following is the syntax of the function −
PIL.ImageMath.eval(expression, environment)
Parameters
Here are the details of this function parameters −
expression − A string representing a Python expression.
environment − A dictionary or keyword arguments mapping image names to Image instances.
Return Value
The function returns an image, an integer value, a floating-point value, or a pixel tuple, depending on the expression.
Examples
Example 1
This example demonstrates how to evaluate an expression with a standard arithmetical operator using the eval() function of the ImageMath module.
from PIL import Image, ImageMath # Open an image and convert it to grayscale image1 = Image.open("Images/black rose.jpg").convert('L') # Evaluate the expression 'a + 100' to invert pixel values resultant_image = ImageMath.eval('a + 100', a=image1) # Display the original and resultant images image1.show() resultant_image.show() print('Evaluated the expression with standard arithmetical operator successfully...')
Output
Input Image

Resultant Image

Evaluated the expression with standard arithmetical operator successfully...
Example 2
Here is an example that demonstrates how to evaluate an expression with a Bitwise operator using the eval() function.
It is important to note that Bitwise operators are not applicable to floating-point images.
from PIL import Image, ImageMath # Open and convert the first grayscale image image1 = Image.open("Images/ColorDots.png").convert('L') # Open and convert the second grayscale image image2 = Image.open("Images/TP-W.png").convert('L') # Apply bitwise AND operation on the two images resultant_image = ImageMath.eval('a & b', a=image1, b=image2) # Display the original images and the result image1.show() image2.show() resultant_image.show() print('Evaluated the expression of the Bitwise operator successfully...')
Output
Input Image 1

Input Image 2

Output Image

Example 3
The following example applies the logical or operator on the entire images.
from PIL import Image, ImageMath # Open and convert the first grayscale image image1 = Image.open("Images/ColorDots.png").convert('L') # Open and convert the second grayscale image image2 = Image.open("Images/TP-W.png").convert('L') # Use the logical "or" operation on the entire images resultant_image = ImageMath.eval('(a or b)', a=image1, b=image2) # Display the original images and the result image1.show() image2.show() resultant_image.show() print('Evaluated the expression using the logical OR successfully...')
Output
Input Image 1

Input Image 2

Output Image

Example 3
The following example evaluates an expression with a Built-In function using the ImageMath.eval() function.
from PIL import Image, ImageMath # Open and convert the first grayscale image image1 = Image.open("Images/ColorDots.png").convert('L') # Open and convert the second grayscale image image2 = Image.open("Images/TP-W.png").convert('L') # Evaluate the expression "min(a, b)" using ImageMath resultant_image = ImageMath.eval('min(a, b)', a=image1, b=image2) # Display the original images and the result image1.show() image2.show() resultant_image.show() print('Evaluated the expression using the Built-in Functions successfully...')
Output
Input Image 1

Input Image 2

Output Image
