Lab 1 Exp
Lab 1 Exp
img_pil.show() # Display the image using the default image viewer of the operating
system
# The show method opens the image in an external viewer, unlike OpenCV, which opens
its own window.
# Resize the PIL image using the resize method from PIL.Image
# Parameters: the original image (img_pil) and the desired size (width, height)
resized_pil = img_pil.resize((width, height))
# Call the resize_image function with the OpenCV and PIL images and desired
dimensions
# This function returns the resized OpenCV image (resized_cv) and the resized PIL
image (resized_pil)
resized_cv, resized_pil = resize_image(img_cv, img_pil, width, height)
# Display the resized OpenCV image in a window titled 'Resized Image - OpenCV'
cv2.imshow('Resized Image - OpenCV', resized_cv)
# Display the resized PIL image using the default image viewer of the operating
system
resized_pil.show()
# Set coordinates for the top-left (left, top) and bottom-right (right, bottom)
corners of the crop box
left, top, right, bottom = 50, 50, 300, 300
# Call the crop_image function with the OpenCV and PIL images and the cropping
coordinates
# The function returns the cropped images for both OpenCV and PIL
cropped_cv, cropped_pil = crop_image(img_cv, img_pil, left, top, right, bottom)
# Display the cropped OpenCV image in a window titled 'Cropped Image - OpenCV'
cv2.imshow('Cropped Image - OpenCV', cropped_cv)
# Display the cropped PIL image using the default image viewer of the operating
system
cropped_pil.show()
# Rotate the image using OpenCV's warpAffine function, passing the image,
# rotation matrix, and output size (width, height)
rotated_cv = cv2.warpAffine(img_cv, M, (w, h))
# Rotate the PIL image (img_pil) using its built-in rotate method
rotated_pil = img_pil.rotate(angle)
# Return both the rotated OpenCV image and rotated PIL image
return rotated_cv, rotated_pil
# Rotate both the OpenCV and PIL images by the specified angle
rotated_cv, rotated_pil = rotate_image(img_cv, img_pil, angle)
# Display the rotated PIL image using its show method (opens a window with the
image)
rotated_pil.show()
# Zoom the OpenCV image using the resize function. The 'fx' and 'fy' parameters
# specify the scale factor for width and height respectively (both set to
zoom_factor)
zoomed_cv = cv2.resize(img_cv, None, fx=zoom_factor, fy=zoom_factor)
# Return both the zoomed OpenCV image and zoomed PIL image
return zoomed_cv, zoomed_pil
# Define the zoom factor (1.5 means the image will be zoomed to 150% of the
original size)
zoom_factor = 1.5
# Zoom both the OpenCV and PIL images by the specified zoom factor
zoomed_cv, zoomed_pil = zoom_image(img_cv, img_pil, zoom_factor)
# Display the zoomed PIL image using its show method (opens a window with the
image)
zoomed_pil.show()
# Shrink the OpenCV image by resizing it using the specified shrink factor.
# The 'fx' and 'fy' parameters define the scale for the width and height
respectively.
# Since we are shrinking, the shrink_factor will be less than 1 (e.g., 0.5 for
50% size).
shrunk_cv = cv2.resize(img_cv, None, fx=shrink_factor, fy=shrink_factor)
# Return both the shrunk OpenCV image and the shrunk PIL image.
return shrunk_cv, shrunk_pil
# Define the shrink factor (0.5 means the image will be shrunk to 50% of its
original size).
shrink_factor = 0.5
# Shrink both the OpenCV and PIL images using the specified shrink factor.
shrunk_cv, shrunk_pil = shrink_image(img_cv, img_pil, shrink_factor)
# Display the shrunk PIL image using its built-in show method (opens a window with
the image).
shrunk_pil.show()
# Display the horizontally flipped PIL image using the default system image viewer
flipped_hor_pil.show()
# Display the vertically flipped PIL image using the default system image viewer
flipped_ver_pil.show()
# Wait indefinitely for a key press to keep both OpenCV windows open
cv2.waitKey(0)