Image Subtraction Using Pillow

Overview:

  • Subtracting an image from another image results in an image with the differences between the two.
  • Image subtraction has its applications in variety of fields including
  • Medical Imaging
  • Astrophotography
  • Remote sensing

Example:

# Python example program for image subtraction

from PIL import Image

import numpy as np

 

# Paths of two image frames

image1Path = "./windshield1.jpg";

image2Path = "./windshield2.jpg";

 

# Open the images

image1     = Image.open(image1Path);

image2     = Image.open(image2Path);

 

# Get the image buffer as ndarray

buffer1    = np.asarray(image1);

buffer2    = np.asarray(image2);

 

# Subtract image2 from image1

buffer3    = buffer1 - buffer2;

 

# Construct a new Image from the resultant buffer

differenceImage     = Image.fromarray(buffer3);

 

# Display all the images including the difference image

image1.show();

image2.show();

differenceImage.show();

 

Output:

Input Image 1:

Input1 to Image Subtraction using Pillow, the Python image processing library.

Input Image 2:

Input2 to Image Subtraction using Pillow, the Python image processing library.

Image1 - Image2:

Output image of Image subtraction using Pillow - The Python Image Processing Library


Copyright 2020 © pythontic.com