Search code examples
pythonpython-imaging-libraryphotoshop

Why RGB value is different when getpixel in PIL and when use Eyedropper tool in Photoshop


enter image description here When I get RGB of (2835, 5089) in Photoshop, I get (112, 213, 240) But I did the same thing in PIL, I get (5, 186, 241) Why are they different?

I try to remove some words.

from PIL import Image

img = Image.open("SCN_0010.jpg")
for i in range(0, img.size[0]):
    for j in range(0, img.size[1]):
        r, g, b = img.getpixel((i, j))
        if r > 40:
            break
        if g < 150 or g > 200:
            break
        if b < 210:
            break
        img.putpixel((i, j), (0, 0, 0))
img.save("edited/SCN_0010.jpg")

But the RGB values were different so I can't do it.

from PIL import Image

img = Image.open("SCN_0010.jpg")
print(img.getpixel((2835, 5089)))

This is the code when I feel strange and do some test.


Solution

  • This is not a direct answer, more of a comment with images, but explains some of the problems encountered.

    Your source image is probably from a scan. Whilst initially it looks okay... but if we take a closer look there are no large areas of flat colour. Source image zoomed in

    However, the original image will be clean; something like this:

    clean image

    This is a Moiré pattern. You can learn more about them here and here

    So what's this got to do with anything??

    If we look at the clean image, the green is RGB 181, 205, 49. Whereas, if we look at an 8x8 pixel sample there are 58 shades of green there:

    8x8 image

    111,138,0 114,141,0 115,140,0 121,148,0 122,149,0 123,149,0 123,150,0 127,154,0 130,155,0 131,157,4 132,159,4 134,159,5 134,161,4 135,162,7 136,163,8 138,165,8 139,166,11 141,168,11 143,168,14 143,170,13 144,171,16 154,180,27 156,183,28 160,186,33 162,187,33 163,188,34 164,191,36 165,192,37 166,191,35 167,194,39 168,194,41 170,195,41 170,196,43 170,197,42 171,198,43 172,199,44 175,202,45 177,203,50 177,204,47 177,204,49 178,205,48 178,205,50 182,205,62 182,209,54 185,210,57 185,212,55 189,216,61 193,220,65 196,221,67 202,229,72 202,229,74 203,228,72 204,231,76 206,231,77 208,233,80 212,239,84 229,255,99 238,255,108

    In short: You might want to a test with images that use flat colour and then work out why the RGB values are different rather than having the added complications of further colour variations.