![]() |
|
A simple function to use non-linear curves in Python - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +--- Thread: A simple function to use non-linear curves in Python (/Thread-A-simple-function-to-use-non-linear-curves-in-Python) |
A simple function to use non-linear curves in Python - Scallact - 10-27-2025 Hello, I hit a wall when trying to convert one of my old v2python scripts making use of curves. The problem is that the curves_spline(...) API function works in linear space, with no option to use curves in non-linear space. Designing curves in linear space is a nightmare, so don't even start me on this ;-) Simply converting the curves coordinates to linear don't work, as x values are compressed to the left, and the result is very, very ugly. The right way to do it would be to sample many points along the curve, by applying the Catmull-Rom splines algorithm, but these splines require 4 points to work, and I have no idea how GIMP calculates the first and last segments. It seems I'm not the only one on the internet with this problem since 3.0, and all questions of this kind are generally left unanswered, so I wanted to share a mildly clever little hack I found here. The idea is to transform the coordinates values to linear, apply the non-linear spline curve, and transform back the values to the original non-linear space.If you find this back-and-forth confusing, so do I, but after much brain storming and many tests I can say that it works really well... except for 8 bits integer images which really don't like 3 consecutive operations in linear space!!! (please keep that in mind)! Here is my code. Reuse it as you wish! Code: def srgb_curves_spline(drawable, channel, spline) :Notes: The default "srgb" color space is used for the non-linear space. If you really need curves in other spaces, change the transfer functions accordingly. You can tweak the "samplecount" parameter for performance (down to 256). The value chosen (1024) ensures good quality conversion of dark values, but 256 is perfectly acceptable. |