Image Processing using Python — Understanding Images, Pixels, Arrays & OpenCV
Introduction
Have you ever wondered how a computer recognizes a face, detects objects in a photograph, or converts a colorful image into black and white?
All these operations are possible through a technology called Image Processing.
Image processing is the technique of using computer algorithms to analyze, modify, enhance, and extract useful information from digital images.
Today, image processing plays an important role in several industries, including healthcare, security, automotive technology, agriculture, and entertainment.
Python provides powerful libraries for image processing. Among them, OpenCV and NumPy are two of the most commonly used tools.
In this blog, we will understand how digital images are represented inside computers, what pixels and color channels are, how NumPy stores images as arrays, and how to perform basic image processing operations using OpenCV.
1. Understanding Digital Images
A digital image is a visual representation of information stored in a computer.
Unlike traditional photographs printed on paper, digital images are represented using a collection of tiny elements called pixels.
A computer does not understand an image in the same way that humans do. It processes an image as numerical information.
For example, when we open a photograph on our computer, the system reads the numerical values associated with its pixels and displays them as a visual representation.
Digital images are commonly classified into two categories:
1. Raster Images
Raster images are made up of a grid of pixels. Each pixel contains information about color or intensity.
Examples:
- JPEG
- PNG
- BMP
- GIF
2. Vector Images
Vector images are created using mathematical paths, curves, and shapes instead of individual pixels.
Examples:
- SVG
- AI
- EPS
In image processing using OpenCV, we primarily work with raster images.
2. Understanding Pixels
A pixel, short for Picture Element, is the smallest individual unit of a digital image.
An image is made up of thousands or millions of pixels arranged in rows and columns.
Each pixel stores information about the color or brightness of a particular point in the image.
Pixel Values
The values of pixels depend on the type of image.
Grayscale Image:
A grayscale image contains different shades of gray.
Pixel values generally range from 0 to 255:
- 0 represents black.
- 255 represents white.
- Values between 0 and 255 represent different shades of gray.
For example:
- 0 = Black
- 64 = Dark Gray
- 128 = Medium Gray
- 192 = Light Gray
- 255 = White
Color Image:
A standard 8-bit color image contains three values for each pixel:
- Red (R)
- Green (G)
- Blue (B)
Each channel generally has values ranging from 0 to 255.
For example:
- (255, 0, 0) = Red
- (0, 255, 0) = Green
- (0, 0, 255) = Blue
- (255, 255, 255) = White
- (0, 0, 0) = Black
Image Dimensions
Image dimensions describe the width and height of an image in pixels.
For example, an image with dimensions 1920 × 1080 contains:
- Width = 1920 pixels
- Height = 1080 pixels
- Total pixels = 2,073,600
The greater the number of pixels, the more spatial detail an image can potentially contain.
3. How Images Are Represented as Arrays
One of the most important concepts in image processing is understanding how images are stored in computers.
Python uses the NumPy library to represent images as multidimensional arrays.
NumPy stands for Numerical Python. It provides efficient operations for working with numerical data.
When an image is loaded using OpenCV, it is generally returned as a NumPy array.
Grayscale Image as a 2D Array
A grayscale image is represented using a two-dimensional array.
Each element represents the intensity value of one pixel.
Example:
import numpy as npimage = np.array([
[0, 64, 128],
[64, 128, 192],
[128, 192, 255]
])print(image)
Output:
[[ 0 64 128]
[ 64 128 192]
[128 192 255]]Here, the array contains 3 rows and 3 columns, representing a 3 × 3 grayscale image.
Color Image as a 3D Array
A color image is generally represented as a three-dimensional array.
Its dimensions are:
(height, width, channels)For example:
(400, 600, 3)This represents:
- Height = 400 pixels
- Width = 600 pixels
- Channels = 3
The three channels represent the color components of each pixel.
Why NumPy Is Important in Image Processing
NumPy makes image processing easier because:
- It stores image data efficiently.
- It supports mathematical operations on entire arrays.
- It allows us to access and modify individual pixels.
- It supports slicing and cropping.
- It works seamlessly with OpenCV and other Python libraries.
4. Understanding Image Channels
A channel represents one component of the color information in an image.
Different image formats use different channel configurations.
RGB — Red, Green and Blue
RGB is a color model that combines three primary colors:
- Red
- Green
- Blue
By combining different intensities of these three colors, we can represent a wide range of colors.
For example:
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)Grayscale
A grayscale image contains only one channel.
Instead of storing three color values for each pixel, it stores a single intensity value.
This reduces the amount of image data and is useful for tasks such as edge detection and image analysis.
BGR — OpenCV’s Default Color Order
An important point to remember is that OpenCV uses BGR (Blue, Green, Red) as its default channel order when reading standard color images.
This is different from the commonly used RGB format.
For example, the color red is represented as:
RGB = (255, 0, 0)
BGR = (0, 0, 255)Understanding this difference is important when displaying images using libraries such as Matplotlib.
5. Introduction to OpenCV
OpenCV stands for Open Source Computer Vision Library.
It is an open-source library designed for image processing and computer vision applications.
OpenCV provides a large collection of functions that help developers read, display, modify, analyze, and process images and videos.
Why Do We Use OpenCV?
Some important advantages of OpenCV include:
- It is open-source and freely available.
- It provides optimized image processing functions.
- It supports multiple programming languages, including Python and C++.
- It works efficiently with NumPy.
- It supports both image and video processing.
- It provides tools for object detection, face detection, and feature extraction.
Applications of OpenCV
OpenCV is used in many real-world applications:
- Face recognition and detection
- Medical image analysis
- Self-driving cars
- Security and surveillance systems
- Object detection
- Image enhancement
- Motion detection
- Industrial quality inspection
6. Installing OpenCV and NumPy
Before using OpenCV, we need to install the required libraries.
Get Sampath Kpl’s stories in your inbox
Join Medium for free to get updates from this writer.
Open a terminal or command prompt and execute:
pip install opencv-python numpy matplotlibImport the libraries:
import cv2
import numpy as np
import matplotlib.pyplot as pltHere:
cv2is the Python interface for OpenCV.numpyis used for numerical and array operations.matplotlibis used to display images inside notebooks and plots.
7. Basic OpenCV Functions
Let’s understand some commonly used OpenCV functions with their syntax and practical examples.
1. Reading an Image — imread()
The cv2.imread() function is used to read an image from a specified file path.
Syntax:
cv2.imread(image_path)Example:
img = cv2.imread("image.jpg")print(img)By default, OpenCV reads a color image in BGR format.
We can also specify the reading mode:
cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)2. Displaying an Image — imshow()
The cv2.imshow() function displays an image in a separate window.
Syntax:
cv2.imshow(window_name, image)Example:
img = cv2.imread("image.jpg")cv2.imshow("Original Image", img)cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(0) waits until a key is pressed.
cv2.destroyAllWindows() closes the OpenCV display windows.
Note: In Jupyter Notebook environments, Matplotlib is often more convenient for displaying images.
3. Checking Image Dimensions — shape
The shape attribute of a NumPy array provides the dimensions of an image.
Example:
img = cv2.imread("image.jpg")print(img.shape)Possible output:
(400, 600, 3)This means the image has 400 rows, 600 columns, and 3 color channels.
For a grayscale image, the output generally contains only height and width.
4. Resizing an Image — resize()
The cv2.resize() function changes the dimensions of an image.
Syntax:
cv2.resize(image, (width, height))Example:
resized = cv2.resize(img, (300, 200))This changes the image size to 300 pixels wide and 200 pixels high.
5. Converting an Image to Grayscale — cvtColor()
The cv2.cvtColor() function converts an image from one color space to another.
Syntax:
cv2.cvtColor(image, conversion_code)Example:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)This converts a BGR color image into a grayscale image.
6. Cropping an Image — Array Slicing
Cropping means extracting a specific region from an image.
Since OpenCV images are NumPy arrays, we can use array slicing.
Syntax:
cropped = img[y1:y2, x1:x2]Example:
cropped = img[50:250, 100:300]This extracts a region using the specified row and column ranges.
Remember that NumPy slicing follows the order:
image[y, x]7. Applying Gaussian Blur — GaussianBlur()
Gaussian Blur is used to smooth an image and reduce noise.
Syntax:
cv2.GaussianBlur(image, kernel_size, sigmaX)Example:
blurred = cv2.GaussianBlur(img, (5, 5), 0)The kernel size must be positive and odd in both dimensions.
A larger kernel generally produces stronger blurring.
8. Edge Detection — Canny()
Canny Edge Detection identifies the boundaries of objects in an image.
Syntax:
cv2.Canny(image, threshold1, threshold2)Example:
edges = cv2.Canny(gray, 100, 200)Here:
- 100 is the lower threshold.
- 200 is the upper threshold.
The output highlights important edges in the image.
9. Saving an Image — imwrite()
The cv2.imwrite() function saves a processed image to a file.
Syntax:
cv2.imwrite(filename, image)Example:
cv2.imwrite("output.jpg", gray)This saves the grayscale image as output.jpg
8. Building a Basic Image Processing Project
Now that we understand the fundamental concepts and OpenCV functions, let’s combine them to build a simple project.
Project: Basic Image Processing Application Using Python and OpenCV
Objective:
To develop a simple application that reads an image and performs multiple image processing operations, such as grayscale conversion, resizing, Gaussian blurring, and edge detection.
Step 1: Import Required Libraries
import cv2
import numpy as np
import matplotlib.pyplot as pltStep 2: Load the Image
img = cv2.imread("image.jpg")if img is None:
raise FileNotFoundError("Image not found!")print("Image Shape:", img.shape)
print("Image Data Type:", img.dtype)
This loads the image and checks its dimensions and data type.
Step 3: Display the Original Image
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(img_rgb)
plt.title("Original Image")
plt.axis("off")
plt.show()We convert BGR to RGB because Matplotlib expects RGB channel order for color images.
Step 4: Convert the Image to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)plt.imshow(gray, cmap="gray")
plt.title("Grayscale Image")
plt.axis("off")
plt.show()This converts the original color image into a single-channel grayscale image.
Step 5: Resize the Image
resized = cv2.resize(img, (300, 300))plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title("Resized Image")
plt.axis("off")
plt.show()This changes the image dimensions to 300 × 300 pixels.
Step 6: Apply Gaussian Blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)plt.imshow(cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB))
plt.title("Blurred Image")
plt.axis("off")
plt.show()This smooths the image and helps reduce small variations and noise.
Step 7: Perform Edge Detection
edges = cv2.Canny(gray, 100, 200)plt.imshow(edges, cmap="gray")
plt.title("Edge Detection")
plt.axis("off")
plt.show()This identifies the edges and boundaries present in the image.
Step 8: Save the Processed Images
cv2.imwrite("grayscale.jpg", gray)
cv2.imwrite("resized.jpg", resized)
cv2.imwrite("blurred.jpg", blurred)
cv2.imwrite("edges.jpg", edges)print("All processed images saved successfully!")This saves all the processed outputs in the current working directory.
8. Conclusion
In this blog, we explored the fundamentals of image processing using Python.
We learned how digital images are represented using pixels, how pixel values define colors and intensity, and how images are stored as NumPy arrays.
We also explored RGB, grayscale, and BGR color channels and understood the importance of OpenCV in computer vision.
Finally, we implemented a basic image processing project using important OpenCV functions, including image reading, resizing, grayscale conversion, Gaussian blur, edge detection, and saving images.
Image processing is an important foundation for advanced technologies such as artificial intelligence, machine learning, facial recognition, and autonomous systems.
Learning NumPy and OpenCV provides a strong starting point for exploring the exciting field of computer vision.
Thank you for reading!







