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 - ImageChops.subtract() Function



    The PIL.ImageChops.subtract() function is used to subtract one image (image2) from another image (image1). After the subtraction, the result is divided by a specified scale and then an offset is added. The mathematical representation of this operation is given by the formula −

    $$\mathrm{out\:=\:((image1\:-\:image2)/scale\:+\:offset)}$$

    Similar to ImageChops.add, this operation is not reversible due to the data type being uint8. When using ImageChops.subtract, if the resulting value is negative, it is clipped to zero. This clipping to the range [0, 255] leads to data loss, and the original image cannot be accurately reconstructed from the result.

    Syntax

    Following is the syntax of the function −

    PIL.ImageChops.subtract(image1, image2, scale=1.0, offset=0)
    

    Parameters

    Here are the details of this function parameters −

    • image1 − This is the first input image.

    • image2 − This is the second input image.

    • scale (optional, default value: 1.0) − Scale is a factor by which the result of subtracting image1 and image2 is divided. If you omit this parameter, it defaults to 1.0.

    • offset (optional, default value: 0.0) − Offset is a value that is added to the result after the division by the scale. If you don't provide an offset value, it defaults to 0.0.

    Return Value

    This Function returns the Image object.

    Examples

    Example 1

    In this example, two random RGB images (image1 and image2) are created using NumPy arrays. Then the ImageChops.subtract function is applied to see how the pixel values of the two images are represented in the output image after performing the subtraction.

    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([(255, 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)))
    
    # Subtract the two images
    result = ImageChops.subtract(image1, image2)
    print("Pixel values of the result at (0, 0) after subtraction:", result.getpixel((0, 0)))
    

    Output

    Pixel values of image1 at (0, 0): (235, 0, 0)
    Pixel values of image2 at (0, 0): (255, 0, 0)
    Pixel values of the result at (0, 0) after subtraction: (0, 0, 0)
    

    We can observe the output, the pixel at (0, 0) is (0, 0, 0), as the subtraction (235 - 255) is clipped to 0.

    Example 2

    The following example subtract the second image from the first with default parameters (scale=1.0, offset=0.0).

    from PIL import Image, ImageChops
    
    # Open two image files
    image1 = Image.open('Images/TP logo.jpg')
    image2 = Image.open('Images/Rose_.jpg')
    
    # Subtract the second image from the first with default parameters (scale=1.0, offset=0.0)
    result = ImageChops.subtract(image1, image2)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    

    Output

    Input Image 1

    tp logo

    Input Image 2

    rose

    Output Image

    imagechops subtract

    Example 3

    Here is an example that subtracts the second image from the first with specific scale and offset values.

    from PIL import Image, ImageChops
    
    # Open two image files
    image1 = Image.open('Images/Rose_.jpg')
    image2 = Image.open('Images/TP logo.jpg')
    
    # Subtract the second image from the first with specific scale and offset values
    result = ImageChops.subtract(image1, image2, scale=5.0, offset=100)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    

    Output

    Input Image 1

    rose

    Input Image 2

    tp logo

    Output Image

    chops subtract
    python_pillow_function_reference.htm
    Advertisements