Gimp-Forum.net
Can pdb.gimp_histogram replace this functionality? - 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)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Can pdb.gimp_histogram replace this functionality? (/Thread-Can-pdb-gimp-histogram-replace-this-functionality)



Can pdb.gimp_histogram replace this functionality? - trandoductin - 06-09-2018

I roughly translated a c plugin into a python script here:
http://www.gimplearn.net/viewtopic.php/Get-applied-RGB-Curves-Script-for-GIMP?f=3&p=17815#p17815

The main logic is it loops through all the pixel coordinates.
- at each pixel coordinate.
 - grab color value of red channel of bottom layer (r value) turns this into an index.
 - add color value of red channel of top layer to color[index] (r[index])
 - increment frequency (frequency[index], rf[index]) (like counting pixels)
- do this to green, and blue values as well.

That's it, it does this for all channels to create an RGB curve using the above calculated values.
I am wondering if pdb.gimp_histogram can be used so that script is faster than the current pixel by pixel calculation.

I tried thinking about histogram but I am not familiar with it and the location is involved as it calculates what color is actually in place of previous color so I am totally confused and not sure at all if histogram can some how perform this task.


RE: Can pdb.gimp_histogram replace this functionality? - Ofnuts - 06-09-2018

Yes, but forget the pixels:
  • Divide the 0-255 range (or 0.-1. range especially if you want this to work with high bit depth) into N ranges (N doesn't need to be much bigger than 5, because the Curves that won't radically alter an image are smooth and monotonously increasing).
  • Compute a value for the middle of each range.
  • For each of these values:
    • Use gimp_by_color_select_full(...) to select pixels on the source layer. You can use a threshold to include pixels with neighboring values. You have to use the "full" version to be able to select the channel on which the selection applies.
    • Use gimp_drawable_histogram(...) on the source layer to obtain the average for the channel (normally you use the same channel/criteria that you used in the selection (R, G, B, VALUE...)). The histogram applies only to the selected pixels, so you can always ask for the full range. This gives you the average value for all the pixels with a channel value close to your initial value.
    • Use gimp_drawable_histogram(...) again, on the target layer to obtain the average for the channel. This is the average value for the same pixels in the altered image.
  • The loop above gives you a list of (input,output) values from with you can approximate the curve (if not working on value, repeat on the R, G, B channels)  
Note that working on value only or on R,G,B is not the same, so you have to assume one or the other (make that a user choice).


RE: Can pdb.gimp_histogram replace this functionality? - trandoductin - 06-09-2018

Thank you ofnuts. Now I have something to work with.