Resizing Images with Python and OpenCV

This article demonstrates how to use Python's OpenCV library to read an image, display its original dimensions, resize it to a specified size, save the resized image, and handle user input to close the display windows.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Resizing Images with Python and OpenCV

This article provides a step‑by‑step example of using Python and the OpenCV library to change an image’s size.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/7/17 下午9:53
# @Author  : huaan

import cv2 as cv

img = cv.imread("one.jpg")
cv.imshow("img", img)
print("原来图片的形状大小:", img.shape)  # shape显示图片大小

resize_img = cv.resize(img, dsize=(400, 460))
cv.imshow("resize_img", resize_img)
cv.imwrite("resize_img.jpg",resize_img)

print("修改后图片形状大小:", resize_img.shape)

# 当只有键盘输入q时,才退出
while True:
    if ord("q")==cv.waitKey(0):
        print("当前输入的是:",cv.waitKey(0),"请用键盘输入:q")
        break

cv.destroyAllWindows()

The script reads an image, shows its original dimensions, resizes it to 400 × 460 pixels, saves the new file, and waits for the user to press “q” before closing all OpenCV windows.

Follow the public account for more learning resources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonImage ProcessingOpenCVResize
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.