This program gets the r, g, b, a values of a pixels two ways. One by converting into a 2d array of tuples and reading the values. The other uses the default PIL method. However they give me different reading of RGB values. I tried few tests to make sure they were reading the same pixel, but it still gives different values. (This happens when the picture has more than one color)
import PIL
import numpy
from PIL import Image
image = PIL.Image.open("image.png")
pixels = image.load()
a = numpy.asarray(image)
r, g, b, c = a[x][y]
print(str(r) "," str(g) "," str(b))
r1, g1, b1, c1 = pixels[x2,y2]
print(str(r1) "," str(g1) "," str(b1))
#print statements arent the same, they dont even add up to the same number.
CodePudding user response:
I tried on a couple of images and the two approaches returned exactly the same values. Keep in mind that a
is a transpose of pixels
. This means that you have to use a[i, j]
and pixels[j, i]
to target the same pixel!