Python 3 Script to Compare Two Images For Similarity or Equality Using OpenCV and Numpy Library Full Project For Beginners

 

Welcome folks today in this blog post we will be comparing two images in python for similarity or they are equal or not using opencv and numpy library. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

In order to get started you need to install the below libraries using the pip command as shown below

 

 

pip install cv2

 

pip install numpy

 

 

Now we need to make an app.py file and copy paste the following code

 

 

app.py

 

 

import cv2
import numpy as np

original = cv2.imread("profile.jpg")
duplicate = cv2.imread("second.jpg")# 1) Check if 2 images are equals
if original.shape == duplicate.shape:
  print("The images have same size and channels")
  difference = cv2.subtract(original, duplicate)
  b, g, r = cv2.split(difference)

  if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
    print("The images are completely Equal")
    
else:
  print("the images are not equal")

 

Leave a Reply