1

I am attempting to use the Pillow library in Python 2.7 to extract pixel values at given coordinates on PNG and JPG images. I don't get an errors but I always get the same value irrespective of the coordinates I have used on an image where the values do vary.

This is an extract from my script where I print all the values (its a small test image):

from PIL import Image

box = Image.open("col_grad.jpg")

pixels = list(box.getdata())

print(pixels)

And from when I try to extract a single value:

from PIL import Image, ImageFilter

box = Image.open("col_grad.jpg")

value = box.load()

print(value[10,10])

I have been using these previous questions on this topic for guidance including:

Getting list of pixel values from PIL

How can I read the RGB value of a given pixel in Python?

Thanks for any help with this,

Alex

Community
  • 1
  • 1
Alexander Peace
  • 113
  • 2
  • 14

2 Answers2

3

I'm not sure if you can access it the way you want, because of the complexity of images data.

Just get the pixel:

Image.getpixel(xy)

Returns the pixel value at a given position.
Parameters:   xy – The coordinate, given as (x, y).
Returns:  The pixel value. If the image is a multi-layer image, this method returns a tuple.:
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
1

You should consider doing one of the following:

FIRST convert your image to grayscale and then find the pixel values present.

img_grey= img.convert('1')     # convert image to black and white

SECOND If you want the pixel values of RGB channels, you have to split your color image. Then find the pixel values in all the channels at a particular coordinate.

Image.split(img) 
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87