# checkfitlighting.py # compute RMS deviation from average intensity of all the white squares # to see how well the lighting correction worked import numpy, Image from scipy import * from scipy.linalg import * def checkfitlight(im): w, h = im.size if w*10 == h*14: rows = 10; cols = 14; s = w/14 else: assert False, 'unexpected aspect ratio' whites = [1, 4, 7, 10, 40, 70, 100, 140, 137, 134, 131, 91, 61, 31, 45] greys = [3, 6, 9, 11, 20, 41, 50, 71, 75, 80, 101, 110, 121, 130, 132, 135, 138] blacks = [2, 5, 8, 21, 30, 46, 60, 81, 90, 111, 120, 133, 136, 139] greys2 = [46, 95, 56, 85, 66, 107, 75, 76, 65, 106, 86, 55, 96, 45] squareset = [whites, greys] squaresetnames = ["white", "grey"] stds = [] for squares, sqname in zip(squareset, squaresetnames): n = len(squares) rgb = zeros([n, 3]) for k, sq in enumerate(squares): row = (sq-1) % rows col = (sq-1) / rows y = row - (rows - 1)/2.0 x = col - (cols - 1)/2.0 box = (col*s, row*s, col*s+s, row*s+s) imc = im.crop(box) band = imc.split() for b, c in zip([0, 1, 2], ['R', 'G', 'B']): a = numpy.asarray(band[b]) avg = mean(a) rgb[k, b] = avg #print "rgb=", rgb #print "mean=", rgb.mean(0) #print "std= ", rgb.std(0) rgbmean = rgb.mean(0) rgb = rgb / rgbmean # normalized intensities #print "mean=", rgb.mean(0) st = rgb.std(0) #print "std= ", st mst = mean(st) #print sqname, "std =", mst, stds.append(mst) return stds