News: 11 March 2016 - Forum Rules, Mobile Version
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia

Author Topic: Count each color in an image?  (Read 397 times)

NERV Agent

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
Count each color in an image?
« on: November 07, 2017, 08:38:52 pm »
I have an image and want to know how many times a color is used (if at all).

For example, I want to know how many pixels use the color (69 69 69) in an image, and if the program can give me the number of pixels that use that color. I'm not looking for a histogram or bar chart; I need the actual number of pixels for "reasons".

Can this be done?

Oh, I forgot. I need to search for the value by entering the actual RGB value. I know that GIMP can do this by checking the color index, but if I have 256 or 16 million colors, there is no way in Hell I'm gonna go through all that to see which index number is (69 69 69).
« Last Edit: November 07, 2017, 08:50:34 pm by NERV Agent »

Jorpho

  • Hero Member
  • *****
  • Posts: 3643
  • The cat screams with the voice of a man.
    • View Profile
Re: Count each color in an image?
« Reply #1 on: November 07, 2017, 10:37:57 pm »
Oh, I forgot. I need to search for the value by entering the actual RGB value. I know that GIMP can do this by checking the color index, but if I have 256 or 16 million colors, there is no way in Hell I'm gonna go through all that to see which index number is (69 69 69).
Sounds a bit strange - if you have 256 colors, I can't imagine you'd have that many shades of grey to pick from, and if you're using 16 million colors, I would expect you'd have lots of near misses like (70 69 68) and so on.

But whatever.  I think you ought to be able to quickly knock something together using the Python Imaging Library that will check each individual pixel for you.
This depresses me. I feel like a goldfish right now...

NERV Agent

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
Re: Count each color in an image?
« Reply #2 on: November 08, 2017, 09:45:54 pm »
Unfortunately, the most programming experience I have is from my Computer Science I C++ course during my freshman year of kollej, and that was many years ago. Hence, why I am prone to posting in the Newcomer's Board due to not having enough programming experience.

If no existing program exists that can perform this task, can someone walk me through compiling a Python executable that can do this?

Jorpho

  • Hero Member
  • *****
  • Posts: 3643
  • The cat screams with the voice of a man.
    • View Profile
Re: Count each color in an image?
« Reply #3 on: November 09, 2017, 01:02:00 am »
Python is blissfully simple.  You certainly don't need to make an executable.  In fact, you can do everything straight from the interpreter if you like, just like old-timey BASIC.  Your lack of experience is not much of a barrier.

I did a Google search for "analyze image pixel by pixel in python pillow" (no quotes) and was delivered to this StackOverflow page.
https://stackoverflow.com/questions/138250/how-can-i-read-the-rgb-value-of-a-given-pixel-in-python

So, it looks like all you need is:
Code: [Select]
from PIL import Image
im = Image.open('image.jpg', 'r')
pixel_values = list(im.getdata())
pixnum = 0
for p in pixel_values:
   if p == (69,69,69):
      pixnum = pixnum + 1
print pixnum

Literally, that's the code. I gave it a spin just now.

Would you like instructions on how to install Python and Pillow (which is apparently what they're calling the image library these days) ?
This depresses me. I feel like a goldfish right now...

NERV Agent

  • Sr. Member
  • ****
  • Posts: 323
    • View Profile
Re: Count each color in an image?
« Reply #4 on: November 09, 2017, 11:44:03 pm »
I got Python 2.7.11 Shell installed at the moment.

With that code, do I put the image file in a specific directory for Python to read it, or do I put the directory path somewhere in that code?

Jorpho

  • Hero Member
  • *****
  • Posts: 3643
  • The cat screams with the voice of a man.
    • View Profile
Re: Count each color in an image?
« Reply #5 on: November 10, 2017, 12:16:08 am »
To keep things simple, you can just put the image in the same directory as python.exe. Otherwise, you can just replace 'image.jpg' in the second line with the full path to your image file.  The only catch is that you need to use two backslashes wherever you would normally have just one - as in 'C:\\Users\\Me\\Desktop\\image.jpg'.

To install Pillow, just open a Windows command prompt, navigate to C:\Python27\Scripts (or wherever you installed Python) and type "easy_install Pillow".  (If the first line of the code, "from PIL import Image", gives you an error message, then you need to install Pillow.)
« Last Edit: November 10, 2017, 10:34:31 pm by Jorpho »
This depresses me. I feel like a goldfish right now...

weissvulf

  • Full Member
  • ***
  • Posts: 185
    • View Profile
Re: Count each color in an image?
« Reply #6 on: November 10, 2017, 04:03:46 pm »
Sounds like a good way to send secret messages! Say... are you a spy!!!? :D

I would add that it depends on what format image you are scanning. Most modern images are encoded as '1byte per color channel" (aka 24bit) in a RGB order, but if you're scanning a 4, 8 or 16 bit image, the pixel-data structure changes drastically.

I don't know python, but the posted code appears to only be for 24bit color inages, and the 'Image.open()' function may include some color upscaling function that distorts pixel-value counts. Also, remember that JPG is a lossy format; if you re-save the image the pixel color count will probably jump wildly.

Jorpho

  • Hero Member
  • *****
  • Posts: 3643
  • The cat screams with the voice of a man.
    • View Profile
Re: Count each color in an image?
« Reply #7 on: November 10, 2017, 10:46:23 pm »
I don't know python, but the posted code appears to only be for 24bit color inages
I tried a .gif just now, and it appeared to return a palette index from 0 to 255 for each pixel.

Quote
and the 'Image.open()' function may include some color upscaling function that distorts pixel-value counts.
That would be a potential hazard for almost any image analysis program, surely?
This depresses me. I feel like a goldfish right now...

weissvulf

  • Full Member
  • ***
  • Posts: 185
    • View Profile
Re: Count each color in an image?
« Reply #8 on: November 11, 2017, 04:38:39 am »
I would expect processes designed for generic image access would not stretch/upscale or they wouldn't be very suitable for photo editing etc. I was just thinking that a routine designed specifically for displaying images might cause issues.

If it accurately returns results for an 8bpp GIF and a 24bit JPG, then I'd say it's exactly what you were wanting.

Jorpho

  • Hero Member
  • *****
  • Posts: 3643
  • The cat screams with the voice of a man.
    • View Profile
Re: Count each color in an image?
« Reply #9 on: November 11, 2017, 02:31:06 pm »
Of course, in the case of a .gif, there's no reason the same color can't be used multiple times in the palette, in which case you'd need to double-check whether the color you're looking for is unique.  There's probably a way to do that with Pillow.
This depresses me. I feel like a goldfish right now...