Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion between Pillow Image object and numpy array changes dimension

Tags:

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?

like image 624
Jongsu Liam Kim Avatar asked Sep 25 '13 22:09

Jongsu Liam Kim


People also ask

Can an image could be converted into a NumPy array?

Using OpenCV Library imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.

Does OpenCV work with NumPy array?

OpenCV is the most popular computer vision library and has a wide range of features. It doesn't have its own internal storage format for images, instead, it uses NumPy arrays. The common scenario for using this library is when you need to convert an image from Pillow to NumPy so that you can work with it using OpenCV.

Does pillow use NumPy?

In this chapter, we use numpy to store and manipulate image data using python imaging library – “pillow”. Note − This works only if you have PIP installed and updated.


1 Answers

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

like image 152
prgao Avatar answered Sep 20 '22 15:09

prgao