# NOTE : Always resize image with PIL and then read the image with matplotlib
### Resizing image
from PIL import Image
img = Image.open("your_image.jpg") # Open the image
resized_img = img.resize((16, 16)) # Resize the image
resized_img.save("resized_image.jpg") # Save or display the resized image
resized_img.show() # Show image
### Reading image for manipulation
import matplotlib.pyplot as plt
data = plt.imread('image.jpg')
plt.imshow(data)
plt.show()
print(data.shape) # See shape
print(data.format) # See format
print(data.size) # See dimension
print(data.mode) # See mode
# Setting channel values to 0 intensity
data[:, :, 0] = 0 # red channel
data[:, :, 1] = 0 # green channel
data[:, :, 2] = 0 # blue channel
plt.imshow(data)
gray_image = data.mean(axis=2)
### Using OpenCV
import cv2
rgb_image = cv2.imread("input_image.jpg")
gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2GRAY)
cv2.imwrite("output_image.jpg", gray_image) # Save the grayscale image
cv2.imshow("Grayscale Image", gray_image) # Display the grayscale image