使用 Python 的 PIL库 实现对深度学习图片集的数据增强

背景:

毕设设计相关,通过谷歌的tensorflow训练一个关于深度学习的图像识别,但是苦于原始数据集太过稀疏(就是很少),故通过数据增强的方法来对原始数据进行水平翻转等操作,这里谢谢学长为我们踩过的坑。

也很欣慰昨天终于能跑出来一个模型了,这之前图的标记却已进行近一个月....

要求:

该代码的运行 root 变量指定的是文件夹,存储文件夹是 storage 来记录,同时有两个约数变量 save_dirs 和 begin_rename_num ,前者指定是否存储到一个单独的文件夹,默认为 1,后者是对处理后的文件进行重命名指定开始的数字。。。。。啊,我的天,编不下去了,看代码看代码。。

学长要求:

原图命名为六位数字的png文件,标记图命名为六位数字+_L的 png 文件

参考代码:

2019年6月28日15:36:02 优化代码流程

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    :2019/04/16 09:21
# @Author  : Qdan
# @File    :新项目.py
# @Project :PyCharm
# @Description: 对图片数据集进行操作
#               项目要求:对给定目录下的三个子文件夹进行处理并保存在增强数据集子文件夹下
#

import os
from PIL import Image

# 工作路径
working_path = r"D:\Documents\Workspaces\毕业设计项目\00全局项目\堇青石角石"

# runing
##############################################################################################################

folders = os.listdir(working_path)


