Percentage difference between images: Difference between revisions

Added Python 3
(Added Julia language)
(Added Python 3)
Line 1,133:
 
=={{header|Python}}==
You must install the [httphttps://wwwpillow.pythonwarereadthedocs.com/products/pilio/ Python Imaging Library] to use this example.
 
{{works with|python version 3.x}}
<lang python>import Image
 
i1 = Image.open("image1.jpg")
i2 = Image.open("image2.jpg")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
 
pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
 
ncomponents = i1.size[0] * i1.size[1] * 3
print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents</lang>
 
{{works with|python version 2.x}}
<lang python>from itertools import izip
Anonymous user