Merge pull request #2279 from uploadcare/fast-assert_image_similar

Fast assert_image_similar
This commit is contained in:
wiredfool 2016-12-13 19:52:50 +00:00 committed by GitHub
commit 86ff654487
4 changed files with 25 additions and 11 deletions

View File

@ -7,6 +7,21 @@ import tempfile
import os import os
import unittest import unittest
from PIL import Image, ImageMath
def convert_to_comparable(a, b):
new_a, new_b = a, b
if a.mode == 'P':
new_a = Image.new('L', a.size)
new_b = Image.new('L', b.size)
new_a.putdata(a.getdata())
new_b.putdata(b.getdata())
elif a.mode == 'I;16':
new_a = a.convert('I')
new_b = b.convert('I')
return new_a, new_b
class PillowTestCase(unittest.TestCase): class PillowTestCase(unittest.TestCase):
@ -80,14 +95,13 @@ class PillowTestCase(unittest.TestCase):
a.size, b.size, a.size, b.size,
msg or "got size %r, expected %r" % (a.size, b.size)) msg or "got size %r, expected %r" % (a.size, b.size))
a, b = convert_to_comparable(a, b)
diff = 0 diff = 0
try: for ach, bch in zip(a.split(), b.split()):
ord(b'0') chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert('L')
for abyte, bbyte in zip(a.tobytes(), b.tobytes()): diff += sum(i * num for i, num in enumerate(chdiff.histogram()))
diff += abs(ord(abyte)-ord(bbyte))
except:
for abyte, bbyte in zip(a.tobytes(), b.tobytes()):
diff += abs(abyte-bbyte)
ave_diff = float(diff)/(a.size[0]*a.size[1]) ave_diff = float(diff)/(a.size[0]*a.size[1])
self.assertGreaterEqual( self.assertGreaterEqual(
epsilon, ave_diff, epsilon, ave_diff,

Binary file not shown.

View File

@ -31,7 +31,7 @@ class TestFileMsp(PillowTestCase):
# Assert # Assert
self.assertEqual(im.size, (128, 128)) self.assertEqual(im.size, (128, 128))
self.assert_image_similar(im, hopper("1"), 4) self.assert_image_equal(im, hopper("1"), 4)
def test_cannot_save_wrong_mode(self): def test_cannot_save_wrong_mode(self):
# Arrange # Arrange

View File

@ -205,7 +205,7 @@ class TestImageCms(PillowTestCase):
target = Image.open('Tests/images/hopper.Lab.tif') target = Image.open('Tests/images/hopper.Lab.tif')
self.assert_image_similar(i, target, 30) self.assert_image_similar(i, target, 3.5)
def test_lab_srgb(self): def test_lab_srgb(self):
psRGB = ImageCms.createProfile("sRGB") psRGB = ImageCms.createProfile("sRGB")