I'm fooling around with Python and processing image files. I wrote a very basic python (3.10) application that loads a image (see attachments) using Pillow library. Reads all the pixel values one by one and counting all the unique RGB values.
I am a bit surprised by the outcome when processing red circle image. It shows there are 212 unique RGB values in this picture. But I would expected about 2 unique RGB values because the image only has two colors (white and red).
When i process the second image (black square) I just get two RGB values.
Can somebody explain to me the large variety of RGB values in a white-red image? and why does the white-black image just shows the expected number of unique RGB values?
Mapping pixel values code (very quick and dirty code)
pixelMap = defaultdict(list)
for xCoordinate in range(0, self.image.size[0]):
for yCoordinate in range(0, self.image.size[1]):
# Get a pixel at coordinate
pixel = self.getpixelatcoordinate(xCoordinate, yCoordinate)
# Create a key based on the RGB code, format: <R>-<G>-<B>
index = "-".join(str(colorcode) for colorcode in pixel)
pixelMap[index].append(tuple([xCoordinate, xCoordinate]))
print(len(pixelMap.keys()))
I created the test images myself using Gimp or Pinta (tried both, outcomes is the same).
Steps used to create included images:
- Open up pinta or gimp (tried both)
- Create 200x200px canvas with white background
- Draw a shape and fill the inner shape up with a color. (Paint bucket tool)
- Export image as JPG (also tried PNG) with highest quality (also tried lowest quality)