Getting back the image from converted Numpy Array
Image.fromarray() function helps to get back the image from converted numpy array. We get back the pixels also same after converting back and forth. Hence, this is very much efficient
Python3
img = Image. open ( 'Sample.png' ) numpydata = asarray(img) print ( type (numpydata)) # shape print (numpydata.shape) # Below is the way of creating Pillow # image from our numpyarray pilImage = Image.fromarray(numpydata) print ( type (pilImage)) # Let us check image details print (pilImage.mode) print (pilImage.size) |
Output :
<class 'numpy.ndarray'> (200, 400, 3) <class 'PIL.Image.Image'> RGB (400, 200)
How to Convert images to NumPy array?
Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. In this article we will see How to Convert images to NumPy array?
Modules Needed:
- NumPy: By default in higher versions of Python like 3.x onwards, NumPy is available and if not available(in lower versions), one can install by using
pip install numpy
- Pillow: This has to be explicitly installed in later versions too. It is a preferred image manipulation tool. In Python 3, Pillow python library which is nothing but the upgradation of PIL only. It can be installed using
pip install Pillow
Contact Us