dameng
dameng

Reputation: 548

the data from PIL's getpixel & getdata are not the same?

Guys. I use getpixel & getdata to retrieve data from the same pic.but some points are different. why?

im = Image.open("cljr.jpg") 
t=list(im.getdata())
for i in range(10):
    print "%d %x %x %x"%(i,t[i][0],t[i][1],t[i][2])

print '' 

print "%x %x %x"% im.getpixel((0,7))
print "%x %x %x"% im.getpixel((0,8))

and here is the output:

0 ec f7 f9
1 ec f7 f9
2 ec f7 f9
3 ec f7 f9
4 ec f7 f9
5 ec f7 f9
6 ec f7 f9
7 ec f7 f9
8 eb f6 f8
9 eb f6 f8

ec f7 f9
ed f8 fa

Upvotes: 2

Views: 4689

Answers (2)

Cameron Hayne
Cameron Hayne

Reputation: 481

The confusion arises from the fact that the arg to getpixel is the coordinate (x, y) and

  • x = column index
  • y = row index.

I.e. you should do getpixel(col, row)

Upvotes: 1

wim
wim

Reputation: 363233

From this, you can see that im.getdata is ordered in column-major and im.getpixel will be row-major.

>>> import Image
>>> import numpy as np
>>> x = np.array([[1., 2.], [3., 4.]])
>>> im = Image.fromarray(x)
>>> list(im.getdata())
[1.0, 2.0, 3.0, 4.0]
>>> [im.getpixel(x) for x in [(0,0), (0,1), (1,0), (1,1)]]
[1.0, 3.0, 2.0, 4.0]

Upvotes: 2

Related Questions