Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    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

    black and white rose

    Resultant Image

    pil imagemath eval
    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

    convert grayscale image

    Input Image 2

    convert grayscale image

    Output Image

    resultant grayscale 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

    convert grayscale image

    Input Image 2

    convert grayscale image

    Output Image

    logical or on entire images

    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

    convert grayscale image

    Input Image 2

    convert grayscale image

    Output Image

    imagemath eval
    python_pillow_function_reference.htm
    Advertisements