Re: Reversed X an Y when convering PIL images to NumPy arrays

Christopher Barker <[email protected]>
Newsgroups gmane.comp.python.image
Message-ID <[email protected]>
Yury V. Zaytsev wrote:
> Is it normal when I execute the following code I get the red dot at X =
> 200, Y = 100 on the plot?!

yes, I think it is.

> I would have expected to be able to address
> pixels in the resulting array in a usual p[X, Y] format.

IIRC, the standard data storage for PIL iamges results in a (h, w) 
array, rather than a (w, h) one -- using X,Y, rather than Y,X is just a 
convention.

In numpy -- the convention is to think of and display the indexing as 
(row, column), hence (y, x), and MPL displays it that way.

you can just use (y,x) , or you can transpose the array:

 >     p = a.T.copy()

Remember to transpose back if you want to make a PIL image again.

-Chris



> I might be blind, but I haven't found anything in the documentation.
> Actually I have found out about the possibility to interface with Numpy
> itself at some blog: http://effbot.org/zone/pil-changes-116.htm .
> 
> import Image
> import numpy as np
> import pylab as pyl
> 
> if __name__ == "__main__":
> 
>     img_name = 'ch43_roi.tiff'
>     img = Image.open(img_name)
> 
>     a = np.asarray(img)
> 
>     p = a.copy()
>     p[100, 200] = (255, 0, 0)
> 
>     pyl.imshow(p)
>     pyl.show()
> 
>  


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[email protected]
_______________________________________________
Image-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/image-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.