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)
python 修改图片大小和VOC格式文件名
最新推荐文章于 2024-05-18 12:38:04 发布