Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selected pixel count
#3
(03-10-2023, 08:04 AM)Ofnuts Wrote: Open the python console and enter this:

Code:
image=image=[x for x in gimp.image_list() if image.ID==5][0]
Where you replace "5" by the image ID, which is the number you find in the title bar:



Then to get your pixel count:

Code:
pdb.gimp_drawable_histogram(image.active_layer,0,0.,1.)[3]

If you want to redo this for another selection, just repeat that last line after changing the selection, since the image will be the same.

To be a bot more complete, in the histogram call, the args are:
  • The layer/drawable for which you want statistics
  • A channel identifier (Value (maxOf(R,G,B), Red, Green, Blue, Alpha, Luminance)
  • The minimum value you want inlcuded
  • The maximum value you want inlcuded (so you define a range between these two), like with the handles in the
  •  histogram display
The result is a list if numbers:
  • Average value
  • Standard deviation
  • Median value
  • Pixel count for the whole selection (range not taken in account)
  • Pixels count for the pixels within range
  • Ratio of the two previous values
So the code I gave you extracts the 4th value (index=3, since this is 0-based). The output of the API is in the [0..255] range instead of the [0. .. 1.0] range so you add a division by 255 if necessary.

It is of course also easy to format this output differently, and writing a script to write the values to file wouldn't be difficult.

Here is how the API and the GUI relate:
Thanks For That Ofnuts.
I've got it to work, but not with
Code:
image=image=[x for x in gimp.image_list() if image.ID==5][0]
making sure the ID matches the Gimp Image number - i keep getting the error :-

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'image' is not defined

 But I am only using one image, so I can just use
Code:
theImage = gimp.image_list()[0]
Also is there anyway I can get this number directly into the clipboard?
Cheers
Steven
Reply


Messages In This Thread
Selected pixel count - by steven Brazzale - 03-10-2023, 12:36 AM
RE: Selected pixel count - by Ofnuts - 03-10-2023, 08:04 AM
RE: Selected pixel count - by steven Brazzale - 03-12-2023, 12:13 AM
RE: Selected pixel count - by Ofnuts - 03-12-2023, 02:03 PM
RE: Selected pixel count - by steven Brazzale - 03-14-2023, 05:36 AM

Forum Jump: