本文目录导读:

- Python + Pillow(通用方案)
- OpenCV(更专业的色彩校正)
- ImageMagick(命令行方案)
- Photoshop 动作(GUI方案)
- 专业批量处理工具
- 视频色彩校正(适用于视频帧)
- 各方案对比
Python + Pillow(通用方案)
from PIL import Image, ImageEnhance
import os
from pathlib import Path
def batch_color_correction(input_dir, output_dir,
brightness=1.0, contrast=1.0,
saturation=1.0, color_balance=None):
"""
批量色彩校正
:param input_dir: 输入文件夹路径
:param output_dir: 输出文件夹路径
:param brightness: 亮度(0.0-2.0)
:param contrast: 对比度(0.0-2.0)
:param saturation: 饱和度(0.0-2.0)
:param color_balance: 颜色平衡元组 (R,G,B) (1.2, 1.0, 0.8)
"""
Path(output_dir).mkdir(parents=True, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff')):
img_path = os.path.join(input_dir, filename)
img = Image.open(img_path)
# 调整亮度
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(brightness)
# 调整对比度
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(contrast)
# 调整饱和度
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(saturation)
# 颜色平衡调整
if color_balance:
r, g, b = img.split()
r = r.point(lambda i: i * color_balance[0])
g = g.point(lambda i: i * color_balance[1])
b = b.point(lambda i: i * color_balance[2])
img = Image.merge('RGB', (r, g, b))
# 保存
output_path = os.path.join(output_dir, filename)
img.save(output_path, quality=95)
print(f"已处理: {filename}")
# 使用示例
batch_color_correction(
input_dir="./input_images",
output_dir="./output_images",
brightness=1.1,
contrast=1.2,
saturation=0.9,
color_balance=(0.8, 1.0, 1.2) # 减少红色,增加蓝色
)
OpenCV(更专业的色彩校正)
import cv2
import numpy as np
import os
def auto_white_balance(img):
"""自动白平衡"""
result = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
avg_a = np.mean(result[:, :, 1])
avg_b = np.mean(result[:, :, 2])
result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1)
result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1)
return cv2.cvtColor(result, cv2.COLOR_LAB2BGR)
def adjust_gamma(image, gamma=1.0):
"""Gamma校正"""
inv_gamma = 1.0 / gamma
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
def process_images(input_dir, output_dir,
auto_wb=True, gamma=1.0,
clahe_clip_limit=2.0):
"""
批量处理图像
"""
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff')):
img_path = os.path.join(input_dir, filename)
img = cv2.imread(img_path)
# 自动白平衡
if auto_wb:
img = auto_white_balance(img)
# Gamma校正
if gamma != 1.0:
img = adjust_gamma(img, gamma)
# CLAHE对比度增强
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=clahe_clip_limit, tileGridSize=(8,8))
l = clahe.apply(l)
lab = cv2.merge((l, a, b))
img = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
output_path = os.path.join(output_dir, filename)
cv2.imwrite(output_path, img)
print(f"已处理: {filename}")
# 使用示例
process_images(
input_dir="./raw_photos",
output_dir="./corrected_photos",
auto_wb=True,
gamma=1.2,
clahe_clip_limit=2.0
)
ImageMagick(命令行方案)
基本使用
# 批量调整亮度、对比度、饱和度
for img in *.jpg; do
convert "$img" -modulate 120,100,100 -brightness-contrast 10x5 "$img_corrected.jpg"
done
# 或使用mogrify直接处理
mogrify -path ./output -modulate 110,105,95 -brightness-contrast 5x3 *.jpg
更复杂的色彩校正
# 色彩平衡:增加红色,减少绿色和蓝色
for f in *.jpg; do
convert "$f" \
-channel R -gamma 1.2 \
-channel G -gamma 0.9 \
-channel B -gamma 0.8 \
-channel RGB -sigmoidal-contrast 20,50% \
"./output/$f"
done
Photoshop 动作(GUI方案)
-
创建动作:
- 打开一张图片
- 窗口 → 动作 → 新建动作
- 录制:图像 → 调整 → 色彩平衡/曲线/色阶
- 停止录制
-
批量处理:
- 文件 → 自动 → 批处理
- 选择源文件夹和目标文件夹
- 选择创建的动作
专业批量处理工具
XnConvert(免费)
<!-- 命令行调用 -->
xnconvert -r -i input_folder -o output_folder
-brightness 10
-contrast 15
-saturation -5
-f jpg -q 95
IrfanView(免费)
# 批量处理命令
i_view32.exe /filelist=images.txt /convert=c:\output\*.jpg /resize=(800,600)
/brightness=10 /contrast=10 /gamma_value=1.2 /saturation=5
视频色彩校正(适用于视频帧)
# 使用FFmpeg处理视频帧
import subprocess
def video_color_correction(input_video, output_video,
saturation=1.0, contrast=1.0,
brightness=0.0, gamma=1.0):
cmd = [
'ffmpeg',
'-i', input_video,
'-vf', f'eq=saturation={saturation}:contrast={contrast}:brightness={brightness}:gamma={gamma}',
'-c:a', 'copy',
output_video
]
subprocess.run(cmd)
各方案对比
| 方法 | 适用场景 | 精度 | 速度 | 易用性 |
|---|---|---|---|---|
| Python+Pillow | 简单调整 | 中等 | 中等 | |
| OpenCV | 专业校正 | 高 | 中等 | |
| ImageMagick | 批量处理 | 高 | 快速 | |
| Photoshop | 精确控制 | 最高 | 慢 |
建议:
- 简单调整:用 Pillow 或 ImageMagick
- 专业处理:用 OpenCV 或 Photoshop
- 大批量:用 ImageMagick 或 XnConvert