文件夹中的图片resize大小 python

这段代码用于将指定文件夹下的所有图片按照指定的宽度和高度进行缩放,并使用编号进行重命名。它首先列出文件夹中的所有图片,然后逐个读取,使用OpenCV的resize函数进行缩放,最后保存到新的文件夹中。如果目标文件夹不存在,代码会创建该文件夹。

将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('图片文件夹路径')
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值