Log in

View Full Version : tone curves


age
22nd October 2015, 11:00
A simply smooth curves adjustement script.
15081

put curves.py in Python/Lib/site-packages.

Usage example:

import vapoursynth as vs
import curves
core = vs.get_core()
clip = yourclip
clip=curves.ycurve(clip,[(0,0),(16191,13364),(32768,32768),(49087,51400),(65535,65535)])

age
26th October 2015, 13:16
Another version of curve,probably better, with spline interpolation(modification of original code in finesharp.py).
Added planes to be processed and changed the way to insert control points (as a string).


import vapoursynth as vs


def spline(x, coordinates):
def get_matrix(px, py, l):
matrix = []
matrix.append([(i == 0) * 1.0 for i in range(l + 1)])
for i in range(1, l - 1):
p = [0 for t in range(l + 1)]
p[i - 1] = px[i] - px[i - 1]
p[i] = 2 * (px[i + 1] - px[i - 1])
p[i + 1] = px[i + 1] - px[i]
p[l] = 6 * (((py[i + 1] - py[i]) / p[i + 1]) - (py[i] - py[i - 1]) / p[i - 1])
matrix.append(p)
matrix.append([(i == l - 1) * 1.0 for i in range(l + 1)])
return matrix

def equation(matrix, dim):
for i in range(dim):
num = matrix[i][i]
for j in range(dim + 1):
matrix[i][j] /= num
for j in range(dim):
if i != j:
a = matrix[j][i]
for k in range(i, dim + 1):
matrix[j][k] -= a * matrix[i][k]




if coordinates=="" :
s=x
return s
else :
coordinates=coordinates.replace('-',',').split(",")
coordinates = [int(i) for i in coordinates]
length = (len(coordinates))//2

i=0
px =[]
py =[]
while i < ((len(coordinates)-1)) :
px.append(coordinates[i])
py.append(coordinates[i+1])
i=i+2

if (length == 1) :
s=x
return s




elif (length == 2) :

i=0
x1=px[i]
y1=py[i]
x2=px[i+1]
y2=py[i+1]
s= ((x-x1)*(y2-y1)/(x2-x1))+y1
return s


else :
matrix = get_matrix(px, py, length)
equation(matrix, length)
for i in range(length + 1):
if x >= px[i] and x <= px[i + 1]:
break
j = i + 1
h = px[j] - px[i]
s = matrix[j][length] * (x - px[i]) ** 3
s -= matrix[i][length] * (x - px[j]) ** 3
s /= 6 * h
s += (py[j] / h - h * matrix[j][length] / 6) * (x - px[i])
s -= (py[i] / h - h * matrix[i][length] / 6) * (x - px[j])
return s





def curve(c, coordinates="",planes=[] ) :
core = vs.get_core()
lutcurve = []
h=2**c.format.bits_per_sample
for x in range(h):
x=spline(x,coordinates)
x=int(round(x))
if (x <= 0) :
x=0
elif (x >= h) :
x=h-1
else :
x=x
lutcurve.append(x)
if (len(planes)>0):
c= core.std.Lut(clip=c,planes=planes, lut=lutcurve)
else:
c= core.std.Lut(clip=c, lut=lutcurve)
return c


# save this file as curve.py
# usage:
# import curve
# clip = curve.curve(clip, "0-0,128-128,255-255", planes=[0,1,2])