0
0
Computer Visionml~15 mins

Image properties (shape, dtype, size) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Image properties (shape, dtype, size)
Problem:You want to understand the basic properties of images used in machine learning, such as their shape, data type, and size in memory.
Current Metrics:N/A - This is a data exploration task without model training.
Issue:Beginners often do not know how to check image properties, which is important for preprocessing and debugging.
Your Task
Load an image using Python, then print its shape, data type, and size in bytes. Ensure the code runs without errors and outputs correct values.
Use only standard Python libraries and OpenCV (cv2) or PIL for image loading.
Do not use any deep learning frameworks for this task.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2

# Load image from file
image = cv2.imread('sample_image.jpg')  # Replace with your image path

# Check if image loaded successfully
if image is None:
    print('Error: Image not found or unable to load.')
else:
    # Get shape (height, width, channels)
    shape = image.shape
    # Get data type
    dtype = image.dtype
    # Calculate size in bytes
    size_bytes = image.size * image.itemsize

    print(f'Image shape: {shape}')
    print(f'Image data type: {dtype}')
    print(f'Image size in bytes: {size_bytes}')
Added code to load an image using OpenCV.
Printed image shape, data type, and calculated size in bytes.
Included error handling if image path is incorrect.
Results Interpretation

Before: No knowledge of image properties, which can cause errors in ML pipelines.

After: Able to print image shape (e.g., (480, 640, 3)), data type (e.g., uint8), and size in bytes (e.g., 921600).

Knowing image properties helps you prepare data correctly for machine learning models and avoid common errors.
Bonus Experiment
Try loading the same image using PIL (Pillow) and print its properties similarly.
💡 Hint
Use PIL.Image.open() and convert the image to a numpy array to access shape and dtype.