使用此方法将图片进行数据增强,具体增强图片的形式是如下几种:
""" 1、对比度:白色画面(最亮时)下的亮度除以黑色画面(最暗时)下的亮度; 2、色彩饱和度::彩度除以明度,指色彩的鲜艳程度,也称色彩的纯度; 3、色调:向负方向调节会显现红色,正方向调节则增加黄色。适合对肤色对象进行微调; 4、锐度:是反映图像平面清晰度和图像边缘锐利程度的一个指标。 """
代码如下:
import os from PIL import Image from PIL import ImageEnhance """ 1、对比度:白色画面(最亮时)下的亮度除以黑色画面(最暗时)下的亮度; 2、色彩饱和度::彩度除以明度,指色彩的鲜艳程度,也称色彩的纯度; 3、色调:向负方向调节会显现红色,正方向调节则增加黄色。适合对肤色对象进行微调; 4、锐度:是反映图像平面清晰度和图像边缘锐利程度的一个指标。 """ def augument(image_path, parent): #读取图片 image = Image.open(image_path) image_name = os.path.split(image_path)[ 1] name = os.path.splitext(image_name)[ 0] #变亮 #亮度增强,增强因子为0.0将产生黑色图像;为1.0将保持原始图像。 enh_bri = ImageEnhance.Brightness(image) brightness = 1.5 image_brightened1 = enh_bri.enhance(brightness) image_brightened1.save(os.path.join(parent, '{}_bri1.jpg'.format(name))) #变暗 enh_bri = ImageEnhance.Brightness(image) brightness = 0.8 image_brightened2 = enh_bri.enhance(brightness) image_brightened2.save(os.path.join(parent, '{}_bri2.jpg'.format(name))) #色度,增强因子为1.0是原始图像 # 色度增强 enh_col = ImageEnhance.Color(image) color = 1.5 image_colored1 = enh_col.enhance(color) image_colored1.save(os.path.join(parent, '{}_col1.jpg'.format(name))) # 色度减弱 enh_col = ImageEnhance.Color(image) color = 0.8 image_colored1 = enh_col.enhance(color) image_colored1.save(os.path.join(parent, '{}_col2.jpg'.format(name))) #对比度,增强因子为1.0是原始图片 # 对比度增强 enh_con = ImageEnhance.Contrast(image) contrast = 1.5 image_contrasted1 = enh_con.enhance(contrast) image_contrasted1.save(os.path.join(parent, '{}_con1.jpg'.format(name))) # 对比度减弱 enh_con = ImageEnhance.Contrast(image) contrast = 0.8 image_contrasted2 = enh_con.enhance(contrast) image_contrasted2.save(os.path.join(parent, '{}_con2.jpg'.format(name))) # 锐度,增强因子为1.0是原始图片 # 锐度增强 enh_sha = ImageEnhance.Sharpness(image) sharpness = 3.0 image_sharped1 = enh_sha.enhance(sharpness) image_sharped1.save(os.path.join(parent, '{}_sha1.jpg'.format(name))) # 锐度减弱 enh_sha = ImageEnhance.Sharpness(image) sharpness = 0.8 image_sharped2 = enh_sha.enhance(sharpness) image_sharped2.save(os.path.join(parent, '{}_sha2.jpg'.format(name))) dir = 'E:/4/' for parent, dirnames, filenames in os.walk(dir): for filename in filenames: fullpath = os.path.join(parent + '/', filename) if 'jpg' in fullpath: print(fullpath, parent) augument(fullpath, parent)