将image_path文件夹下的图片resize成(w,h)重命名并保存
import cv2
import os
def Resize_Image(image_path,w=64,h=64):
image_path_list = os.listdir(image_path)
image_path_list.sort()
number=1
for filename in image_path_list:
file = image_path+'/'+filename
img = cv2.imread((file), cv2.IMREAD_COLOR)
# 调用cv2.resize函数resize图片
new_img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
img_name = str(number)+'.jpg'
number+=1
'''生成图片存储的目标路径'''
save_path = image_path + '_new/'
if os.path.exists(save_path):
print(number)
'''调用cv.2的imwrite函数保存图片'''
save_img = save_path + img_name
cv2.imwrite(save_img, new_img)
else:
os.mkdir(save_path)
save_img = save_path + img_name
cv2.imwrite(save_img, new_img)
Resize_Image('图片文件夹路径')
这段代码用于将指定文件夹下的所有图片按照指定的宽度和高度进行缩放,并使用编号进行重命名。它首先列出文件夹中的所有图片,然后逐个读取,使用OpenCV的resize函数进行缩放,最后保存到新的文件夹中。如果目标文件夹不存在,代码会创建该文件夹。
872

被折叠的 条评论
为什么被折叠?



