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

    Selected Reading

    Python Pillow - ImageChops.difference() Function



    The PIL.ImageChops.difference function computes the absolute value of the pixel-by-pixel difference between two images. It returns a new image where each pixel represents the absolute value of the difference between the corresponding pixels in the input images.

    The operation is defined as followes −

    $$\mathrm{out\:=\:abs(image1\:-\:image2)}$$

    Syntax

    Following is the syntax of the function −

    PIL.ImageChops.difference(image1, image2)
    

    Parameters

    Here are the details of this function parameters −

    • image1 − The first input image.

    • image2 − The second input image.

    Return Value

    The return type of this function is an Image.

    Examples

    Example 1

    In this example, we will use the getpixel() function to observe the resultant pixel values after applying the ImageChops.difference() function on two input images.

    from PIL import Image, ImageChops
    import numpy as np
    
    # Create two random RGB images
    image1 = Image.fromarray(np.array([(235, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)]), mode="RGB")
    print("Pixel values of image1 at (0, 0):", image1.getpixel((0, 0)))
    
    image2 = Image.fromarray(np.array([(25, 14, 3), (25, 222, 0), (255, 155, 0), (255, 55, 100), (180, 0, 78)]), mode="RGB")
    print("Pixel values of image2 at (0, 0):", image2.getpixel((0, 0)))
    
    # Compute the absolute pixel-wise difference between the two images
    result = ImageChops.difference(image1, image2)
    print("Pixel value in result at (0, 0) after difference:", result.getpixel((0, 0)))
    

    Output

    Pixel values of image1 at (0, 0): (235, 0, 0)
    Pixel values of image2 at (0, 0): (25, 0, 0)
    Pixel value in result at (0, 0) after difference: (210, 0, 0)
    

    Example 2

    In this examples, the ImageChops.difference() function is used to compute the absolute pixel-wise difference between two JPEG images (image1 and image2).

    from PIL import Image, ImageChops
    import numpy as np
    
    # Open two image files
    image1 = Image.open('Images/butterfly.jpg')
    image2 = Image.open('Images/flowers.jpg')
    
    # Compute the absolute pixel-wise difference between the two images
    result = ImageChops.difference(image1, image2)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    

    Output

    Input Image 1

    butterfly on flower

    Input Image 2

    sun rays on pink flower

    Output Image

    imagechops difference

    Example 3

    In this example, the ImageChops.difference() function is used on two JPEG image files to compute the absolute pixel-wise difference between the two images.

    from PIL import Image, ImageChops
    
    # Open two image files
    image1 = Image.open('Images/pillow-logo.png')
    image2 = Image.open('Images/test_img.png')
    
    # Compute the absolute pixel-wise difference between the two images
    result = ImageChops.difference(image1, image2)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    
    # Print the pixel values at a specific location (e.g., (100, 100))
    print("Pixel value in image1 at (100, 100):", image1.getpixel((100, 100)))
    print("Pixel value in image2 at (100, 100):", image2.getpixel((100, 100)))
    print("Pixel value in result at (100, 100) after difference:", result.getpixel((100, 100)))
    

    Output

    Input Image 1

    pillow logo s

    Input Image 2

    test img

    Output Image

    chops difference
    Pixel value in image1 at (100, 100): (183, 84, 131, 255)
    Pixel value in image2 at (100, 100): (174, 65, 9, 255)
    Pixel value in result at (100, 100) after difference: (9, 19, 122, 0)
    
    python_pillow_function_reference.htm
    Advertisements