Name : Syed Muhammad Hassan
Reg no : B23F1000DS056
Program: Data Science
Digital image processing lab 1
Task 1
Write a program to read a digital image, split and display the
image into 4 quadrants: up, down, left and right.
from PIL import Image
import matplotlib.pyplot as plt
import cv2
# Load the image
image = Image.open("images.jpeg") # Replace with your image path
# Display the image using matplotlib
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
if hasattr(image, 'size'):
# It's a PIL image, convert to array
image_array = np.array(image)
height, width = image_array.shape[0], image_array.shape[1]
else:
# Assume it's already a numpy array
image_array = image
height, width = image_array.shape[0], image_array.shape[1]
# Calculate the center points
center_x = width // 2
center_y = height // 2
# Split the image into 4 quadrants
top_left = image_array[0:center_y, 0:center_x]
top_right = image_array[0:center_y, center_x:width]
bottom_left = image_array[center_y:height, 0:center_x]
bottom_right = image_array[center_y:height, center_x:width]
# Create a figure with proper subplot arrangement
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Display each quadrant in its own subplot
axes[0, 0].imshow(top_left)
axes[0, 0].set_title('Top-Left Quadrant')
axes[0, 0].axis('off')
axes[0, 1].imshow(top_right)
axes[0, 1].set_title('Top-Right Quadrant')
axes[0, 1].axis('off')
axes[1, 0].imshow(bottom_left)
axes[1, 0].set_title('Bottom-Left Quadrant')
axes[1, 0].axis('off')
axes[1, 1].imshow(bottom_right)
axes[1, 1].set_title('Bottom-Right Quadrant')
axes[1, 1].axis('off')
plt.tight_layout()
plt.show()
Task 2:
Give a program to show rotation, scaling and translation of
the same image.
if hasattr(image, 'size'):
# Convert PIL image to numpy array
image_array = np.array(image)
height, width = image_array.shape[0], image_array.shape[1]
else:
# Assume it's already a numpy array
image_array = image
height, width = image_array.shape[0], image_array.shape[1]
# Apply multiple transformations
transformations = []
# 1. Original
transformations.append(('Original', image_array))
# 2. Scaled down
scaled_down = cv2.resize(image_array, (width//2, height//2),
interpolation=cv2.INTER_AREA)
transformations.append(('Scaled 0.5x', scaled_down))
scaled_up = cv2.resize(image_array, (width*2, height*2),
interpolation=cv2.INTER_LINEAR)
transformations.append(('Scaled 2.0x', scaled_up))
# 4. Rotated and scaled
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 30, 0.8)
rotated_scaled = cv2.warpAffine(image_array, rotation_matrix, (width,
height))
transformations.append(('Rotated 30° + Scaled 0.8x', rotated_scaled))
# Create a figure with proper subplot arrangement
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Display each transformation in its own subplot
for i, (title, transformed_img) in enumerate(transformations):
row = i // 2
col = i % 2
axes[row, col].imshow(transformed_img)
axes[row, col].set_title(title)
axes[row, col].axis('off')
plt.tight_layout()
plt.show()