Python将文件夹里面的所有图片resize为固定尺寸,并存于指定的路径下

前端开发 蚂蚁 230℃ 0评论

https://blog.csdn.net/wss794/article/details/83342404
原始:

import os
from PIL import Image
 
 
def save_resize_img(img_path, img_path_train, width, height):
    if not os.path.exists(img_path_train):
        os.mkdir(img_path_train)
    number = 0
    for label_name in os.listdir(img_path):
        if not os.path.exists(img_path_train + label_name + '/'):
            os.mkdir(img_path_train + label_name + '/')
        for imgs_name in os.listdir(img_path + label_name):
            img = (Image.open(img_path + label_name + '/' + imgs_name))
            img = img.resize((width, height), Image.ANTIALIAS)
            number += 1
            img.save(img_path_train + label_name + '/' + str(number) + '.jpg')
 
 
 
if __name__ == '__main__':
    save_resize_img("D:/workmyself/trainData/", "D:/workmyself/data/", 224, 224)

修改过:

#!/usr/bin/python3
import os
from PIL import Image
 
def save_resize_img(img_path, img_path_train, width, height):
    if not os.path.exists(img_path_train):
        os.mkdir(img_path_train)
    number = 0
    for label_name in os.listdir(img_path):
        if not os.path.exists(img_path_train + '/'):
            os.mkdir(img_path_train + '/')
            print(label_name)
        for imgs_name in os.listdir(img_path + label_name):
            img = (Image.open(img_path + label_name + '/' + imgs_name))
            img = img.resize((width, height), Image.ANTIALIAS)
            number += 1
            img.save(img_path_train + '/' + str(number) + '.jpg')
 
 
 
if __name__ == '__main__':
    save_resize_img("E:/壁纸美图/", "E:/壁纸美图/data/", 224, 224)

转载请注明:有爱前端 » Python将文件夹里面的所有图片resize为固定尺寸,并存于指定的路径下

喜欢 (48)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址