python 修改图片大小和VOC格式文件名

import os
from PIL import Image
Extension = ['jpeg', 'png', 'jpg']
files = os.listdir('F://Images//')  # 需要处理的图片路径


def resize_image(filename, i, width=800, height=800):
    image = Image.open('F://Images//'+filename)
    w, h = image.size
    if w <= width and h <= height:
        print(filename, 'is OK.')
        return
    if (1.0 * w / width) > (1.0 * h / height):
        scale = 1.0 * w / width
        new_image = image.resize((int(w / scale), int(h / scale)), Image.ANTIALIAS)

    else:
        scale = 1.0 * h / height
        new_image = image.resize((int(w / scale), int(h / scale)), Image.ANTIALIAS)
    if i < 10:
        name = '00000'+str(i)
    if 9 < i < 100:
        name = '0000'+str(i)
    if 99 < i < 1000:
        name = '000' + str(i)
    if 999 < i < 10000:
        name = '00' + str(i)
    new_image.save('F://NewImage//' + str(name)+'.jpg')  # 处理之后图片保存的路径和文件名
    new_image.close()


for i, file in enumerate(files):
    if file.split('.')[-1] in Extension:
        print('正在处理第{0}张图片'.format(i+1))
        resize_image(file, i+1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿尔卑斯糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值