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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
