# Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.20 KB  |  hits: 12  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. from scipy.misc import imread, imsave
2. from scipy import mean, interp, ravel, array
3. from itertools import izip
4. import sys
5.
6. def mkcurve(chan1,chan2):
7.     "Calculate channel curve by averaging target values."
8.     fst = lambda p: p[0]
9.     snd = lambda p: p[1]
10.     sums = {}
11.     for v1, v2 in izip(ravel(chan1), ravel(chan2)):
12.         old = sums.get(v1, [])
13.         sums.update({v1: old + [v2]})
14.     c = array( [ (src,mean(vals))
15.                  for src,vals in sorted(sums.iteritems()) ])
16.     nvals = interp(range(256), c[:,0], c[:,1], 0, 255)
17.     return dict(zip(range(256), nvals))
18.
20.     "Match colors of the bad image to good image."
21.     r, g, b = bad.transpose((2,0,1))
22.     r2, g2, b2 = good.transpose((2,0,1))
23.     rc = mkcurve(r,r2)
24.     gc = mkcurve(g,g2)
25.     bc = mkcurve(b,b2)
27.     h, w = corr.shape[:2]
28.     for row in xrange(h):
29.         for col in xrange(w):
30.             r,g,b = corr[row,col]
31.             corr[row,col] = [rc[r], gc[g], bc[b]]
32.     return corr
33.
34. if __name__ == "__main__":
35.     good, bad, saveas = sys.argv[1:1+3]