LambdaLabTM
Artificial Intelligence · Class 10 · Practical File · Program 9 of 15
Computer VisionFrom the CBSE curriculum list⏱️ 6 min read

Program 9: Read an Image and Identify Its Shape

Write a program to read an image and identify its shape using Python.

1Aim

Write a program to read an image and identify its shape using Python.
Python 3OpenCV⬇ photo.jpg

Keep photo.jpg in the same folder as the program.

2Program

image_shape.py
# Program 9: Read an image and identify its shape using Python
# The "shape" of an image is its size as an array of numbers:
# height (rows of pixels) x width (columns of pixels) x colour channels.

import cv2

image = cv2.imread("photo.jpg")
print("Shape of the colour image :", image.shape)

height, width, channels = image.shape
print("Height   :", height, "pixels")
print("Width    :", width, "pixels")
print("Channels :", channels, "(Blue, Green, Red)")
print("Total pixels         :", height * width)
print("Total numbers stored :", image.size)
print()

grey = cv2.imread("photo.jpg", cv2.IMREAD_GRAYSCALE)
print("Shape of the grayscale image :", grey.shape)
print("A grayscale image has no third number: one value per pixel.")
print()

if height > width:
    print("This image is in portrait shape (taller than wide).")
elif width > height:
    print("This image is in landscape shape (wider than tall).")
else:
    print("This image is square.")

3Output

The output of a real run, exactly as it appeared.

Output
Shape of the colour image : (600, 512, 3)
Height   : 600 pixels
Width    : 512 pixels
Channels : 3 (Blue, Green, Red)
Total pixels         : 307200
Total numbers stored : 921600

Shape of the grayscale image : (600, 512)
A grayscale image has no third number: one value per pixel.

This image is in portrait shape (taller than wide).

4How it works

image.shape

The shape of the image array: (height, width, channels). Height is the number of rows of pixels, width the number of columns.

height, width, channels = image.shape

Puts the three numbers into three variables.

image.size

The total count of numbers stored: height × width × 3.

cv2.imread("photo.jpg", cv2.IMREAD_GRAYSCALE)

Reads the image in grayscale. Its shape has only two numbers, because each pixel is one value.

Note
In this practical, "shape" means the shape of the image array — its dimensions. For identifying the geometric shape of an object in a picture (circle, triangle, rectangle), see the Road Signs project.

5Try this

  • Find the shape of a photo taken on your phone.
  • Resize the image to half its size with cv2.resize() and print the new shape.
  • Crop the top half with image[0:300, :] and print its shape.

6Viva questions

▸What does image.shape return?

The height, width and number of colour channels of the image.

▸Why does a grayscale image's shape have only two numbers?

Each pixel has a single brightness value, so there is no third dimension for colour channels.

▸How many numbers does a 600 × 512 colour image store?

600 × 512 × 3 = 921,600.

Tip
In your practical file, write the aim, the program and the output for each program — run it yourself and copy your own output. See The Practical File for how to set the file out.