How do I compare files?
Matimus
mccredie at gmail.com
Tue Jul 22 20:29:16 EDT 2008
On Jul 22, 4:27 pm, Clay Hobbs <c... at lakeserv.net> wrote:
> I am making a program that (with urllib) that downloads two jpeg files
> and, if they are different, displays the new one. I need to find a way
> to compare two files in Python. How is this done?
>
> -- Ratfink
Do you just want to check to see if they are the same? Or do you
actually want to know what the differences are?
import urllib
data1 = urllib.urlopen("http://url.of.jpg1").read()
data2 = urllib.urlopen("http://url.of.jpg2").read()
if data1 == data2:
print "they are the same"
Doing a regular text diff won't tell you much. You could use PIL and
do all sorts of image manipulation though. You might generate an image
of the difference between each pixel.
Something like this:
import Image
import ImageChops
import urllib
data1 = urllib.urlopen("http://url.of.jpg1").read()
data2 = urllib.urlopen("http://url.of.jpg2").read()
im1 = Image.fromstring(data1)
im2 = Image.fromstring(data2)
result = ImageChops.difference(im1, im2)
result.save("result_image.jpg")
Read up on PIL:
http://www.pythonware.com/library/pil/handbook/index.htm
Matt
More information about the Python-list
mailing list