默认已安装好Python和OpenCV库,可以执行下面的代码,实现一个基本的图像读取和保存。 import numpy as np import matplotlib.pyplot as plt import cv2 #OPENCV默认图像数据为BGR格式 print("opencv version = ",cv2.__version__) #打印当前opencv的版本号 #读取图像文件 image = cv2.imread("C:\\works\\cat.jpg") #显示图像的维度和数据类型 print(f"图像的维度:{image.shape}") print(f"图像的数据类型:{image.dtype}") #获取图像的大小 height = image.shape[0] #高度 width = image.shape[1] #宽度 channels = image.shape[2] #像素的通道数- 1灰度图,3彩色图像BGR print("height = ",height) print("width = ",width) print("channels = ",channels) half_height = height/2 #取一半大小 half_width = width/2 #取一半大小 print("half_height = ",half_height) print("half_width = ",half_width) #缩放图像,新的图像的大小为原来的一半 resized_img = cv2.resize(image,(round(half_width),round(half_height))) #保存缩放后的图像 if resized_img is not None: cv2.imwrite("C:\\works\\output_cat