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

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

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

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")
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment