Hi ,

I would need to compare two jpg files using python. Could some one help me, how I can do this

Thanks

Do you just want to know if they are equal? If that is the case, you can simply use:

# compare two files and check if they are equal
# files can be binary or text based

import filecmp

# pick two files you want to compare ...
file1 = "Boing1.wav"
file2 = "Boing2.wav"
if filecmp.cmp(file1, file2):
    print "Files %s and %s are identical" % (file1, file2)
else:
    print "Files %s and %s differ!" % (file1, file2)

First you have to define "equal". If it means any difference at all then first, see if the two files have the same length. If not, then they are not equal, If they are the same length, open each one in binary mode and read a fixed number of bytes from each, then compare for equality. Repeat until the end of file is reached or they are not equal, in which case you can print or return where they differ.

Thanks for the replies ! I basically want to validate and say both the jpg files are same. I will try out this option and let you know about this ! Thanks again

Nice One !
Thanks.

I want to know how we can automate this by picking up files from one machine's drive and compare with files on another machine's drive .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.