amartin7211
amartin7211

Reputation: 499

pix[x,y] from PIL produces name error

I am a beginner programmer and I am trying to access pixel data from a picture using python. I want to eventually get the pixel data into an array. I searched the web for the code of how to do this and this is what I got:

from PIL import Image

im = Image.open("C:/Users/Owner/Desktop/bw.png")
pix = im.load()
print pix[x,y]
pix[x,y] = value

It seems to work fine until I get to the print[x,y] line. I get an error saying "NameError: name 'x' is not defined". I have downloaded PIL 1.1.7.

Can anyone lend me a helping hand?

Upvotes: 1

Views: 2403

Answers (2)

GSPdibbler
GSPdibbler

Reputation: 405

im = Image.open("C:/Users/Owner/Desktop/bw.png")
x, y = 1, 2    #sample coordinates
print im.getpixel((x, y))

that should work, note that to getpixel method you pass one argument - a tuple

Upvotes: 0

nneonneo
nneonneo

Reputation: 179392

Uh, you didn't define x, y, or value...maybe try defining those first? What pixel did you want to access?

Upvotes: 1

Related Questions