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.
Test Development Learning Exchange
Test Development Learning Exchange
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.