0

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)

Test image 1 enter image description here

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
Beach Chicken
  • 368
  • 3
  • 13
  • 5
    Your red-circle image has antialiased edges. Zoom-in and you'll see how the image contains shades of red to ensure a smooth border: https://stackoverflow.com/questions/71306678/how-to-draw-circles-with-proper-anti-aliasing – Dai Jul 16 '23 at 09:49
  • If you try it with an image that uses https://en.wikipedia.org/wiki/Subpixel_rendering you'd get even more colours appearing! – Sam Mason Jul 18 '23 at 19:15
  • also note that JPEG will introduce https://en.wikipedia.org/wiki/Compression_artifact such that you will see even more colours appearing. PNG is a much better choice here – Sam Mason Jul 18 '23 at 19:21
  • 1
    `gimp` does have the ability to create shapes without anti-aliased edges, if that's important. – Tim Roberts Jul 18 '23 at 19:21

0 Answers0