Conversion between Pillow Image object and numpy array changes dimension

21,500

Solution 1

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

Solution 2

If your image is greyscale do:

in_data = in_data.T

but if you are working with rbg images you want to make sure your transpose operation is along only two axis:

in_data = np.transpose(in_data, (1,0,2))
Share:
21,500
Jongsu Liam Kim
Author by

Jongsu Liam Kim

PhD in Computational Science and Engineering-Mechanical/Electrical Engineering from Yonsei University, South Korea.

Updated on December 17, 2020

Comments

  • Jongsu Liam Kim
    Jongsu Liam Kim over 3 years

    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?