1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| factor=2 #facteur de correction pour retrouver les corrections de photoshop
def make_bezier(xys):
# xys should be a sequence of 2-tuples (Bezier control points)
n = len(xys)
combinations = pascal_row(n-1)
def bezier(ts):
# This uses the generalized formula for bezier curves
# http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization
result = []
for t in ts:
tpowers = (t**i for i in range(n))
upowers = reversed([(1-t)**i for i in range(n)])
coefs = [c*a*b for c, a, b in zip(combinations, tpowers, upowers)]
result.append(
tuple(sum([coef*p for coef, p in zip(coefs, ps)]) for ps in zip(*xys)))
return result
return bezier
def pascal_row(n):
# This returns the nth row of Pascal's Triangle
result = [1]
x, numerator = 1, n
for denominator in range(1, n//2+1):
# print(numerator,denominator,x)
x *= numerator
x /= denominator
result.append(x)
numerator -= 1
if n&1 == 0:
# n is even
result.extend(reversed(result[:-1]))
else:
result.extend(reversed(result))
return result
def bezierRGB(r,g,b):
ts = [t/100.0 for t in range(101)]
dcouches={}
dcorr={'R':r,'G':g,'B':b}
for c in dcorr :
xys = [(0, 0), (128,128+(dcorr[c]*factor)), (255, 255)]
bezier = make_bezier(xys)
points = bezier(ts)
dcouches[c]=points
return [dcouches['R'],dcouches['G'],dcouches['B']]
def newPoint(pt,bezier):
#return (pt,bezier)
a=0
while pt > bezier[a][0] :
a+=1
if a>0 : npt = int(bezier[a][1])
else : npt = int(bezier[0][1])
return (pt,npt,bezier)
def colorAdjustement(im,r,g,b):
lbezierRGB=bezierRGB(r,g,b)
width, height = im.size
colortuples = im.getcolors(maxcolors=17000000)
pix = im.load()
u=max(len(lbezierRGB[0]),len(lbezierRGB[1]),len(lbezierRGB[2])) #longueur max des points de courbes
print u
for x in range(0, width):
for y in range(0, height):
ro,go,bo=list(pix[x,y])
color=(ro,go,bo)
if y==100 and x==100:
print newPoint(ro,lbezierRGB[0])
if r !=0 :
ro = newPoint(ro,lbezierRGB[0])[1]
if g !=0 :
go = newPoint(go,lbezierRGB[1])[1]
if b !=0 :
bo = newPoint(bo,lbezierRGB[2])[1]
im.putpixel((x, y), (ro,go,bo))
return im
if __name__ == '__main__':
import Image
im = Image.open("0002.JPG")
im = colorAdjustement(im,30,10,0)
im.save('0002_CorrectionBezier.JPG') |
Partager