Different Ways to Load Images in Python from Scratch

In the Computer Vision, we do the work on images and videos and the starting of any project that involves computer vision, we first read/ load images. And we of-course know that the videos are sequence of images, so we learn how read images and manipulate them.

In this post I will describe four Python Libraries that can load images. These are as follows:
  • OpenCV: cv2.imread()
  • Matplotlib: plt.imread() and image.imread()
  • scikit-image: io.imread()
  • Pillow: Image.open()
Original Image



OpenCV:

OpenCV (Open Source Computer Vision) is an open source computer vision and machine learning library. To work with computer vision problems OpenCV is very helpful. We can perform lots of tasks using OpenCV like Recognition, Motion Analysis, Scene Reconstruction, image restoration and many more.

Install OpenCV usingpip install opencv-python

Read/ Load the image:cv2.imread() function is used to read the image. The following Python code reads the image.
 
import cv2
img = cv2.imread('Tulips.jpg')


Note: If the image is not in the same file where you are running python code the specify the path of the image accordingly. See the below code.
import cv2
img_path = r'C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg'
img = cv2.imread(image_path)


Display The Image:
cv2.imshow() is used to display the image. See the below code.

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destrouAllWindows() 

Note: Don't forget to import cv2 and to write cv.wait(0) and cv2.destroyAllWindows()

Another way and most common to display the image is using pl.imshow().

from matplotlib.pyplot as plt
plt.imshow(img)

Output:

output using plt.imshow()

Note  the difference between original image and above image. 

This is because openCV reads image as BGR image. So to convert from BGR to RGB use below code.

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


You may be interested in below article.

How to Load and Visualize Multiple Images in Python 


Matplotlib:

Matplotlib is a comprehensive library for creating static, animated and interactive visualization in Python. plt.imread() is used to read the image and plt.imshow() tom display the image.

Install Matplotlib using  pip install matplotlib

The below code will read and display the image:

from matplotlib.pyplot as plt
img= plt.imread('Tulips.jpg')
plt.imshow(img)

Output:

We can use image.imread() instead of plt.imread(). See the below code:

from matplotlib import image
import matplotlib.pyplot as plt
img= image.imread('Tulips.jpg')
plt.imshow(img)


scikit-image:

scikit-image is an open source image processing library for Python Programming Language.

Install scikit-image using  pip install scikit-image  

io.imread() is used to read the image. See the below Python code:

from skiimage import io
import matplotlib.pyplot as plt
img= io.imread('Tulips.jpg')
plt.imshow(img)

Output: Output is same as the output in Matplotlib

Pillow:

PIL(Pyhton Imaging Library), in newer version known as Pillow, is a free open-source library for Python Programming Language used in image processing.


Install Pillow using pip install Pillow==2.2.2 

Image.open() is used to open/ read the image. See the below code.

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('Tulips.jpg')
plt.imshow(img)

Output: Again the output is same as in the Matplotlib and scikit-image.

Further Readings:

Comments