一张图片可以扩充七张
# coding = utf-8
import cv2
from PIL import Image
from PIL import ImageEnhance
from numpy.ma import array
import numpy as np
import os
# 批量处理代码
rootdir = 'D:/dataset' # 指明被遍历的文件夹
def high_bright(currentPath, filename, targetPath):
# 读取图像
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# image.show()
# 增强亮度 bh_
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.07
image_brightened_h = enh_bri.enhance(brightness)
# image_brightened_h.show()
image_brightened_h.save(targetPath+ '1' + filename) # 保存
def low_bright(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# 降低亮度 bl_
enh_bri_low = ImageEnhance.Brightness(image)
brightness = 0.87
image_brightened_low = enh_bri_low.enhance(brightness)
# image_brightened_low.show()
image_brightened_low.save(targetPath+ '2' + filename)
def color(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# 改变色度 co_
enh_col = ImageEnhance.Color(image)
color = 0.8
image_colored = enh_col.enhance(color)
# image_colored.show()
image_colored.save(targetPath + '3' + filename)
def cont(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# 改变对比度 cont_
enh_con = ImageEnhance.Contrast(image)
contrast = 0.8
image_contrasted = enh_con.enhance(contrast)
# image_contrasted.show()
image_contrasted.save(targetPath + '4' + filename)
def sha(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# 改变锐度 sha_
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 3.0
image_sharp = enh_sha.enhance(sharpness)
# image_sharp.show()
image_sharp.save(targetPath +' 5'+ filename)
def yre(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# y方向上的缩放 yre_
# image.show()
w = image.width
h = image.height
out_ww = image.resize((w, h + 40)) # 拉伸成高为h的正方形
# out_ww.show()
out_ww_1 = np.array(out_ww)
out_w_2 = out_ww_1[30:(h - 10), 0:w] # 开始的纵坐标,开始的横坐标
out_w_2 = Image.fromarray(out_w_2)
# out_w_2.show()
out_w_2.save(targetPath + '6' +filename)
def xre(currentPath, filename, targetPath):
image = Image.open(currentPath)
image_cv = cv2.imread(currentPath)
# x方向上的缩放 xre_
# image.show()
w = image.width
h = image.height
out_hh = image.resize((w + 80, h)) # 拉伸成宽为w的正方形,width,height
# out_hh.show()
out_hh_1 = array(out_hh)
out_h_2 = out_hh_1[0:h, 40:(w + 40)]
out_h_2 = Image.fromarray(out_h_2)
# out_h_2.show()
out_h_2.save(targetPath + '7' + filename)
if __name__ == '__main__':
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
# print('parent is:' + parent)
print('filename is: ' + filename)
# 把文件名添加到一起后输出
currentPath = os.path.join(parent, filename)
# print('the full name of file is :' + currentPath)
# 保存处理后的图片的目标文件夹
targetPath = 'D:/dataset/dataset_augmentasion/'
# 进行处理
high_bright(currentPath, filename, targetPath)
low_bright(currentPath, filename, targetPath)
color(currentPath, filename, targetPath)
cont(currentPath, filename, targetPath)
sha(currentPath, filename, targetPath)
yre(currentPath, filename, targetPath)
xre(currentPath, filename, targetPath)