2012-04-04 23:54:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-03-08 18:29:46 +01:00
|
|
|
#import math, operator
|
2012-02-18 17:51:18 +01:00
|
|
|
import Image
|
|
|
|
import sys
|
|
|
|
|
|
|
|
COMPUTE_THRESHOLD = 16
|
|
|
|
|
|
|
|
errors = []
|
2012-04-04 23:54:07 +02:00
|
|
|
passed = 0
|
2012-02-18 17:51:18 +01:00
|
|
|
|
|
|
|
# returns true if pixels are not identical
|
|
|
|
def compare_pixels(pixel1, pixel2):
|
|
|
|
r_diff = abs(pixel1[0] - pixel2[0])
|
|
|
|
g_diff = abs(pixel1[1] - pixel2[1])
|
|
|
|
b_diff = abs(pixel1[2] - pixel2[2])
|
|
|
|
if(r_diff > COMPUTE_THRESHOLD or g_diff > COMPUTE_THRESHOLD or b_diff > COMPUTE_THRESHOLD):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# compare tow images and return number of different pixels
|
|
|
|
def compare(fn1, fn2):
|
|
|
|
global errors
|
2012-04-04 23:54:07 +02:00
|
|
|
global passed
|
2012-02-18 17:51:18 +01:00
|
|
|
im1 = Image.open(fn1)
|
2012-02-20 17:42:12 +01:00
|
|
|
try:
|
|
|
|
im2 = Image.open(fn2)
|
|
|
|
except IOError:
|
2012-06-07 22:26:50 +02:00
|
|
|
errors.append((None, fn1, fn2))
|
2012-02-20 17:42:12 +01:00
|
|
|
return -1
|
2012-02-18 17:51:18 +01:00
|
|
|
diff = 0
|
|
|
|
pixels = im1.size[0] * im1.size[1]
|
2012-03-08 18:29:46 +01:00
|
|
|
delta_pixels = im2.size[0] * im2.size[1] - pixels
|
|
|
|
if delta_pixels != 0:
|
2012-06-07 22:26:50 +02:00
|
|
|
errors.append((delta_pixels, fn1, fn2))
|
2012-03-08 18:29:46 +01:00
|
|
|
return delta_pixels
|
2012-02-18 17:51:18 +01:00
|
|
|
im1 = im1.getdata()
|
|
|
|
im2 = im2.getdata()
|
|
|
|
for i in range(3, pixels - 1, 3):
|
|
|
|
if(compare_pixels(im1[i], im2[i])):
|
|
|
|
diff = diff + 1
|
|
|
|
if diff != 0:
|
2012-06-07 22:26:50 +02:00
|
|
|
errors.append((diff, fn1, fn2))
|
2012-04-04 23:54:07 +02:00
|
|
|
passed += 1
|
2012-02-18 17:51:18 +01:00
|
|
|
return diff
|
|
|
|
|
|
|
|
def summary():
|
|
|
|
global errors
|
2012-04-04 23:54:07 +02:00
|
|
|
global passed
|
2012-03-23 20:42:11 +01:00
|
|
|
print "-"*80
|
2012-04-04 23:54:07 +02:00
|
|
|
print "Visual text rendering summary:",
|
2012-02-18 17:51:18 +01:00
|
|
|
if len(errors) != 0:
|
|
|
|
for error in errors:
|
2012-06-07 22:26:50 +02:00
|
|
|
if (error[0] is None):
|
|
|
|
print "Could not verify %s: No reference image found!" % error[1]
|
2012-02-20 17:42:12 +01:00
|
|
|
else:
|
2012-06-07 22:26:50 +02:00
|
|
|
print "Failed: %d different pixels:\n\t%s (actual)\n\t%s (expected)" % error
|
2012-02-18 17:51:18 +01:00
|
|
|
sys.exit(1)
|
2012-03-23 20:42:11 +01:00
|
|
|
else:
|
2012-04-04 23:54:07 +02:00
|
|
|
print 'All %s tests passed: \x1b[1;32m✓ \x1b[0m' % passed
|
2012-03-23 20:42:11 +01:00
|
|
|
sys.exit(0)
|