How to compare two imagefile from two different files in python

2024/10/9 0:51:09

I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or different. For example file 1 will have image1 and image 2 and image 3 , etc then file 2 will have image1,image2 and image3 etc . I need to do this python. How do I do this? Can someone help me? I am new to programming and I am new to python as well. I have tried the solution as below, but it did not work.

import cv2
import numpy as npfile1= "C:\Program Files (x86)\Python35-32\file1" 
file2="C:\Program Files (x86)\Python35-32\file2"

for f1 in file1:image1 = cv2.imread(f1)for f2 in file2:image2 = cv2.imread(f2)difference = cv2.subtract(image1, image2)

result = not np.any(difference) #if difference is all zeros it will return False

if result is True:print("The images are the same")else:cv2.imwrite("result.jpg", difference)print ("the images are different")

but the above code seems to not working as expected. I know the for loops is not correct. I am new to python. Can you please let me what I am doing wrong here, please?

Actually, I am using this for comparing screen taken by automation and manual testing on mobile devices. The files are *.png. I manage to get this working with below code.

the above code you need top provide the image1 and image 2 on the command prompt.But I want python to take from images from files one in one location and images from other location and compare automatically. If the images are same then it should print as zero as now the above code response. If they are different then it will be no zero. The issue I am facing how I could take from two files and compare one by one from scripts. Eg. File1\Image1.png ==File2\ image1.png

Answer

Use ImageMagick, it is available for Python and included on most Linux distros. Get familiar at the commandline first, then work it up into Python.

Create two directories

mkdir directory{1..2}

Create a black square in directory1

convert -size 128x128 xc:black directory1/1.png

enter image description here

Create a black square with a red 10x10 rectangle in directory2

convert -size 128x128 xc:black -fill red -draw "rectangle 0,0, 9,9"  directory2/2.png

enter image description here

Now ask ImageMagick to tell us how many pixels are different between the two images, -metric ae is the Absolute Error.

convert directory1/1.png directory2/2.png -metric ae -compare -format "%[distortion]" info:

Output

100

Note 1

If you want to allow the images to be nearly the same, you can add -fuzz 10% which will allow each pixel to differ by up to 10% from the corresponding pixel in the other image before counting it as different. This may be more useful when comparing JPEG images which may have slightly different quality/quantisation settings, and/or anti-aliasing, both of which cause images to differ slightly.

Note 2

You can shell out from Python and run shell scripts like the above using this... link

Note 3

If you create, say a red GIF and a red PNG, and copare them, they will come up identical, like this

# Create red GIF
convert -size 128x128 xc:red red.gif
# Create red PNG
convert -size 128x128 xc:red red.png
# Compare and find no difference
convert red.png red.gif  -metric ae -compare -format "%[distortion]" info:
0

despite the fact that the files theselves differ enormously

ls -l red*
-rw-r--r--  1 mark  staff  196  1 Apr 11:52 red.gif
-rw-r--r--  1 mark  staff  290  1 Apr 11:52 red.png
https://en.xdnf.cn/q/118653.html

Related Q&A

Cant import from module despite presence of __init__.py

I have the following folder structureproject_folder/pyutils/__init__.pyscript1.pyscript2.py lambdas/__init__.pylambda_script1.pylambda_script2.pylambda_tests/__init__.pylambda_test1.pyWithin lambda_tes…

Multiple strings in one variable

Say, for example, I have some code that goes like this: sentence = input("Enter input: ") target_letter = "a" or "b" or "c" print(sentence.index(target_letter))M…

How to get the area of shape filled using turtle

I graphed a fractal shape in Python using turtle, and am trying to get the area of this fractal after a sufficiently high iteration. This fractal is related to the Koch snowflake, for those interested.…

What distinguishes a command from needing () vs not?

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not…

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…

Python readline() is not reading a line with single space

I am reading a text file using readline(). My file contains below content with a space at second line:!" # $ % & When I print read values using-print("%s\n" % iso5_char)It prints-!&q…