#-*- coding: UTF-8 -*-
from PIL import Image
import random
import os
#读取文件内的所有,迭代
def read_file_all(data_dir_path,save_dir):
for f in os.listdir(data_dir_path):
data_file_path = os.path.join(data_dir_path, f)
if os.path.isfile(data_file_path):
image_rotate(data_file_path,save_dir)
#print(collected)
else:
read_file_all(data_file_path)
def image_rotate(image_path,save_dir):
#读取图像
im = Image.open(image_path)
# 指定逆时针旋转的角度
save_path = save_dir + random_name() + '.jpg'
im.save(save_path)
im_rotate1 = im.rotate(90)
save_path = save_dir + random_name() + '.jpg'
im_rotate1.save(save_path)
im_rotate2 = im.rotate(180)
save_path = save_dir + random_name() + '.jpg'
im_rotate2.save(save_path)
im_rotate3 = im.rotate(270)
save_path = save_dir + random_name() + '.jpg'
im_rotate3.save(save_path)
def random_name():
#随机数
a_list = ['0','1','2','3','4','5','6','7','8','9']
name = random.sample(a_list,5)
file_name = "".join(name)
return file_name
if __name__ == '__main__':
data_dir_path = 'E:/image_class/data2/train/cat/' #读取的文件路径
save_dir = 'E:/image_class/data2/train/cats/'#保存的文件路径
read_file_all(data_dir_path,save_dir)
图片数据集扩充
最新推荐文章于 2024-07-25 18:55:58 发布