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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With