BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 1
Date: 27/08/24
Image Acquisition
Aim: To generate, display, and manipulate grayscale and RGB images using Python libraries
such as PIL, OpenCV, and Matplotlib. The images are displayed with varying intensity
levels, checkerboard patterns, gradients, and webcam capture.
Algorithm:
1. Step 1: Import the required libraries (PIL, OpenCV, Matplotlib).
o Syntax: import cv2, import matplotlib.pyplot as plt, from PIL import Image
o Explanation: These libraries are essential for image processing and visualization.
2. Step 2: Load the image from the specified file path using PIL or OpenCV.
o Syntax: image = Image.open(file_path) or image = cv2.imread(file_path)
o Explanation: This step loads the image into a variable for further processing.
3. Step 3: Convert the image to RGB format if necessary using cv2.cvtColor.
o Syntax: image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
o Explanation: OpenCV loads images in BGR format by default, so conversion to
RGB is required for proper visualization.
4. Step 4: Display the image using Matplotlib with options to hide axes or modify the
display size.
o Syntax: plt.imshow(image)
o Explanation: This function displays the image using the specified color map.
5. Step 5: For more advanced manipulation, such as resizing or capturing an image from a
webcam, use cv2.resize or cv2.VideoCapture.
6. Step 6: Capture and display the manipulated image using Matplotlib.
BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 2
Program 1: To load and display an image using PIL
Output:
Source Code:
image = Image.open('C:\\Users\\sanch\\OneDrive\\Desktop\\wallpaper\\
2019BrazilianGrandPrixSunday_2ST8108.jpeg')
print('orignal size',image)
plt.imshow(image)
plt.axis('off') # Hide axis
plt.show()
BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 3
Program 2: To load and display an image in RGB format using OpenCV
Source Code:
from PIL import Image
import matplotlib.pyplot as plt
# Load an image from a file
image = Image.open('C:\\Users\\sanch\\OneDrive\\Desktop\\wallpaper\\
2019BrazilianGrandPrixSunday_2ST8108.jpeg')
# Convert the image to grayscale
gray_image = image.convert('L')
# Save the grayscale image
gray_image.save('grayscale_image.jpeg')
# Display the resized and grayscale images
plt.imshow(gray_image)
plt.axis('off') # Hide axis
plt.show()
Output:
Source Code:
import cv2
import matplotlib.pyplot as plt
# Load the image from a file
image = cv2.imread('C:\\Users\\sanch\\OneDrive\\Desktop\\wallpaper\\
2019BrazilianGrandPrixSunday_2ST8108.jpeg') # Replace with your image file
path
# Convert the image from BGR (OpenCV default) to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image using Matplotlib
plt.imshow(image_rgb)
plt.title("Loaded Image")
plt.axis('off') # Hide the axis
plt.show()
Output:
BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 4
Program 3: To resize and display an image using cv2
Source Code:
from PIL import Image
import matplotlib.pyplot as plt
# Load an image from a file
image = Image.open('C:\\Users\\sanch\\OneDrive\\Desktop\\wallpaper\\
2019BrazilianGrandPrixSunday_2ST8108.jpeg')
# Resize the image
resized_image = image.resize((300, 300))
# Display the resized and grayscale images
plt.imshow(gray_image)
plt.axis('off') # Hide axis
plt.show()
Output:
BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 5
Program 4: To capture and display an image using a webcam
Source Code:
# Open the webcam (usually, 0 is the index for the default camera)
cap = cv2.VideoCapture(0)
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Could not open webcam.")
else:
# Capture a frame from the webcam
ret, frame = cap.read()
# Release the webcam
cap.release()
if ret:
# Convert the image from BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the captured image
plt.imshow(frame_rgb)
plt.title("Captured Image from Webcam")
plt.axis('off') # Hide the axis
plt.show()
else:
print("Error: Could not read frame.")
Output:
BDS572AL_ Image Processing | [22112032]
5 BSc (Data Science) Department of Data Science
Page | 6
Program 5: Find the RGB equivalent colours for this Hue Values.
Source Code:
import matplotlib.pyplot as plt
# Define the RGB values for each hue
colors = {
"0° (Red)": (1, 0, 0),
"60° (Yellow)": (1, 1, 0),
"120° (Green)": (0, 1, 0),
"180° (Cyan)": (0, 1, 1),
"240° (Blue)": (0, 0, 1),
"300° (Magenta)": (1, 0, 1),
"360° (Red)": (1, 0, 0)
# Plot the colors
fig, ax = plt.subplots(figsize=(8, 6))
# Display each color in a bar with its label
for i, (label, color) in enumerate(colors.items()):
ax.barh(i, 1, color=color, edgecolor='black')
ax.text(0.5, i, label + f" RGB: {tuple(int(c*255) for c in color)}",
ha='center', va='center', color='white', fontsize=12)
# Adjust the plot
ax.set_xlim(0, 1)
ax.set_ylim(-1, len(colors))
ax.axis('off')
# Show the plot
plt.show()
Output: