---- AI试用 ---域名问题某些图片和js资源无法访问,导致一些代码实例无法运行!(代码里gzui.net换成momen.vip即可)

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

Python 蚂蚁 3794℃ 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为固定尺寸,并存于指定的路径下

喜欢 (112)or分享 (0)