基于python-opencv简单写了一个图像调整尺寸的代码,这一版本通过比例因子调整图像尺寸大小。
代码实现如下所示,如有错误或者理解不到位的地方还请各位指正!
单张调整图像尺寸
import cv2
def resize(image):
scale=1.5
new_image=cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)
return new_image
if __name__ == '__main__':
img=cv2.imread('temb.png')
new_img=resize(img)
IMSHOW(new_img)
批量调整图像尺寸
import cv2
import os
def resize_batch(read_path,new_path):
for filename in os.listdir(read_path):
image=cv2.imread(read_path+'/'+filename)
scale = 1.5
new_image = cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
cv2.imwrite(new_path+'/'+filename,new_image)
if __name__ == '__main__':
#D:/Project_python/code_tools/test:这是原始图像文件夹的地址
#D:/Project_python/code_tools/new:这是图像尺寸修改后保存文件夹地址
read_dir= "D:/Project_python/code_tools/test"
new_path="D:/Project_python/code_tools/new"
resize_batch(read_dir,new_path)