Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding Difference between Images using PIL
In image processing, finding the difference between two images is a crucial step in various applications. It is essential to understand the difference between the two images, which can help us in detecting changes, identifying objects, and other related applications. In this tutorial, we will explore how to find the difference between two images using the Python Imaging Library (PIL).
Installation
To use PIL, we need to install it using the pip package manager ?
pip install pillow
Basic Syntax
To find the difference between two images using PIL, we can use the ImageChops module. The ImageChops module provides various operations on images, including finding the difference between two images ?
from PIL import Image, ImageChops
# Open two images
img1 = Image.open('image1.jpg')
img2 = Image.open('image2.jpg')
# Find the difference
diff = ImageChops.difference(img1, img2)
# Display the difference
diff.show()
How It Works
The ImageChops.difference() function works by comparing each pixel in both images and calculating the absolute difference between their values. The result is a new image where:
White pixels represent areas where the images differ significantly
Black pixels represent areas where the images are identical
Gray pixels represent minor differences between the images
Note: Both images should have the same dimensions for accurate comparison. If they differ in size, you should resize them first.
Example 1: Basic Image Difference
Let's create two simple images and find their difference ?
from PIL import Image, ImageDraw, ImageChops
# Create two similar images with slight differences
img1 = Image.new('RGB', (200, 200), 'white')
img2 = Image.new('RGB', (200, 200), 'white')
# Add shapes to both images
draw1 = ImageDraw.Draw(img1)
draw2 = ImageDraw.Draw(img2)
# Circle in first image
draw1.ellipse([50, 50, 150, 150], fill='red')
# Circle in second image (slightly different position)
draw2.ellipse([60, 60, 160, 160], fill='red')
# Find the difference
diff = ImageChops.difference(img1, img2)
# Convert to grayscale for better visibility
diff_gray = diff.convert('L')
print("Difference image created successfully")
print("Image mode:", diff_gray.mode)
print("Image size:", diff_gray.size)
Difference image created successfully Image mode: L Image size: (200, 200)
Example 2: Working with Real Images
Here's how to compare two actual image files with resizing and thresholding ?
from PIL import Image, ImageChops
# Create sample images for demonstration
img1 = Image.new('RGB', (300, 300), 'lightblue')
img2 = Image.new('RGB', (300, 300), 'lightblue')
# Add different elements to each image
from PIL import ImageDraw
draw1 = ImageDraw.Draw(img1)
draw2 = ImageDraw.Draw(img2)
draw1.rectangle([50, 50, 150, 150], fill='yellow')
draw2.rectangle([100, 100, 200, 200], fill='yellow')
# Resize images to ensure same dimensions
img1 = img1.resize((400, 400))
img2 = img2.resize((400, 400))
# Find the difference
diff = ImageChops.difference(img1, img2)
# Apply threshold to highlight significant differences
threshold = 50
diff_threshold = diff.point(lambda x: 0 if x < threshold else 255)
print("Original image size:", img1.size)
print("Difference image created with threshold:", threshold)
Original image size: (400, 400) Difference image created with threshold: 50
Parameters and Methods
| Method | Purpose | Return Value |
|---|---|---|
ImageChops.difference() |
Find absolute difference between images | New Image object |
Image.resize() |
Change image dimensions | Resized Image object |
Image.point() |
Apply threshold function to pixels | Modified Image object |
Common Use Cases
Change Detection: Monitor surveillance footage to detect movement or changes in scenes
Medical Imaging: Compare medical scans to identify tumors or track disease progression
Quality Control: Inspect manufactured products for defects or variations
Forensic Analysis: Compare evidence photos to reveal important details
Image Alignment: Verify proper registration between multiple images
Key Points
Images must have identical dimensions for accurate comparison
Use
resize()to match image sizes before comparisonApply thresholding with
point()to highlight significant differencesConvert to grayscale for better difference visualization
The result image shows differences as bright pixels and similarities as dark pixels
Conclusion
PIL's ImageChops module provides an efficient way to find differences between images using the difference() method. This technique is valuable for change detection, quality control, and forensic analysis applications where identifying visual differences is crucial.
