Image Processing Using Python — Understanding Images, Pixels, Arrays & OpenCV
1. Introduction to Image Processing
Image processing is a technique used to perform operations on digital images to improve their quality, extract useful information, or identify important features.
In simple words, image processing means modifying or analyzing an image using a computer.
Python provides several libraries for image processing, such as:
- OpenCV
- NumPy
- Pillow
- Matplotlib
Among these, OpenCV is one of the most popular libraries used in computer vision and image processing applications.
Real-world applications:
- Face detection
- Medical image analysis
- Object detection
- Self-driving cars
- Security and surveillance
- Image enhancement
2. Understanding Digital Images
A digital image is an image represented in a computer using numerical values.
When we capture an image using a mobile phone or camera, it is converted into digital data that computers can understand.
A digital image consists of small elements called pixels.
Each pixel contains information about the color and brightness of a particular point in the image.
For example:
- A black pixel has a value of 0.
- A white pixel has a value of 255 in an 8-bit grayscale image.
- Other values represent different shades of gray.
3. What Are Pixels?
A pixel (Picture Element) is the smallest unit of a digital image.
An image is made up of thousands or millions of pixels arranged in rows and columns.
Important pixel concepts:
- Pixel Value: Represents the intensity or color information of a pixel.
- Image Dimensions: Represent the width and height of an image.
- Resolution: Refers to the total number of pixels in an image.
- Color Channels: Store color information such as red, green, and blue.
Example:
If an image has dimensions of 500 × 300:
- Width = 500 pixels
- Height = 300 pixels
- Total pixels = 150,000
4. Images as NumPy Arrays
Python uses the NumPy library to represent and manipulate images.
An image is stored as a NumPy array containing numerical pixel values.
For example:
import numpy as npimage = np.array([
[0, 100, 255],
[50, 150, 200],
[30, 180, 220]
])print(image)
print(image.shape)
Output:
[[ 0 100 255]
[ 50 150 200]
[ 30 180 220]](3, 3)Here, the image contains 3 rows and 3 columns.
Role of NumPy in Image Processing:
- Stores image data in array format.
- Performs mathematical operations on pixels.
- Helps resize and manipulate images.
- Supports efficient image processing operations.
5. Understanding Image Channels
Image channels represent the color information of an image.
RGB Image
RGB stands for Red, Green, and Blue.
These three color channels combine to produce different colors.
For example:
- Red = (255, 0, 0)
- Green = (0, 255, 0)
- Blue = (0, 0, 255)
- White = (255, 255, 255)
- Black = (0, 0, 0)
Grayscale Image
A grayscale image contains only shades of gray, ranging from black to white.
- 0 represents black.
- 255 represents white.
- Values between 0 and 255 represent different gray shades.
BGR Image
OpenCV generally reads color images in BGR format instead of RGB.
BGR stands for Blue, Green, and Red.
This difference is important when displaying or converting images using different libraries.
6. Introduction to OpenCV
OpenCV stands for Open Source Computer Vision Library.
It is an open-source library used for image processing, computer vision, and video analysis.
OpenCV supports Python, C++, and other programming languages.
Why is OpenCV important?
- It provides tools for image manipulation.
- It supports real-time video processing.
- It helps detect objects and faces.
- It supports image filtering and enhancement.
- It is widely used in AI and machine learning applications.
Installing OpenCV
pip install opencv-pythonImport the required libraries:
import cv2
import numpy as np
import matplotlib.pyplot as plt7. Basic OpenCV Functions
Here are some important OpenCV functions used in basic image processing.
1. imread()
Used to read or load an image.
Get Kammari Gnana Prasunambika’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax:
cv2.imread("image.jpg")Example:
image = cv2.imread("image.jpg")2. imshow()
Used to display an image in an OpenCV window.
Syntax:
cv2.imshow("Window Name", image)Example:
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()3. shape
Used to find the dimensions of an image.
print(image.shape)For a color image, the output generally represents:
(height, width, channels)4. resize()
Used to change the dimensions of an image.
Syntax:
cv2.resize(image, (width, height))Example:
resized = cv2.resize(image, (300, 300))5. cvtColor()
Used to convert an image from one color space to another.
Example: BGR to Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)6. flip()
Used to flip an image.
flipped = cv2.flip(image, 1)- 0 = Vertical flip
- 1 = Horizontal flip
- -1 = Both horizontal and vertical flip
7. GaussianBlur()
Used to reduce noise and smooth an image.
blurred = cv2.GaussianBlur(image, (5, 5), 0)8. Canny()
Used to detect edges in an image.
edges = cv2.Canny(image, 100, 200)9. imwrite()
Used to save a processed image.
cv2.imwrite("output.jpg", image)10. putText()
Used to add text to an image.
cv2.putText(
image,
"Image Processing",
(50, 50),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2
)8. Building a Basic Image Processing Project
Let’s build a simple image processing project using Python and OpenCV.
Project Title: Basic Image Processing Using OpenCV
Objective:
To load an image and perform different image processing operations such as resizing, grayscale conversion, blurring, edge detection, and saving the processed images.
Step 1: Import Libraries
import cv2
import numpy as np
import matplotlib.pyplot as pltStep 2: Load an Image
image = cv2.imread("image.jpg")if image is None:
print("Image not found")
else:
print("Image loaded successfully")Step 3: Check Image Dimensions
print("Image Shape:", image.shape)
print("Image Size:", image.size)
print("Image Data Type:", image.dtype)Step 4: Resize the Image
resized = cv2.resize(image, (300, 300))Step 5: Convert into Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)Step 6: Apply Gaussian Blur
blurred = cv2.GaussianBlur(image, (5, 5), 0)Step 7: Perform Edge Detection
edges = cv2.Canny(gray, 100, 200)Step 8: Display the Results
plt.figure(figsize=(10, 8))plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis("off")plt.subplot(2, 2, 2)
plt.imshow(gray, cmap="gray")
plt.title("Grayscale Image")
plt.axis("off")plt.subplot(2, 2, 3)
plt.imshow(cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB))
plt.title("Blurred Image")
plt.axis("off")plt.subplot(2, 2, 4)
plt.imshow(edges, cmap="gray")
plt.title("Edge Detection")
plt.axis("off")plt.tight_layout()
plt.show()
Step 9: Save the Processed Images
cv2.imwrite("resized_image.jpg", resized)
cv2.imwrite("grayscale_image.jpg", gray)
cv2.imwrite("blurred_image.jpg", blurred)
cv2.imwrite("edge_image.jpg", edges)print("All processed images saved successfully!")Project Output:
The project produces four different image-processing results:
- Resized Image
- Grayscale Image
- Blurred Image
- Edge Detection Image
This project demonstrates how OpenCV can be used to perform basic image-processing operations.
9. Conclusion
Image processing is an important part of computer vision and artificial intelligence.
In this blog, we explored digital images, pixels, NumPy arrays, image channels, and the OpenCV library. We also learned basic OpenCV functions and implemented a simple image-processing project.
Understanding these fundamentals provides a strong foundation for learning advanced concepts such as face recognition, object detection, image classification, and deep learning.
Learning image processing is the first step toward understanding how computers see and interpret the world!
#Python #OpenCV #ImageProcessing #NumPy #ComputerVision #MachineLearning #ArtificialIntelligence