def pro_pictures(image_path, suffix, new_dir):
    """
    对给定图片进行数据增强处理
    :param image_path: 图片所在路径
    :param suffix: 序列图片后缀填充
    :param new_dir: 存储的新路径
    :return: 空值
    """
    rename_num = 1  # 命名的起始编号

    parent = image_path  # 图片路径
    fill_rename_num = 6  # 填充的位数
    suffix = suffix  # 标志填充字符

    filenames = os.listdir(image_path)
    save_image = working_path + r"\\处理数据集\\"

    save_image = save_image + new_dir
    if not os.path.exists(save_image):
        os.makedirs(save_image)

    for filename in filenames:
        # 获取当前路径下的当前文件
        current_path = os.path.join(parent, filename)
        picture_type = os.path.splitext(image_path + r"\\" + filename)
        new_image = Image.open(current_path)
        zero_fill = str(rename_num).zfill(fill_rename_num)  # 数字填充
        image_rename = save_image + f'\\{zero_fill}' + suffix + picture_type[1]  # 规范化重命名
        new_image.save(image_rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print(r"\n原图复制完成!")

    for filename in filenames:
        new_image = Image.open(current_path)
        current_path = os.path.join(parent, filename)
        out = new_image.transpose(Image.FLIP_LEFT_RIGHT)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        image_rename = save_image + f'\\{zero_fill}' + suffix + picture_type[1]
        out.save(image_rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print(r"\n水平翻转完成!")

    for filename in filenames:
        new_image = Image.open(current_path)
        current_path = os.path.join(parent, filename)
        out = new_image.transpose(Image.FLIP_TOP_BOTTOM)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        image_rename = save_image + f'\\{zero_fill}' + suffix + picture_type[1]
        out.save(image_rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print(r"\n垂直翻转完成!")

    for filename in filenames:
        new_image = Image.open(current_path)
        current_path = os.path.join(parent, filename)
        tmp = new_image.transpose(Image.FLIP_LEFT_RIGHT)
        out = tmp.transpose(Image.FLIP_TOP_BOTTOM)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        image_rename = save_image + f'\\{zero_fill}' + suffix + picture_type[1]
        out.save(image_rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print(r"\n水平和垂直翻转完成!")
    return


for folder in folders:
    image_path = working_path + r"\\" + folder
    if "jpg原图" == folder:
        pro_pictures(image_path, "", "jpg原图")
    elif "png填充" == folder:
        pro_pictures(image_path, "_L", "png填充")
    elif "png原图" == folder:
        pro_pictures(image_path, "", "png原图")

print("该文件夹任务处理完成!")

 

原代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    :2019/04/13 01:04
# @Author  : Qdan
# @File    :图片操作2.0.py
# @Project :PyCharm
# @Description: ::

from PIL import Image
import re
import os.path

######################## 设 置 项 目 #####################################
# 需要处理的文件夹
root = r"C:\Users\Dans\Desktop\毕业设计项目\0全局项目\2期重制\原图"

# 存储文件夹
storage = r"C:\Users\Dans\Desktop\毕业设计项目\0全局项目\2期重制\tmp"

# 导出填充后的 png
fill_png = 0

# --------------------------------------------------------------------------------

# 存储到一个文件夹
save_dirs = 1

# 开始数字
begin_rename_num = 475

# png图后缀
png_suffix = "_L"

# 填充位数
fill_rename_num = 6

#######################################################################
Source_File = root  # + '\\00原图'  # 必须得在原图文件夹下进行处理

# 存储到子文件夹,默认为一个文件夹
save_type = [r"\png原图", r"\png填充"]
if save_dirs == 0:
    save_dir = [r"\00原图", r"\01水平翻转", r"\02垂直翻转", r"\03水平和垂直翻转"]
else:
    save_dir = ["", "", "", "", ""]

original_image = storage + save_type[fill_png] + save_dir[0]
os.makedirs(original_image, True)

left_right = storage + save_type[fill_png] + save_dir[1]
if not os.path.exists(left_right):
    os.makedirs(left_right, True)

top_bottom = storage + save_type[fill_png] + save_dir[2]
if not os.path.exists(top_bottom):
    os.makedirs(top_bottom, True)

op_all = storage + save_type[fill_png] + save_dir[3]
if not os.path.exists(op_all):
    os.makedirs(op_all, True)

rename_num = begin_rename_num
Begin = ""
End = ""
file_type = ""
suffix = ""

# 开始批处理任务
for parent, dirnames, filenames in os.walk(Source_File):
    print('当前处理目录 :' + parent)

    for filename in filenames:
        # 正则表达式判断图片类型
        if rename_num == begin_rename_num:
            pattern = re.compile(r"\.[a-z||A-Z]*")
            file_type = str(pattern.findall(filename)).replace("['", "").replace("']", "")
            if fill_png == 1:
                suffix = "_L"  # 填充颜色的命名

        # 获取当前路径下的当前文件
        current_path = os.path.join(parent, filename)
        im = Image.open(current_path)
        zero_fill = str(rename_num).zfill(fill_rename_num)  # 数字填充
        rename = original_image + f'\\{zero_fill}' + suffix + file_type  # 规范化重命名
        # im.save(rename)
        # End = str(rename_num).zfill(fill_rename_num) + suffix
        print(rename_num, end="  ")
        # rename_num = rename_num + 1
    print("原图复制完成!")

    for filename in filenames:
        current_path = os.path.join(parent, filename)
        im = Image.open(current_path)
        out = im.transpose(Image.FLIP_LEFT_RIGHT)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        rename = left_right + f'\\{zero_fill}' + suffix + file_type
        out.save(rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print("水平翻转完成!")

    for filename in filenames:
        im = Image.open(current_path)
        current_path = os.path.join(parent, filename)
        out = im.transpose(Image.FLIP_TOP_BOTTOM)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        rename = top_bottom + f'\\{zero_fill}' + suffix + file_type
        out.save(rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print("垂直翻转完成!")

    for filename in filenames:
        im = Image.open(current_path)
        current_path = os.path.join(parent, filename)
        tmp = im.transpose(Image.FLIP_LEFT_RIGHT)
        out = tmp.transpose(Image.FLIP_TOP_BOTTOM)
        zero_fill = str(rename_num).zfill(fill_rename_num)
        rename = op_all + f'\\{zero_fill}' + suffix + file_type
        out.save(rename)
        print(rename_num, end="  ")
        rename_num = rename_num + 1
    print("水平和垂直翻转完成!")

    print(parent)
    print("当前目录处理完成!")

print("项目完成!")
# how are you, Dan?

需要完善的地方:

可以通过一个数据指定操作的过程,如只进行水平操作等....

你可能感兴趣的:(Python)