I make cross stitch designs, lately been using Affinity Photo/Designer. So I can figure out how much of each colour thread I need, I want to count the number of each pixels of each colour (in the flattened end picture) but I can't find any way, in Affinity or anything else I've tried, but I'm happy to try anything which doesn't cost money (or which I might already have, so just don't suggest anything made by Adobe). Can anyone help me out?
You could do it with GIMP which is free, open source, and works on Mac, Windows, and Linux.
Open an image in GIMP, using File > Open
In the main menu click Windows > Dockable Dialogs > Histogram
Choose the Select by Color tool Shift+C, and set the Threshold to 0, in the Tool Options, under the Toolbox
- Click on a colour to select it, and the number of pixels will show in the Histogram. Repeat for other colours
Note: If your image is very small, you might want to zoom in. Use + and - to zoom in/out. Holding down Space while you move the mouse allows panning around the image. Also note that the grey and white checkerboard in the above example is to show the transparent background. If your image has a solid background you won't see these.
Drawing heavily on Billy Kerr's answer using Gimp, this is how you can achieve the same in Affinity Photo:
First ensure you have the histogram panel active and visible using the View > Studio > Histogram menu option. (You might need to set the Advanced option for the panel, using the small burger menu top right, in order to view the statistics associated with the histogram.) Ensure that the "Marquee" option is selected, to limit the scope to selected pixels.
Then use the Select > Select Sampled Colour... option. A small panel will be shown:
Click in your image to choose the colour to be sampled. There's a Threshold slider to determine how closely each pixel must match to be selected. Adjust the slider to include/exclude the pixels you need. Then click Apply. The histogram and statistics will be updated to reflect the selection:
In my example you can see 136 pixels were selected. If you see a small warning triangle top right of the histogram (as in my first example), click it to force a full refresh of the data.
Repeat for the other colours. It's slightly tiresome because you will need to use the Select > Select Sampled Colour... menu option each time.
If you have the command-line tool imagemagick installed, you can use the following command in *nix or OSX terminal:
convert <filename> txt:- | cut -d " " -f 4 | uniq -c
<filename> with the name of your file. This will print a list of counts and hex colors, like:
10 #4A413F 14 #483E3C
This works by using the
convert tool to convert the file to "txt" format, which is made up of lines like:
9,0: (75,74,76) #4B4A4C srgb(75,74,76)
Which is the x, y coords, then the decimal RGB values, the hex value, and then the srgb value of the pixel. Obviously, this format is not very efficient :). We write this to standard out (
txt:- means "write txt format to stdout").
Then, we use the built-in
cut command to grab the fourth (
-f 4) column, delimited on spaces (
-d " "), which here is the hex value (interestingly, there are two spaces between the decimal RGB and the hex value, so if you want the decimal RGB, use
Finally, we use the built-in
uniq command to count
-c the unique lines, and print it to standard out (the terminal).
I couldn't find a way to do this in Photoshop, but there is a workaround to do it with Illustrator:
- Copy-paste your image in Illustrator*;
- Use Object > Create Object Mosaic** with the image's exact resolution to convert it into separate squares;
- Use the Selection Tool [V] and double-click on the object to enter the resulting group;
- Select the Magic Wand Tool [Y] and double-click the tool's icon to access its settings;
- Tick 'Fill Colour', set its Tolerance to 1 and untick the other options;
- Open Window > Document Info and open its options to tick 'Objects'
- Click on a coloured 'pixel' with the Magic Wand to select all of them and have the total amount displayed at the top of the Document Info palette.
* I tried placing a .png with transparency, and Ai refused to Mosaic it, hence my advice to just copy-paste.
** This Question has an overview of this technique.