gimppython-fu

GIMP - Get a point within selection from python script


My script runs on a fuzzy selected area. I need to get the color value of the selected area from within python-fu script. This could be the average value of the selection area or, better, the color value of the pixel that user clicked on to make the fuzzy selection.

I can get the selection bounds and then somehow get a point within the selection to make use of pdb.gimp_image_pick_color. But I don't know how to get the point (which best represents the color of the selection) within any arbitrary shaped selection.

Could you please help?


Solution

  • A point inside the selection could be tricky, because you don't know if the selection is a single contiguous area.

    Two ways to do it:

    Using the histogram over the selection

    You can use pdb.gimp_drawable_histogram to get histogram values for each of the RGB channels in the selected pixels

    mean, std_dev, median, pixels, count, percentile=pdb.gimp_drawable_histogram(layer,HISTOGRAM_RED,0.,1.)
    

    Using the call three times (once for each channel) in this image selection:

    enter image description here

            | Average | Median |  Picker
    --------+---------+--------+---------     
      Red   |   44.0  |  44    |    47
    Green   |   54.2  |  54    |    56
     Blue   |   43.6  |  43    |    46
    

    The values obtained for the same area with the color picker in average mode aren't so far off.

    Using the color of a pixel

    allX=path.strokes[0].points[0][2::6]
    allY=path.strokes[0].points[0][3::6]