Liam Kim Liam Kim - 6 months ago 46
Python Question

Conversion between Pillow Image object and numpy array changes dimension

I am using Pillow and numpy, but have a problem with conversion between Pillow Image object and numpy array.

when I execute following code, the result is weird.

im = Image.open(os.path.join(self.img_path, ifname))
print im.size
in_data = np.asarray(im, dtype=np.uint8)
print in_data.shape


result is

(1024, 768)
(768, 1024)


Why dimension is changed?

Answer

im maybe column-major while arrays in numpy are row-major

do in_data = in_data.T to transpose the python array

probably should check in_data with matplotlib's imshow to make sure the picture looks right.

But do you know that matplotlib comes with its own loading functions that gives you numpy arrays directly? See: http://matplotlib.org/users/image_tutorial.html

Comments