Loading and Displaying Images in Google Colab: A Guide with OpenCV, PIL, and Matplotlib

Ashay Tanaji Ingale
4 min readJul 24, 2023

Images are an essential component of various applications, from computer vision and machine learning to digital art and content creation. If you’re working in Google Colab, a cloud-based Python environment, you can easily handle images using popular libraries like OpenCV and PIL (Python Imaging Library) and visualize them using Matplotlib. In this article, we’ll walk you through the process of loading, reading, and displaying images in Google Colab.

Setting up Google Colab Environment

Before we proceed, let’s make sure we have the necessary libraries installed. Google Colab comes pre-installed with most of the common libraries, but we’ll still check and install the required ones:

!pip install opencv-python
!pip install pillow
!pip install matplotlib

Loading and Reading Images with OpenCV and PIL

1. Uploading Images to Google Colab

Before we begin loading and reading images, we need to upload the image files to our Google Colab environment. To upload images, follow these steps:

  1. Click the “Files” icon on the left sidebar.

2. Click the “Upload” button and select the image files you want to use.

Upload button
Select Image to upload from Files
Copy the Path for further references

Loading Images with OpenCV:

Now that we have our images uploaded,

a. For Simplying Loading the Image, the Basic command using OpenCV is:

import cv2
from google.colab.patches import cv2_imshow
# Open the image.
img = cv2.imread("/content/Lata.jpg")
cv2_imshow(img)

b. Loading and reading them using OpenCV:

import cv2
import numpy as np
import matplotlib.pyplot as plt

from google.colab.patches import cv2_imshow

# Make sure the path to the image is correct.
# If the image "Lata.jpg" is in the "/content/" directory, you can simply use:
img = cv2.imread("/content/Lata.jpg")

# Check if the image was loaded properly.
if img is None:
print("Error: Image not found or unable to load.")
else:
# Get the dimensions of the image.
dimensions = img.shape
height = dimensions[0]
width = dimensions[1]
channels = dimensions[2]

cv2_imshow(img)

print("Image Dimension: {} x {} pixels with {} channels".format(width, height, channels))
print("Image Dimension: {}".format(dimensions))
cv2.waitKey(0)
cv2.destroyAllWindows()

Loading Images with PIL:

Alternatively, we can use PIL to load and read images:

from PIL import Image

# Provide the path to your uploaded image
image_path = '/content/Lata.jpg'
image = Image.open(image_path)

# Display image properties (width, height, mode)
width, height = image.size
mode = image.mode
print(f"Image loaded successfully. Dimensions: {width}x{height}, Mode: {mode}")
image.show()

#Output - "Image loaded successfully. Dimensions: 480x593, Mode: RGB"

Displaying Images using Matplotlib

Now that we have successfully loaded the image using either OpenCV or PIL, let’s move on to displaying the image using Matplotlib:

import matplotlib.pyplot as plt

# Load and read the image using PIL or OpenCV
image_path = '/content/Lata.jpg'
# image = cv2.imread(image_path) # Uncomment this line if using OpenCV
# image = Image.open(image_path) # Uncomment this line if using PIL

# Display the image using Matplotlib
plt.imshow(image)
plt.axis('off') # Hide axes for a cleaner look
plt.show()

In the above code, you can use either OpenCV or PIL to load and read the image. Just uncomment the corresponding lines based on your preference. The plt.imshow(image) function is used to display the image, and plt.axis('off') hides the axes, providing a cleaner display of the image.

Conclusion:

In this article, we explored how to load and read images in Google Colab using popular Python libraries such as OpenCV and PIL. We also demonstrated how to install the required libraries in Google Colab. Additionally, we used Matplotlib to visualize and display the loaded images. By following these steps, you can easily work with images and integrate them into various image processing, computer vision, and data visualization projects within the Google Colab environment. So go ahead, try out these techniques, and have fun working with images!

At the end of the article, it is thoughtful to provide references for the Image used in the Article and express condolences to the Late Lata Mangeshkar, a legendary Indian playback singer, and cultural icon who has left a lasting impact on the world of music. Her melodious voice and timeless songs continue to resonate with millions of people across generations.

Reference:

  1. Lata Mangeshkar — Wikipedia. (https://en.wikipedia.org/wiki/Lata_Mangeshkar)

Our deepest condolences go out to the family, friends, and fans of Late Lata Mangeshkar. Her contributions to the Indian music industry and her legacy will forever be cherished, and her voice will continue to inspire countless aspiring artists for years to come.

May her soul rest in eternal peace.

--

--