Table of Contents
show
Conversion from PIL to numpy
Using array constructor
import numpy as np
array = np.array(image)
print(array)
These arrays are similar to OpenCv arrays
Images and Numpy
Numpy itself cannot directly open .jpg or .png extension file
This must be used in conjunction with pillow or pil library
PIL – Python Imaging Library
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline #not needed for latest version
- matplotlib – allows to plot out and display images inside of the notbook
- matplotlib inline – to display things within the notebook
To open an image in Numpy
Image – function to open up the images and then operations like transform image into an array that numpy can understand
from PIL import Image
open( ) – specify the path for the image file to open
Pic = Image.open(“parrot.jpg”)
To convert picture into an array
pic_arr = np.asarray(pic)
To find the type of the converted array
type(pic_arr)

To find the shape of the converted array
pic_arr.shape

Views: 5