Introduction
Resizing images changes their size to fit your needs. It helps models work faster and use less memory.
Jump into concepts and practice - no test required
Resizing images changes their size to fit your needs. It helps models work faster and use less memory.
resized_image = cv2.resize(image, (new_width, new_height))
The cv2.resize function is from the OpenCV library.
The size is given as (width, height) in pixels.
resized = cv2.resize(img, (100, 100))
resized = cv2.resize(img, (200, 150))
resized = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
This code creates a small 3x3 color image, resizes it to 6x6, and prints the shapes and pixel values before and after resizing.
import cv2 import numpy as np # Create a simple 3x3 image with 3 color channels (RGB) image = np.array([ [[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 255, 0], [0, 255, 255], [255, 0, 255]], [[0, 0, 0], [127, 127, 127], [255, 255, 255]] ], dtype=np.uint8) print('Original image shape:', image.shape) # Resize image to 6x6 pixels resized_image = cv2.resize(image, (6, 6)) print('Resized image shape:', resized_image.shape) # Show pixel values of resized image print('Resized image array:') print(resized_image)
Resizing can change the image quality; enlarging may cause blurriness.
Use interpolation methods like cv2.INTER_LINEAR or cv2.INTER_AREA for better results.
Always keep the aspect ratio if you want to avoid stretching the image.
Resizing changes image size to fit your needs.
Use cv2.resize with target width and height.
Resizing helps models run faster and saves space.
import cv2
image = cv2.imread('photo.jpg')
resized = cv2.resize(image, (100, 50))
print(resized.shape)import cv2
img = cv2.imread('img.png')
resized_img = cv2.resize(img, 200, 100)
print(resized_img.shape)