遍历文件,进行数据增强,重命名,tensor格式以图像输出

1、遍历文件,进行数据增强,重命名

from PIL import Image
import os
from PIL import ImageEnhance
import numpy as np

root = "D:/Codes/data/face"
os.makedirs("D:/Codes/data/face_auge",exist_ok=True)
root2 = "D:/Codes/data/face_auge"
j = 1000
for dir in os.listdir(root):
    os.makedirs(os.path.join(root2,dir),exist_ok=True)
    for image in os.listdir(os.path.join(root,dir)):   #遍历目录下的文件

        img = Image.open(os.path.join(root,dir) + '/' +image)
        img = img.convert('RGB')        #将图像统一为三通道
        print(dir)
        img.save(os.path.join(root2,dir) + '/' + str(j) + image)
        # img = img.resize((size,size))     #将图像尺寸统一为224*224
        sca = range(0,360,36)   #0,36,72....
        for i in sca:  #图像旋转十张
            img2 = img.rotate(i)   #图像旋转
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1
        # resize = range(64,1024,64)
        # for i in resize:
        #     for z in resize:
        #         img2 = img.resize(img.resize((i, z)))
        #         img2.save(os.path.join(root, dir) + '/' + str(j) + image)
        #         j += 1

        point = np.arange(0.6,1.4,0.1)
        for z in point:
            img2 = img.point(lambda i: i * z)  #每个像素点扩大1.2倍
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1

        for z in point:
            enh1 = ImageEnhance.Contrast(img)  # 创建调整对比度对象
            enh2 = ImageEnhance.Color(img)  # 色彩饱和度
            enh3 = ImageEnhance.Brightness(img)  # 亮度
            enh4 = ImageEnhance.Sharpness(img)   # 清晰度
            img2 = enh1.enhance(z) #增加30%对比度
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1
            img2 = enh2.enhance(z) #增加30%对比度
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1
            img2 = enh3.enhance(z) #增加30%对比度
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1
            img2 = enh4.enhance(z) #增加30%对比度
            img2.save(os.path.join(root2,dir) + '/' + str(j) + image)
            j +=1

for dir in os.listdir(root):
    # os.makedirs(os.path.join(root2,dir),exist_ok=True)
    tmp =1
    root3 = os.path.join(root, dir)
    for image in os.listdir(os.path.join(root,dir)):   #遍历目录下的文件
        os.rename(os.path.join(root3,image),os.path.join(root3,str(tmp)+".jpg"))
        tmp +=1

2、tensor格式以图像输出

from torchvision.transforms import ToPILImage

show = ToPILImage()
img3 = show(img3)
plt.subplot(2,2,4)
plt.imshow(img3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值