• 【python】批量调整图片亮度和饱和度,比PS还方便的图片处理,cv2


    使用python中的cv2模块批量调整图片亮度和饱和度,比PS还方便!

    1. 同一批图片批量处理亮度和饱和度先通过以下链接先确定数值大小

    使用滑动条调整图片亮度和饱和度

    2. 确定数值后再使用本文代码进行批量调整

    完整代码:

    import numpy as np
    import cv2
    import os
    # 调整最大值
    MAX_VALUE = 100
    
    def update(input_img_path, output_img_path, lightness, saturation):
        """
        用于修改图片的亮度和饱和度
        :param input_img_path: 图片路径
        :param output_img_path: 输出图片路径
        :param lightness: 亮度
        :param 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 = 'imgs'
    output_dir = 'output'
    
    #这里调参!!!
    lightness = int(input("lightness(亮度-100~+100):")) # 亮度
    saturation = int(input("saturation(饱和度-100~+100):")) # 饱和度
    
    # 获得需要转化的图片路径并生成目标路径
    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)
    

    效果图:

  • 相关阅读:
    关于用户流量运营相关方面
    郑州房市
    公众号运营
    面试知识点总结
    Ubuntu根目录下各文件夹的功能详细介绍(轉)
    ubuntu 使用印象筆記 evernote nixnote2
    安装ktorrent amule 下载edk2 迅雷文件
    i2p
    ubuntu开启ipv6
    tg-bot备忘
  • 原文地址:https://www.cnblogs.com/helenlee01/p/12707306.html
Copyright © 2020-2023  润新知