A Beginner’s Guide to Image Processing with Python: From Pixels to OpenCV
What is an image?
An image is a visual representation of an object, person, place, or scene. It can be a photograph, drawing, picture, or digital graphic.
In simple words, an image is something we see visually on a screen or on paper.
How Do Machines See and Understand Images?
A machine represents a digital image as a collection of tiny elements called pixels (picture elements).
Each pixel contains numerical information representing brightness or colour. Each pixel in a colour image contains three numerical values representing the intensity of these three colour channels: Blue, Green, and Red (BGR).
Each channel has values ranging from 0 to 255:
- 0 represents no intensity of that colour.
- 255 represents maximum intensity of that colour.
Simple way to understand
Think of an image as an Excel sheet:
- Rows represent image height.
- Columns represent image width.
- Each cell represents one pixel.
- Each pixel contains numerical colour information.
OpenCV
OpenCV (Open Source Computer Vision Library) is a popular open-source Python library used for image processing and computer vision. It helps computers read, process, analyse, and understand images and videos.
In simple terms, OpenCV provides tools that allow us to perform operations on images, such as resizing, cropping, changing colours, detecting edges, and identifying objects. OpenCV uses the BGR colour format by default, instead of the commonly used RGB (Red, Green, Blue) format.
How does OpenCV represent an image?
OpenCV uses NumPy arrays to represent images.
- Grayscale image: A 2D array (height × width).
- Colour image: A 3D array (height × width × 3), with three colour channels: Blue, Green, and Red (BGR).
import cv2
img = cv2.imread("image.jpg")
cv2.imshow("My First Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()cv2.imread()– Reads an image from the specified file path.cv2.imshow()– Displays the image in a window.cv2.waitKey(0)– Waits until a key is pressed.cv2.destroyAllWindows()– Closes all OpenCV display windows.
What is the role of NumPy in image processing?
NumPy (Numerical Python) is a Python library used for working with numerical data and multidimensional arrays.
In image processing, NumPy helps us:
- Store images: Represents images as 2D and 3D arrays.
import cv2
img = cv2.imread("image.jpg")
# Save the image
cv2.imwrite("new_image.jpg", img2. Modify pixels: Change the colour or brightness of pixels.
import cv2
# Read an image
img = cv2.imread("image.jpg")
# Modify a pixel at row 100, column 100
img[100, 100] = [255, 0, 0]
# Display modified image
cv2.imshow("Modified Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()3. Perform mathematical operations: Adjust image intensity and manipulate pixel values.
4. Crop images
Basic Opencv Functions:
- Converting to greyscale:
To change the colour representation of an image using OpenCV, we use cv2.cvtColor().
Get Nikitha Samanthula’s stories in your inbox
Join Medium for free to get updates from this writer.
The cvtColor() function converts an image from one colour space to another.
(i) Convert BGR to RGB
import cv2
# Read image
img = cv2.imread("image.jpg")
# Convert BGR to RGB
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Display image
cv2.imshow("RGB Image", rgb_img)
cv2.waitKey(0)
cv2.destroyAllWindows()(ii) Convert colour image to grayscale
import cv2
img = cv2.imread("image.jpg")
# Convert BGR to Grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale Image", gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()2. Image cropping:
Image cropping is the process of extracting a specific portion of an image by selecting the required rows and columns.
It helps us focus on a particular object or region while removing unwanted areas.
import cv2
# Read an image
img = cv2.imread("image.jpg")
# Crop the image
cropped_img = img[100:300, 150:400]
# Display original image
cv2.imshow("Original Image", img)
# Display cropped image
cv2.imshow("Cropped Image", cropped_img)
cv2.waitKey(0)
cv2.destroyAllWindows()3. Image Blurring :
Image blurring is an image processing technique used to reduce noise, smooth image details, and remove unnecessary information from an image.
In OpenCV, blurring is performed by applying a filter (kernel) to the image. This filter averages or combines the pixel values around each pixel to create a smoother appearance.
import cv2
image = cv2.imread('image.jpg')
blurred_image = cv2.blur(image, ksize=(20,20))
cv2.imshow("pic", blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()4. Edge Detection with OpenCV
It is a technique used to identify the boundaries of objects in an image. It detects points where the image brightness changes sharply, which usually correspond to object edges.
Why is Edge Detection important?
- Helps locate objects in an image.
- Reduces unnecessary image details while preserving important structural information.
- Used in object detection, image segmentation, face recognition, autonomous vehicles, and medical imaging.
The most commonly used method by OpenCV is Canny Edge Detection.
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Apply Gaussian Blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Perform Canny Edge Detection
edges = cv2.Canny(blurred, 100, 200)
# Display the original and edge-detected images
cv2.imshow('Original Image', image)
cv2.imshow('Edge Detection', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()5. Image rotation
Image rotation is a geometric transformation that turns an image around a fixed point, usually its center, by a specified angle.
import cv2
image = cv2.imread('image.jpg')
rotated=cv2.rotate(image,cv2.ROTATE_90_CLOCKWISE)
cv2.imshow("pic",rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()6. Image flipping
Image flipping means creating a mirror image of the original image across a specific axis.
import cv2
# Read image
img = cv2.imread("boy.jpg")
# Horizontal Flip (Left ↔ Right)
horizontal = cv2.flip(img, 1)
# Vertical Flip (Top ↔ Bottom)
vertical = cv2.flip(img, 0)
# Both Axes Flip
both = cv2.flip(img, -1)
# Display
cv2.imshow("Original", img)
cv2.imshow("Horizontal Flip", horizontal)
cv2.imshow("Vertical Flip", vertical)
cv2.imshow("Both Axes Flip", both)
cv2.waitKey(0)
cv2.destroyAllWindows()FlipCode Operation Result
1 Horizontal Flip Left ↔ Right
0 Vertical Flip Top ↔ Bottom
-1 Both Axes Flip Horizontal + Vertical Flip
(similar to a 180° rotation effect)7. Image Threshold
Image Threshold is an image segmentation technique that converts a grayscale image into a binary (black and white) image.
It works by comparing each pixel value with a chosen threshold value.
import cv2
# Read image in grayscale mode
image = cv2.imread("img.webp", 0)
# Resize image
resized = cv2.resize(image, (300, 300))
# Apply thresholding
_, thresh = cv2.threshold(
resized, # source image
127, # threshold value
255, # maximum value
cv2.THRESH_BINARY
)
# Display images
cv2.imshow("Original Image", resized)
cv2.imshow("Threshold Image", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()Conclusion:
OpenCV provides a powerful and efficient set of tools for image processing, enabling us to manipulate, analyze, and enhance images with just a few lines of code. Throughout this chapter, we explored fundamental operations such as image loading, resizing, rotation, flipping, and thresholding, which form the foundation of many advanced computer vision applications.
- Resizing helps adjust image dimensions for different display and processing requirements.
- Rotation changes the orientation of an image by a specified angle.
- Flipping creates mirror images either horizontally, vertically, or along both axes.
- Thresholding converts grayscale images into binary (black-and-white) images, making object detection and segmentation easier.
These techniques are widely used in real-world applications such as face recognition, OCR (Optical Character Recognition), medical imaging, autonomous vehicles, security systems, image enhancement, and machine learning pipelines.
In summary, image processing with OpenCV transforms raw images into meaningful visual information. By mastering these basic operations, you build a strong foundation for exploring advanced topics such as image filtering, feature extraction, object detection, image segmentation, and deep learning-based computer vision.
“Image processing is the bridge between raw pixel data and intelligent computer vision systems, and OpenCV is one of the most powerful tools to build that bridge.” 🚀






