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?
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:
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:
| 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.
pdb.plug_in_sel2path(image, drawable)
. The result is a new path that is set active, so you can obtain it with path=image.active_vectors
path.strokes
(otherwise you have a non-contiguous selection).allX=path.strokes[0].points[0][2::6]
allY=path.strokes[0].points[0][3::6]
pdb.gimp_drawable_get_pixel()
, but this color may be quite different from the rest (in my test image above, the center pixel is (54, 62, 47), so somewhat lighter and redder than average).