I have an image of a star field where I would like to count the total number of stars.

I have converted the image to black and white (1-bit indexed colours) and used the Select by colour tool (Shift+O) to select all the white spots. Is there a way to get the number of individual selections (not pixels)?

  • There are Astronomy tools that could do this for you. Some examples would be astrometry.net, startools.org, DeepSkyStacker. Otherwise, you could split the image into squares, count one of the squares to get an estimate. Not sure how GIMP could count several individual selections.
    – AndrewH
    Commented Oct 5, 2023 at 13:30
  • This question also might be relevant to you: Count the total number of selections
    – AndrewH
    Commented Oct 5, 2023 at 13:31

1 Answer 1


As is in the comments, there is astronomy software which probably could be more useful for you there -

It is possible to do this in GIMP, but you will need to type some expressions programatically. (However, opening the script console may as well give you new ideas and other ways to proceed with your work)

First, you need to convert your selection to a "Path": selections are internally raster (pixel) based - this means GIMP only can "see" the number of selected pixels, and don't know anything about their topology. A Path or Vector, however maps the selection boundaries to Bezier curves.

I never had a problem with this step, but it is possible that if you have large images with several million selected areas, it may fail. For hundreds of thousands you should be ok, I guess - but it will take sometime to execute.

So, it is Select->To Path option (the last one on the Select top level menu). It took me ~5 seconds for 5000 clusters on a dual core 2017 machine.

Then, open up the Python console: Filters->Python-Fu->Console At the Python >>> prompt, you first need to get a reference to your image. Type the following command at is, and pres <enter>: (These instructions are for GIMP 2.10 - they might be different fro GIMP 2.99/3.0)

img = gimp.image_list()[0]

(The [0] above will get a reference to the rightmost image in a tab in GIMP - if you have more than one image open, you may have to adjust it)

Now: print(len (img.vectors[0].strokes)) will give you the total number of path components.

(Again: the [0] refers to the bottom-most "Path" in the image - if you have created others, either with the "paths" tool, or converting from other selections, you have to pick the correct one)

If you are doing this for several images in a row, the three operations (selecting by color, converting the selection to a path and counting the path components), can be arranged in a single script that can be executed at a single key-stroke. Check other answers of mine here (or on "stackoverflow.com" with the tag [GIMP] ) to see how to create a complete script for GIMP 2.x

  • 1
    The ofn-path-edit script that you'll find here has a Summary function that gives the count of strokes of a given path.
    – xenoid
    Commented Oct 6, 2023 at 7:29
  • 1
    Thanks for such an instructive answer. It worked like a charm!
    – Mr. Tao
    Commented Oct 7, 2023 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.