【Python】批量改变图像饱和度

import os
from PIL import Image, ImageEnhance


def adjust_saturation(input_folder, output_folder, saturation_level):

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for filename in os.listdir(input_folder):

        if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')):

            img_path = os.path.join(input_folder, filename)
            img = Image.open(img_path)

            enhancer = ImageEnhance.Color(img)
            img_enhanced = enhancer.enhance(saturation_level)

            output_path = os.path.join(output_folder, filename)
            img_enhanced.save(output_path)
            print(f"Processed {filename}")


input_folder  = r'C:\Users\Administrator\Desktop\6.26' 
output_folder = r'C:\Users\Administrator\Desktop\Saturation'  
saturation_level = 2.0  

adjust_saturation(input_folder, output_folder, saturation_level)

### 使用Python增强图像饱和度 为了提高图像饱和度,可以利用多种Python库来实现这一目标。其中一种常用的方法是通过`PIL`(即`Pillow`)库中的颜色空间转换功能来进行操作[^2]。 下面是一个具体的例子,展示如何使用`Pillow`库调整图像饱和度: ```python from PIL import Image, ImageEnhance def enhance_saturation(image_path, saturation_factor=1.5): """ 增强给定路径下图片的饱和度 参数: image_path (str): 输入图像文件路径. saturation_factor (float): 饱和度增强因子,默认为1.5倍. 返回: enhanced_image (Image.Image): 经过处理后的图像对象. """ with Image.open(image_path) as img: enhancer = ImageEnhance.Color(img) enhanced_image = enhancer.enhance(saturation_factor) return enhanced_image if __name__ == "__main__": input_img_path = "example.jpg" output_img_path = "enhanced_example.jpg" result = enhance_saturation(input_img_path, 2.0) # 将饱和度设置为原图两倍 result.save(output_img_path) ``` 此代码片段定义了一个名为`enhance_saturation()`函数,该函数接收两个参数:一个是待处理的图像文件路径;另一个是指定要应用到原始饱和水平上的乘数系数`saturated_factor`。当这个值大于1时,则表示增加了色彩强度;反之则会减弱它。 此外,在上述基础上还可以考虑采用更高级别的框架如`imgaug`或`albumentations`等专门用于计算机视觉任务的数据预处理工具包来进行更加复杂的变换组合[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值