Resizing the image to a specific width and height with opencv in Python

Today, I would like to share a program for resizing the images to a specific width and height with opencv library in Python. Let’s take a look.

Opencv has resize method cv2.resize() to resize the images.

We can resize the images simply as follows.

import cv2

# read the input
img = cv2.imread('input.jpeg')
height, width, channel = img.shape
print(f"Height and width of original image: {height}, {width}")

# resize the image
new_size = (450, 340) # (width, height)
print(f"New height and width: {new_size[1]}, {new_size[0]}")
resize_img = cv2.resize(img, new_size)

cv2.imshow('Original img', img)
cv2.imshow('Resized img', resize_img)

cv2.waitKey(0)

The program above will resize the input image and show original and resized images.

Actually, cv2.resize() has other functionalities. Please study more about this. It is interesting.

This is all for now. Hope you enjoy that.

By Asahi



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム