要求
文件存放在img_folder_path中
裁剪要求:
图片大小以高度为基准。居中裁剪
缩放要求:
图片缩放到512大小
图片另存到save_file_path路径中
代码
import numpy as np
import cv2
import os
from tqdm import tqdm#原图片存放位置
img_folder_path=r"D:\programFiles\Edge\Download\OIA-ODIR\Training Set\Images"filenames_list=os.listdir(img_folder_path)
img_path=""for i in tqdm(filenames_list):# read imgimg_path=img_folder_path+"\\"+iimg_=cv2.imread(img_path)# get crop infoshape_=img_.shapex0=int((shape_[1]-shape_[0])/2)x1=int(x0+shape_[0])y0=0y1=shape_[0]# croppingimg_cropped=img_[y0:y1,x0:x1]resized_image = cv2.resize(img_cropped, (512, 512), interpolation=cv2.INTER_LINEAR) save_file_path=r"./training_set/"+icv2.imwrite(save_file_path,resized_image)
原图
裁剪后