[Image-SIG] Reversed X an Y when convering PIL images to NumPy arrays

Christopher Barker Chris.Barker at noaa.gov
Mon Apr 19 19:00:55 CEST 2010


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

Chris.Barker at noaa.gov


More information about the Image-SIG mailing list