Liam Kim Liam Kim - 2 years ago 218
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 =, 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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download