Python Read Pil Image Into 2-d Numpy Array
Images are an easier way to represent the working model. In Machine Learning, Python uses the epitome data in the format of Height, Width, Aqueduct format. i.east. Images are converted into Numpy Array in Height, Width, Channel format.
Modules Needed:
- NumPy: By default in higher versions of Python similar 3.ten onwards, NumPy is available and if non available(in lower versions), one can install by using
pip install numpy
- Pillow: This has to be explicitly installed in later versions besides. 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
One can hands cheque the version of installed Pillow past using the below lawmaking
Python3
import PIL
print ( 'Installed Pillow Version:' , PIL.__version__)
Output:
Installed Pillow Version: vii.2.0
Loading the images via Pillow Library
Let us check for an image that is in the PNG or JPEG format. The image can exist referred via its path. Image class is the heart of PIL. It has open up() function which opens upwards an paradigm and digital file format tin can be retrieved besides equally pixel format.
Paradigm Used:
Python3
from PIL import Image
prototype = Image. open up ( 'Sample.png' )
print (image. format )
print (paradigm.size)
impress (image.mode)
Output :
PNG (400, 200) RGB
Converting an image into NumPy Array
Python provides many modules and API's for converting an image into a NumPy array. Let's talk over a few of them in particular.
Using NumPy module
Numpy module in itself provides various methods to exercise the aforementioned. These methods are –
Method 1: Using asarray() function
asarray() function is used to convert PIL images into NumPy arrays. This part converts the input to an array
Python3
from PIL import Image
from numpy import asarray
img = Image. open ( 'Sample.png' )
numpydata = asarray(img)
impress ( type (numpydata))
print (numpydata.shape)
Output :
<grade 'numpy.ndarray'> (200, 400, 3)
Method ii: Using numpy.assortment() part
Past using numpy.array() function which takes an prototype as the argument and converts to NumPy array
Python3
from PIL import Image
import numpy
img = Image. open up ( "Sample.png" )
np_img = numpy.array(img)
print (np_img.shape)
Output :
(200, 400, iii)
In club to get the value of each pixel of the NumPy array image, nosotros need to print the retrieved data that got either from asarray() function or array() part.
Python3
from PIL import Image
from numpy import asarray
img = Image. open ( 'Sample.png' )
numpydata = asarray(img)
print (numpydata)
Output :
[[[111 60 0] [116 65 0] [122 69 0] ... [ 97 47 0] [ 99 47 0] [100 49 0]] [[111 61 0] [118 65 0] [122 69 0] ... [ 97 47 0] [ 99 48 0] [100 49 0]] [[118 65 0] [122 69 0] [126 73 3] ... [ 98 48 0] [100 49 0] [100 49 0]] ... [[ 96 44 7] [ 95 43 half dozen] [ 93 41 4] ... [225 lxxx 3] [228 80 0] [229 78 0]] [[ 93 40 6] [ 90 37 5] [ 85 32 0] ... [226 81 4] [231 80 1] [232 79 1]] [[ 89 36 4] [ 84 31 0] [ 79 26 0] ... [228 81 4] [232 81 four] [233 80 ii]]]
Getting back the prototype from converted Numpy Array
Image.fromarray() function helps to get back the image from converted numpy array. Nosotros get back the pixels also same afterwards converting back and forth. Hence, this is very much efficient
Python3
from PIL import Image
from numpy import asarray
img = Prototype. open ( 'Sample.png' )
numpydata = asarray(img)
print ( type (numpydata))
print (numpydata.shape)
pilImage = Image.fromarray(numpydata)
print ( type (pilImage))
print (pilImage.fashion)
impress (pilImage.size)
Output :
<grade 'numpy.ndarray'> (200, 400, 3) <class 'PIL.Image.Image'> RGB (400, 200)
Converting Images using Keras API
Keras API provides the functions for loading, converting, and saving epitome data. Keras is possible to run on the top of the TensorFlow framework and hence that is mandatory to have. Deep learning computer vision images require Keras API. To install it type the below control in the final
pip install keras
Every bit Keras requires TensorFlow 2.ii or higher. If not there, demand to install it. To install it type the below command in the terminal.
pip install tensorflow
Python3
from keras.preprocessing.image import load_img
import warnings
img = load_img( 'sample.png' )
impress ( type (img))
impress (img. format )
print (img.mode)
print (img.size)
Output :
<course 'PIL.PngImagePlugin.PngImageFile'> PNG RGB (400, 200)
Using Keras API, catechumen images to Numpy Array and reverting the prototype from Numpy Array
Python3
from keras.preprocessing.image import load_img
import warnings
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
img = load_img( 'sample.png' )
print ( type (img))
impress (img. format )
impress (img.way)
impress (img.size)
img_numpy_array = img_to_array(img)
print ( "Image is converted and NumPy array information :" )
print ( type (img_numpy_array))
print ( "type:" , img_numpy_array.dtype)
print ( "shape:" , img_numpy_array.shape)
img_pil_from_numpy_array = array_to_img(img_numpy_array)
print ( "converting NumPy array into prototype:" ,
blazon (img_pil_from_numpy_array))
Output :
<course 'PIL.PngImagePlugin.PngImageFile'> PNG RGB (400, 200) Image is converted and NumPy array information : <grade 'numpy.ndarray'> type: float32 shape: (200, 400, 3) converting NumPy array into image: <class 'PIL.Image.Image'>
From the to a higher place output, we can check that the source image PIL.Paradigm.Epitome and destination image types are the aforementioned.
Using OpenCV Library
OpenCV version from 3.x has DNN and Caffe frameworks, and they are very helpful to solve deep learning problems. It can be installed by using
pip install opencv-contrib-python
cv2 package has the post-obit methods
- imread() function is used to load the paradigm and It also reads the given image (PIL paradigm) in the NumPy assortment format.
- Then nosotros need to catechumen the image colour from BGR to RGB.
- imwrite() is used to relieve the image in the file.
Python3
import cv2
epitome = cv2.imread( 'Sample.png' )
img = cv2.cvtColor(prototype, cv2.COLOR_BGR2RGB)
cv2.imwrite( 'opncv_sample.png' , img)
print ( blazon (img))
Output :
<form 'numpy.ndarray'>
Conclusion
Python is a very flexible tool and we have seen ways of converting images into Numpy Array and similarly back to images using different APIs. Manipulating the converted array and forming different image data and one can feed into deep learning neural networks.
Source: https://www.geeksforgeeks.org/how-to-convert-images-to-numpy-array/
0 Response to "Python Read Pil Image Into 2-d Numpy Array"
Postar um comentário