1. Home
  2. Docs
  3. Computer Vision
  4. Introduction to OpenCV
  5. Opening Image Files in a ...
  6. Images and Numpy

Images and Numpy

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

Loading

Views: 5

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments