Compare two or more images that image is how much similar to each other
By: Sonu Gupta
Tags: Image Processing, Numpy, Pillow, Matplotlib, Python

Maching Images in percentage

I am new to writing blog but not in programming. This is a small kind of program to compare two or more images that they are how much similier to each other. As you can see in the picture when two images are same at that time the percentage of similarity is 100 % that means program acuracy is 100 % and if two images are not same to each other at that time the similarity percentage are on the matching of pixcel.

So now the time to see the code that how it’s work and how to compare the images.To compare the images i use the PIL(Pillow) and numpy library. Let’s understand the code.

from PIL import ImageChops, Image

Here Image used to read the images form your device and the first import ImageChops is used to compare the images but we cann’t direct give that class an image object we have to convert that images to array for that i used numpy array. So how images is going to convert into array for that when we read the images at that time we can use the histogram() function of image object so it return the pixcel as number and that number is going to create our numpy array here is the code for that.

import numpy as np



im1 = Image.open("image_url")

x = np.array(im1.histogram())

here we convert our first images into array as same we can read and create second image into array.

import numpy as np



im2 = Image.open("image_url")

y = np.array(im2.histogram())

here our both images is converted into array, and now it’s time to compare two images for that we use ImageChops.difference() function here is the code.

diff = ImageChops.difference(im1, im2).getbbox()

That is the main code of the program that is you need to understand in the code now below is hole code that going to compare the images you just have to copy and paset the code and install the required library.

from PIL import ImageChops, Image

import matplotlib.pyplot as plt

import numpy as np

actual_error = 0

im1 = Image.open("image_url")

x = np.array(im1.histogram())



im2 = Image.open("image_url")

y = np.array(im2.histogram())



try:

if len(x) == len(y):

error = np.sqrt(((x - y) ** 2).mean())

error = str(error)[:2]

actual_error = float(100) - float(error)

diff = ImageChops.difference(im1, im2).getbbox()

if diff:

print("Not Duplicate Image")

print('Matching Images In percentage: ', actual_error,'\t%' )

f = plt.figure()

text_lable = str("Matching Images Percentage" + str(actual_error)+"%")

plt.suptitle(text_lable)

f.add_subplot(1,2, 1)

plt.imshow(im1)

f.add_subplot(1,2, 2)

plt.imshow(im2)

plt.show(block=True)

else:

print("Duplicate Image")

print('Matching Images In percentage: ', actual_error,'%' )

f = plt.figure()

text_lable = str("Matching Images Percentage" + str(actual_error)+"%")

plt.suptitle(text_lable)

f.add_subplot(1,2, 1)

plt.imshow(im1)

f.add_subplot(1,2, 2)

plt.imshow(im2)

plt.show(block=True)



except ValueError as identifier:

f = plt.figure()

text_lable = str("Matching Images Percentage " + str(actual_error)+"%")

plt.suptitle(text_lable)

f.add_subplot(1,2, 1)

plt.imshow(im1)

f.add_subplot(1,2, 2)

plt.imshow(im2)

plt.show(block=True)

print('identifier: ', identifier)

In above code you can customized the code and create your own and better project using this program. if any suggesion than please comment and if you like this post than share it.