Understanding Image Data: How it stores and process in the Computer
Introduction
Images are everywhere in our digital world, from social media to websites and games. But how does a computer understand, store, and work with images? In this article, we’ll break down what an image is, why images are useful, and how to load, modify, and save images using simple Python code. We’ll also save each processed image so we can later create a video from them.
1. What is an Image?
An image is a collection of tiny squares called pixels arranged in rows and columns, like a grid. Each pixel holds a color or shade that contributes to the whole picture. There are two main types of images:
- Grayscale images: Pixels show different shades of gray.
- Color images: Pixels have four channels—Red, Green, Blue (RGB), and Alpha (transparency)—that combine to form different colors.
2. Applications of Images
Images are essential for various purposes:
- Photography: Digital photos and image editing.
- Healthcare: X-ray, MRI, and other medical imaging.
- Robotics and AI: Detecting objects in computer vision.
- Maps and Satellites: Satellite images to study land and weather.
3. How Are Images Stored in Computers?
Computers store images as numbers. Each pixel in the image has a value:
- Grayscale images: A single value represents how light or dark the pixel is, from black (0) to white (255).
- Color images: Each pixel has four values (Red, Green, Blue, and Alpha), with values ranging from 0 to 255.
Let’s load and save an image in Python.
We’ll use the Pillow library to work with images. Install it if needed:
pip install pillow numpy matplotlib
How to Load and display an Image
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load an image
image = Image.open('images/leaves.png')
plt.imshow(image)
plt.title("Original Image")
plt.axis('off')
plt.show()
4. Grayscale Images
A grayscale image has pixels represented by single values between 0 and 255:
0
is black.255
is white.- Numbers between create different shades of gray.
5. Converting Images to Grayscale in Python
To convert a color image to grayscale:
# Convert to grayscale
gray_image = image.convert('L')
gray_image.save('images/sample_grayscale.png') # Save grayscale image
gray_array = np.array(gray_image)
# Show the grayscale image
plt.imshow(gray_array, cmap='gray')
plt.axis('off')
plt.title("Grayscale Image")
plt.show()
Here, we convert the color image to grayscale, save it as sample_grayscale.png
, and display it. The gray_array
stores the grayscale pixel values in a 2D format.
6. Color Images (RGBA)
Color images (RGBA) have four values for each pixel:
- Red
- Green
- Blue
- Alpha (transparency)
Code for RGBA Color Images
# Convert image to an array to view pixel data
color_array = np.array(image)
# Save and display the color image
image.save('images/sample_color.png') # Save the original color image
plt.imshow(color_array)
plt.axis('off')
plt.title("Color Image (RGBA)")
plt.show()
# Display the matrix form
print("color_array Image Matrix:")
print(color_array.shape)
print(color_array)
7. Comparing Grayscale and Color Images
Feature | Grayscale Image | Color Image (RGBA) |
---|---|---|
Data Structure | 2D array (e.g., 200×200) | 3D array (e.g., 200x200x4) |
Pixel Value | One brightness value (0-255) | Four values for RGBA (0-255 each) |
Storage Size | Smaller due to fewer channels | Larger due to RGBA channels |
8. Modifying Image Pixels in Python
Modifying Pixels in Grayscale and Color
Let’s change a small section of each image to white. White in grayscale is 255
and [255, 255, 255, 255]
in color (RGBA).
Grayscale Image:
# Modify a region of grayscale image to white
gray_modified = gray_array.copy()
gray_modified[20:50, 20:50] = 255 # Set region to white
# Save and show modified grayscale image
Image.fromarray(gray_modified).save('images/sample_grayscale_modified.png')
plt.imshow(gray_modified, cmap='gray')
plt.axis('off')
plt.title("Modified Grayscale Image")
plt.show()
9. Basic Operations on Images with Code
You can resize, rotate, and flip images in Python. Each step saves the modified image.
# Resize the image
resized_image = image.resize((100, 100))
plt.imshow(resized_image)
plt.axis('off')
plt.title("Resize the image")
plt.show()
resized_image.save('images/sample_resized.png') # Save resized image
# Rotate the image by 45 degrees
rotated_image = image.rotate(45)
plt.imshow(rotated_image)
plt.axis('off')
plt.title("Rotate the image by 45 degrees")
plt.show()
rotated_image.save('images/sample_rotated.png') # Save rotated image
# Flip the image horizontally
flipped_image = image.transpose(Image.FLIP_LEFT_RIGHT)
plt.imshow(flipped_image, cmap='gray')
plt.axis('off')
plt.title("Flip the image horizontally")
plt.show()
flipped_image.save('images/sample_flipped.png') # Save flipped image
10. How to Create a Video from Images
Videos are made of many images displayed quickly. We can combine all our saved images into a video using OpenCV in Python.
Code to Create Video
import cv2
import glob
# Load saved images into a list
img_array = []
for filename in sorted(glob.glob('images/sample_*.png')): # Include saved image files with 'sample_' prefix
img = cv2.imread(filename)
height, width, layers = img.shape
img_array.append(img)
# Create video from images
out = cv2.VideoWriter('images/output_video.avi', cv2.VideoWriter_fourcc(*'DIVX'), 1, (width, height))
for img in img_array:
out.write(img)
out.release()
This code compiles images saved as sample_*.png
into a video called output_video.avi
. The 1 indicates the video speed (1 frames per second).
11. Summary
- What is an Image: An image is made of tiny squares called pixels.
- Applications: Used in photography, healthcare, computer vision, and maps.
- Storage: Stored as numbers, representing pixel colors or brightness.
- Python Operations: Load, save, convert to grayscale, modify pixels, resize, rotate, flip, and combine into a video.
With these basics, you’re ready to start working with images in Python and even experiment with basic image processing!