本文目录导读:

使用 Python + Pillow(最常用)
from PIL import Image, ImageEnhance
import os
from pathlib import Path
def batch_adjust_saturation(input_dir, output_dir, saturation_factor=1.5):
"""
批量调整图片饱和度
:param input_dir: 输入目录
:param output_dir: 输出目录
:param saturation_factor: 饱和度因子(1.0=原始,>1增加,<1减少)
"""
# 创建输出目录
Path(output_dir).mkdir(parents=True, exist_ok=True)
# 支持的图片格式
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff')
for filename in os.listdir(input_dir):
if filename.lower().endswith(supported_formats):
try:
# 打开图片
img_path = os.path.join(input_dir, filename)
img = Image.open(img_path)
# 调整饱和度
enhancer = ImageEnhance.Color(img)
img_adjusted = enhancer.enhance(saturation_factor)
# 保存图片
output_path = os.path.join(output_dir, filename)
img_adjusted.save(output_path)
print(f"已处理: {filename}")
except Exception as e:
print(f"处理 {filename} 时出错: {e}")
# 使用示例
batch_adjust_saturation(
input_dir="input_images",
output_dir="output_images",
saturation_factor=1.5 # 增加50%饱和度
)
使用 OpenCV(RGB转换)
import cv2
import numpy as np
import os
def batch_adjust_saturation_opencv(input_dir, output_dir, saturation_scale=1.5):
"""
使用OpenCV批量调整饱和度
"""
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.jpg', '.png', '.jpeg')):
# 读取图片
img = cv2.imread(os.path.join(input_dir, filename))
# 转换到HSV颜色空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 调整饱和度(S通道)
hsv[:, :, 1] = np.clip(hsv[:, :, 1] * saturation_scale, 0, 255)
# 转换回BGR
img_adjusted = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# 保存
cv2.imwrite(os.path.join(output_dir, filename), img_adjusted)
print(f"已处理: {filename}")
# 使用示例
batch_adjust_saturation_opencv(
input_dir="input_images",
output_dir="output_images",
saturation_scale=1.5
)
更灵活的版本(可调节参数)
from PIL import Image, ImageEnhance
import os
import argparse
from pathlib import Path
class ImageSaturationAdjuster:
def __init__(self):
self.supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff')
def adjust_saturation(self, image_path, saturation_factor):
"""调整单张图片的饱和度"""
img = Image.open(image_path)
enhancer = ImageEnhance.Color(img)
return enhancer.enhance(saturation_factor)
def batch_process(self, input_dir, output_dir, saturation_factor):
"""批量处理"""
# 创建输出目录
Path(output_dir).mkdir(parents=True, exist_ok=True)
# 获取所有图片文件
files = [f for f in os.listdir(input_dir)
if f.lower().endswith(self.supported_formats)]
if not files:
print("没有找到支持的图片文件")
return
for i, filename in enumerate(files, 1):
try:
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
# 调整饱和度
img_adjusted = self.adjust_saturation(input_path, saturation_factor)
# 保存
img_adjusted.save(output_path)
print(f"[{i}/{len(files)}] 已处理: {filename}")
except Exception as e:
print(f"处理 {filename} 时出错: {e}")
def dry_run(self, input_dir, saturation_factor):
"""预览效果(不保存)"""
files = [f for f in os.listdir(input_dir)
if f.lower().endswith(self.supported_formats)]
for filename in files[:5]: # 只预览前5张
input_path = os.path.join(input_dir, filename)
img_adjusted = self.adjust_saturation(input_path, saturation_factor)
print(f"{filename}: 原始尺寸 {img_adjusted.size}, 饱和度因子 {saturation_factor}")
# 使用示例
if __name__ == "__main__":
adjuster = ImageSaturationAdjuster()
# 批量处理
adjuster.batch_process(
input_dir="input_images",
output_dir="output_images",
saturation_factor=1.5 # 增加50%饱和度
)
# 或者预览效果
# adjuster.dry_run("input_images", 1.5)
命令行版本(使用ImageMagick)
#!/bin/bash
# batch_saturation.sh
INPUT_DIR="input_images"
OUTPUT_DIR="output_images"
SATURATION="150%" # 百分比,100%为原始
# 创建输出目录
mkdir -p "$OUTPUT_DIR"
# 批量处理
for img in "$INPUT_DIR"/*.{jpg,jpeg,png,bmp}; do
if [ -f "$img" ]; then
filename=$(basename "$img")
convert "$img" -modulate 100,$SATURATION,100 "$OUTPUT_DIR/$filename"
echo "已处理: $filename"
fi
done
安装依赖
# 安装 Python 依赖 pip install Pillow opencv-python # 安装 ImageMagick(Linux) sudo apt-get install imagemagick # 安装 ImageMagick(Mac) brew install imagemagick
使用说明
-
饱和度因子说明:
0= 原始饱和度> 1.0= 增加饱和度(如1.5表示增加50%)< 1.0= 降低饱和度(如0.5表示减少50%)0= 完全去色(黑白)
-
建议先备份原图,批量处理后不满意可以恢复
-
根据需求选择方法:
- Pillow:简单易用,适合一般需求
- OpenCV:处理速度更快,适合大批量处理
- ImageMagick:命令行操作,适合自动化脚本
-
批量处理前建议先用1-2张测试效果