from PIL import Image
import numpy as np
import os
import os.path
import cv2
MAX_VALUE = 100
'''
### 图片左右对称 ===
rootdir = r'F:\data\png_3' # 指明被遍历的文件夹
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 fulll name of the file is :' + currentPath)
im = Image.open(currentPath)
out = im.transpose(Image.FLIP_LEFT_RIGHT)
newname = r"F:\data\png_3_sym" + '\\' + filename + "_sym.jpg"
out.save(newname)
'''
### 图片亮度饱和度调整 ===
def update(input_img_path, output_img_path, lightness, saturation):
# 加载图片 读取彩色图像归一化且转换为浮点型
image = cv2.imread(input_img_path, cv2.IMREAD_COLOR).astype(np.float32) / 255.0
# 颜色空间转换 BGR转为HLS
hlsImg = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
# 1.调整亮度(线性变换)
hlsImg[:, :, 1] = (1.0 + lightness / float(MAX_VALUE)) * hlsImg[:, :, 1]
hlsImg[:, :, 1][hlsImg[:, :, 1] > 1] = 1
# 饱和度
hlsImg[:, :, 2] = (1.0 + saturation / float(MAX_VALUE)) * hlsImg[:, :, 2]
hlsImg[:, :, 2][hlsImg[:, :, 2] > 1] = 1
# HLS2BGR
lsImg = cv2.cvtColor(hlsImg, cv2.COLOR_HLS2BGR) * 255
lsImg = lsImg.astype(np.uint8)
cv2.imwrite(output_img_path, lsImg)
dataset_dir = 'F:\data\img'
output_dir = 'F:\data\lightdown'
# 这里调参!!!
lightness = -8 # 亮度
saturation = 3 # 饱和度
# 获得需要转化的图片路径并生成目标路径
image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_dir, x))
for x in os.listdir(dataset_dir)]
# 转化所有图片
for path in image_filenames:
update(path[0], path[1], lightness, saturation)
Python:数据增强,水平翻转/亮度饱和度调整
猜你喜欢
转载自blog.csdn.net/m0_37908025/article/details/107015178
今日推荐
周排行