Skip to content

3.1.图像处理自动化

在日常办公中,我们经常需要处理大量图像,如产品照片、营销素材、文档扫描件等。手动处理这些图像不仅耗时,还容易出错。通过Python自动化图像处理,我们可以高效地完成批量缩放、裁剪、加水印、格式转换等任务,大大提高工作效率。

本文将介绍两个强大的Python图像处理库:Pillow(PIL的fork版本)和OpenCV,并通过实例展示如何使用它们实现各种图像处理自动化任务。

使用Pillow操作图像

Pillow是Python中最流行的图像处理库之一,它简单易用,功能强大,适合大多数日常图像处理任务。

安装Pillow

python
pip install Pillow

基本操作

打开和保存图像

python
from PIL import Image

# 打开图像
img = Image.open('example.jpg')

# 显示图像信息
print(f"图像格式: {img.format}")
print(f"图像大小: {img.size}")
print(f"图像模式: {img.mode}")

# 保存图像(可以指定不同格式)
img.save('example_copy.jpg')  # 保存为JPG
img.save('example.png')       # 保存为PNG
img.save('example.webp', quality=80)  # 保存为WebP,指定质量

图像缩放

PIL.Image.Image 类包含重新调整图像大小的 resize() 方法,参数为指定新尺寸的元组。

python
from PIL import Image

def resize_image(input_path, output_path, new_size):
    """调整图像大小
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        new_size: 新尺寸,格式为(宽, 高)
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 调整大小
        resized_img = img.resize(new_size)
        
        # 保存调整后的图像
        resized_img.save(output_path)
        
        print(f"已将图像从 {img.size} 调整为 {new_size}")
        return True
    except Exception as e:
        print(f"调整图像大小时出错: {e}")
        return False

# 使用示例
resize_image('example.jpg', 'example_resized.jpg', (800, 600))

等比例缩放

python
from PIL import Image

def resize_image_proportionally(input_path, output_path, max_size):
    """等比例调整图像大小,保持宽高比
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        max_size: 最大尺寸,格式为(最大宽度, 最大高度)
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 获取原始尺寸
        width, height = img.size
        
        # 计算缩放比例
        max_width, max_height = max_size
        scale = min(max_width / width, max_height / height)
        
        # 计算新尺寸
        new_width = int(width * scale)
        new_height = int(height * scale)
        
        # 调整大小
        resized_img = img.resize((new_width, new_height))
        
        # 保存调整后的图像
        resized_img.save(output_path)
        
        print(f"已将图像从 {img.size} 等比例调整为 {resized_img.size}")
        return True
    except Exception as e:
        print(f"调整图像大小时出错: {e}")
        return False

# 使用示例
resize_image_proportionally('example.jpg', 'example_resized_prop.jpg', (800, 600))

图像裁剪

python
from PIL import Image

def crop_image(input_path, output_path, crop_box):
    """裁剪图像
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        crop_box: 裁剪区域,格式为(左, 上, 右, 下)
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 裁剪图像
        cropped_img = img.crop(crop_box)
        
        # 保存裁剪后的图像
        cropped_img.save(output_path)
        
        print(f"已裁剪图像,裁剪区域: {crop_box}")
        return True
    except Exception as e:
        print(f"裁剪图像时出错: {e}")
        return False

# 使用示例
crop_image('example.jpg', 'example_cropped.jpg', (100, 100, 500, 400))

图像旋转

PIL.Image.Image 类包含旋转图像的 rotate() 方法,参数为逆时针旋转的角度。

python
from PIL import Image

def rotate_image(input_path, output_path, angle, expand=True):
    """旋转图像
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        angle: 旋转角度(逆时针)
        expand: 是否扩展画布以适应旋转后的图像
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 旋转图像
        rotated_img = img.rotate(angle, expand=expand)
        
        # 保存旋转后的图像
        rotated_img.save(output_path)
        
        print(f"已将图像旋转 {angle} 度")
        return True
    except Exception as e:
        print(f"旋转图像时出错: {e}")
        return False

# 使用示例
rotate_image('example.jpg', 'example_rotated.jpg', 90)  # 旋转90度

图像格式转换

Python Imaging Library允许你使用 convert() 方法在不同像素表示之间转换图像,同时可以通过保存时指定不同的扩展名来转换格式。

python
from PIL import Image
import os

def convert_image_format(input_path, output_format):
    """转换图像格式
    
    Args:
        input_path: 输入图像路径
        output_format: 输出格式(如'png', 'jpg', 'webp'等)
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 获取文件名(不含扩展名)
        filename = os.path.splitext(os.path.basename(input_path))[0]
        
        # 构建输出路径
        output_path = f"{filename}.{output_format.lower()}"
        
        # 保存为新格式
        img.save(output_path)
        
        print(f"已将图像从 {os.path.splitext(input_path)[1]} 转换为 {output_format}")
        return output_path
    except Exception as e:
        print(f"转换图像格式时出错: {e}")
        return None

# 使用示例
convert_image_format('example.jpg', 'png')  # 将JPG转换为PNG
convert_image_format('example.jpg', 'webp')  # 将JPG转换为WebP

添加水印

python
from PIL import Image, ImageDraw, ImageFont

def add_text_watermark(input_path, output_path, text, position=None, color=(255, 255, 255, 128), font_size=40):
    """添加文字水印
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        text: 水印文字
        position: 水印位置,格式为(x, y),默认为None(居中)
        color: 水印颜色,格式为(R, G, B, A),默认为半透明白色
        font_size: 字体大小
    """
    try:
        # 打开原始图像
        img = Image.open(input_path).convert("RGBA")
        
        # 创建透明图层
        txt = Image.new('RGBA', img.size, (255, 255, 255, 0))
        
        # 获取绘图对象
        draw = ImageDraw.Draw(txt)
        
        # 加载字体
        try:
            font = ImageFont.truetype("Arial.ttf", font_size)
        except IOError:
            font = ImageFont.load_default()
        
        # 计算文本大小
        text_width, text_height = draw.textsize(text, font)
        
        # 如果未指定位置,则居中放置
        if position is None:
            position = ((img.width - text_width) // 2, (img.height - text_height) // 2)
        
        # 绘制水印文字
        draw.text(position, text, font=font, fill=color)
        
        # 合并图层
        watermarked = Image.alpha_composite(img, txt)
        
        # 保存结果
        watermarked.convert('RGB').save(output_path)
        
        print(f"已添加文字水印: '{text}'")
        return True
    except Exception as e:
        print(f"添加水印时出错: {e}")
        return False

def add_image_watermark(input_path, output_path, watermark_path, position=None, opacity=0.3):
    """添加图像水印
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        watermark_path: 水印图像路径
        position: 水印位置,格式为(x, y),默认为None(右下角)
        opacity: 水印透明度,0-1之间
    """
    try:
        # 打开原始图像和水印
        base_image = Image.open(input_path).convert("RGBA")
        watermark = Image.open(watermark_path).convert("RGBA")
        
        # 调整水印大小(可选,这里设为原图的1/4宽度)
        watermark_width = base_image.width // 4
        watermark_height = int(watermark.height * watermark_width / watermark.width)
        watermark = watermark.resize((watermark_width, watermark_height))
        
        # 调整水印透明度
        watermark_with_opacity = Image.new('RGBA', watermark.size, (0, 0, 0, 0))
        for x in range(watermark.width):
            for y in range(watermark.height):
                r, g, b, a = watermark.getpixel((x, y))
                watermark_with_opacity.putpixel((x, y), (r, g, b, int(a * opacity)))
        
        # 如果未指定位置,则放在右下角
        if position is None:
            position = (base_image.width - watermark_width - 10, base_image.height - watermark_height - 10)
        
        # 创建透明图层
        transparent = Image.new('RGBA', base_image.size, (0, 0, 0, 0))
        transparent.paste(watermark_with_opacity, position)
        
        # 合并图层
        watermarked = Image.alpha_composite(base_image, transparent)
        
        # 保存结果
        watermarked.convert('RGB').save(output_path)
        
        print(f"已添加图像水印")
        return True
    except Exception as e:
        print(f"添加水印时出错: {e}")
        return False

# 使用示例
add_text_watermark('example.jpg', 'example_text_watermark.jpg', '© 2023 公司名称')
add_image_watermark('example.jpg', 'example_image_watermark.jpg', 'logo.png')

图像增强

Python Imaging Library提供了许多可用于增强图像的方法和模块。比如 ImageFilter 模块包含许多可以与 filter() 方法一起使用的预定义增强滤波器。

python
from PIL import Image, ImageEnhance, ImageFilter

def enhance_image(input_path, output_path, brightness=1.0, contrast=1.0, sharpness=1.0, color=1.0):
    """增强图像
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        brightness: 亮度因子,1.0为原始亮度
        contrast: 对比度因子,1.0为原始对比度
        sharpness: 锐度因子,1.0为原始锐度
        color: 色彩因子,1.0为原始色彩
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 调整亮度
        if brightness != 1.0:
            enhancer = ImageEnhance.Brightness(img)
            img = enhancer.enhance(brightness)
        
        # 调整对比度
        if contrast != 1.0:
            enhancer = ImageEnhance.Contrast(img)
            img = enhancer.enhance(contrast)
        
        # 调整锐度
        if sharpness != 1.0:
            enhancer = ImageEnhance.Sharpness(img)
            img = enhancer.enhance(sharpness)
        
        # 调整色彩
        if color != 1.0:
            enhancer = ImageEnhance.Color(img)
            img = enhancer.enhance(color)
        
        # 保存增强后的图像
        img.save(output_path)
        
        print(f"已增强图像")
        return True
    except Exception as e:
        print(f"增强图像时出错: {e}")
        return False

def apply_filter(input_path, output_path, filter_type):
    """应用滤镜
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        filter_type: 滤镜类型,如'BLUR', 'CONTOUR', 'SHARPEN'等
    """
    try:
        # 打开原始图像
        img = Image.open(input_path)
        
        # 应用滤镜
        if filter_type == 'BLUR':
            filtered_img = img.filter(ImageFilter.BLUR)
        elif filter_type == 'CONTOUR':
            filtered_img = img.filter(ImageFilter.CONTOUR)
        elif filter_type == 'SHARPEN':
            filtered_img = img.filter(ImageFilter.SHARPEN)
        elif filter_type == 'EMBOSS':
            filtered_img = img.filter(ImageFilter.EMBOSS)
        elif filter_type == 'EDGE_ENHANCE':
            filtered_img = img.filter(ImageFilter.EDGE_ENHANCE)
        elif filter_type == 'SMOOTH':
            filtered_img = img.filter(ImageFilter.SMOOTH)
        else:
            print(f"未知滤镜类型: {filter_type}")
            return False
        
        # 保存处理后的图像
        filtered_img.save(output_path)
        
        print(f"已应用 {filter_type} 滤镜")
        return True
    except Exception as e:
        print(f"应用滤镜时出错: {e}")
        return False

# 使用示例
enhance_image('example.jpg', 'example_enhanced.jpg', brightness=1.2, contrast=1.1, sharpness=1.3)
apply_filter('example.jpg', 'example_blur.jpg', 'BLUR')
apply_filter('example.jpg', 'example_sharpen.jpg', 'SHARPEN')

批量处理图像

python
import os
from PIL import Image

def batch_process_images(input_folder, output_folder, process_func, **kwargs):
    """批量处理图像
    
    Args:
        input_folder: 输入文件夹路径
        output_folder: 输出文件夹路径
        process_func: 处理函数
        **kwargs: 传递给处理函数的参数
    """
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 获取所有图像文件
    image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')
    image_files = [f for f in os.listdir(input_folder) 
                  if os.path.isfile(os.path.join(input_folder, f)) 
                  and f.lower().endswith(image_extensions)]
    
    # 处理每个图像
    success_count = 0
    for image_file in image_files:
        input_path = os.path.join(input_folder, image_file)
        output_path = os.path.join(output_folder, image_file)
        
        try:
            # 调用处理函数
            result = process_func(input_path, output_path, **kwargs)
            if result:
                success_count += 1
        except Exception as e:
            print(f"处理 {image_file} 时出错: {e}")
    
    print(f"批量处理完成,成功处理 {success_count}/{len(image_files)} 个文件")
    return success_count

# 定义一个简单的处理函数(调整大小)
def resize_for_batch(input_path, output_path, width, height):
    try:
        img = Image.open(input_path)
        img = img.resize((width, height))
        img.save(output_path)
        return True
    except Exception as e:
        print(f"处理 {input_path} 时出错: {e}")
        return False

# 使用示例
batch_process_images('input_images', 'output_images', resize_for_batch, width=800, height=600)

使用OpenCV处理图像

OpenCV是一个功能更强大的计算机视觉库,适合更复杂的图像处理任务,如图像识别、特征提取、目标检测等。

安装OpenCV

python
pip install opencv-python

基本操作

python
import cv2
import numpy as np

# 读取图像
img = cv2.imread('example.jpg')

# 显示图像信息
print(f"图像形状: {img.shape}")  # 返回(高度, 宽度, 通道数)
print(f"图像大小: {img.size}")    # 返回像素总数
print(f"图像类型: {img.dtype}")   # 返回数据类型

# 显示图像(仅在有GUI环境时有效)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存图像
cv2.imwrite('example_copy.jpg', img)

图像缩放

python
import cv2

def resize_image_cv2(input_path, output_path, new_size):
    """调整图像大小
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        new_size: 新尺寸,格式为(宽, 高)
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 调整大小
        resized_img = cv2.resize(img, new_size)
        
        # 保存调整后的图像
        cv2.imwrite(output_path, resized_img)
        
        print(f"已将图像从 {img.shape[1]}x{img.shape[0]} 调整为 {new_size[0]}x{new_size[1]}")
        return True
    except Exception as e:
        print(f"调整图像大小时出错: {e}")
        return False

# 使用示例
resize_image_cv2('example.jpg', 'example_resized_cv2.jpg', (800, 600))

图像裁剪

python
import cv2

def crop_image_cv2(input_path, output_path, crop_box):
    """裁剪图像
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        crop_box: 裁剪区域,格式为(y_start, y_end, x_start, x_end)
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 裁剪图像
        y_start, y_end, x_start, x_end = crop_box
        cropped_img = img[y_start:y_end, x_start:x_end]
        
        # 保存裁剪后的图像
        cv2.imwrite(output_path, cropped_img)
        
        print(f"已裁剪图像,裁剪区域: {crop_box}")
        return True
    except Exception as e:
        print(f"裁剪图像时出错: {e}")
        return False

# 使用示例
crop_image_cv2('example.jpg', 'example_cropped_cv2.jpg', (100, 400, 100, 500))

图像旋转

python
import cv2
import numpy as np

def rotate_image_cv2(input_path, output_path, angle):
    """旋转图像
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        angle: 旋转角度(逆时针)
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 获取图像中心点
        height, width = img.shape[:2]
        center = (width // 2, height // 2)
        
        # 创建旋转矩阵
        rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
        
        # 计算新图像的边界
        abs_cos = abs(rotation_matrix[0, 0])
        abs_sin = abs(rotation_matrix[0, 1])
        new_width = int(height * abs_sin + width * abs_cos)
        new_height = int(height * abs_cos + width * abs_sin)
        
        # 调整旋转矩阵
        rotation_matrix[0, 2] += new_width / 2 - center[0]
        rotation_matrix[1, 2] += new_height / 2 - center[1]
        
        # 执行旋转
        rotated_img = cv2.warpAffine(img, rotation_matrix, (new_width, new_height))
        
        # 保存旋转后的图像
        cv2.imwrite(output_path, rotated_img)
        
        print(f"已将图像旋转 {angle} 度")
        return True
    except Exception as e:
        print(f"旋转图像时出错: {e}")
        return False

# 使用示例
rotate_image_cv2('example.jpg', 'example_rotated_cv2.jpg', 45)  # 旋转45度

添加水印

python
import cv2
import numpy as np

def add_text_watermark_cv2(input_path, output_path, text, position=None, color=(255, 255, 255), thickness=2, font_scale=1.0):
    """添加文字水印
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        text: 水印文字
        position: 水印位置,格式为(x, y),默认为None(居中)
        color: 水印颜色,格式为(B, G, R),默认为白色
        thickness: 文字粗细
        font_scale: 字体大小缩放因子
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 获取文本大小
        font = cv2.FONT_HERSHEY_SIMPLEX
        text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]
        
        # 如果未指定位置,则居中放置
        if position is None:
            position = ((img.shape[1] - text_size[0]) // 2, (img.shape[0] + text_size[1]) // 2)
        
        # 添加文字水印
        cv2.putText(img, text, position, font, font_scale, color, thickness)
        
        # 保存结果
        cv2.imwrite(output_path, img)
        
        print(f"已添加文字水印: '{text}'")
        return True
    except Exception as e:
        print(f"添加水印时出错: {e}")
        return False

def add_image_watermark_cv2(input_path, output_path, watermark_path, position=None, alpha=0.3):
    """添加图像水印
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        watermark_path: 水印图像路径
        position: 水印位置,格式为(x, y),默认为None(右下角)
        alpha: 水印透明度,0-1之间
    """
    try:
        # 读取图像和水印
        img = cv2.imread(input_path)
        watermark = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
        
        # 如果水印有Alpha通道,则分离
        if watermark.shape[2] == 4:
            # 分离BGR和Alpha通道
            bgr = watermark[:, :, 0:3]
            alpha_channel = watermark[:, :, 3] / 255.0
            alpha_channel = cv2.merge([alpha_channel, alpha_channel, alpha_channel])
        else:
            bgr = watermark
            alpha_channel = np.ones(bgr.shape, dtype=bgr.dtype)
        
        # 调整水印大小(可选,这里设为原图的1/4宽度)
        watermark_width = img.shape[1] // 4
        watermark_height = int(watermark.shape[0] * watermark_width / watermark.shape[1])
        bgr = cv2.resize(bgr, (watermark_width, watermark_height))
        alpha_channel = cv2.resize(alpha_channel, (watermark_width, watermark_height))
        
        # 如果未指定位置,则放在右下角
        if position is None:
            position = (img.shape[1] - watermark_width - 10, img.shape[0] - watermark_height - 10)
        
        # 计算ROI
        x, y = position
        h, w = bgr.shape[0], bgr.shape[1]
        roi = img[y:y+h, x:x+w]
        
        # 应用水印
        result = (1 - alpha * alpha_channel) * roi + alpha * alpha_channel * bgr
        
        # 将结果放回原图
        img[y:y+h, x:x+w] = result
        
        # 保存结果
        cv2.imwrite(output_path, img)
        
        print(f"已添加图像水印")
        return True
    except Exception as e:
        print(f"添加水印时出错: {e}")
        return False

# 使用示例
add_text_watermark_cv2('example.jpg', 'example_text_watermark_cv2.jpg', '© 2023 公司名称')
add_image_watermark_cv2('example.jpg', 'example_image_watermark_cv2.jpg', 'logo.png')

图像增强和滤镜

python
import cv2
import numpy as np

def adjust_brightness_contrast(input_path, output_path, brightness=0, contrast=1.0):
    """调整亮度和对比度
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        brightness: 亮度调整值,正值增加亮度,负值降低亮度
        contrast: 对比度调整因子,大于1增加对比度,小于1降低对比度
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 应用公式:新像素 = 对比度 * 原像素 + 亮度
        adjusted = cv2.convertScaleAbs(img, alpha=contrast, beta=brightness)
        
        # 保存结果
        cv2.imwrite(output_path, adjusted)
        
        print(f"已调整亮度和对比度")
        return True
    except Exception as e:
        print(f"调整亮度和对比度时出错: {e}")
        return False

def apply_filter_cv2(input_path, output_path, filter_type, kernel_size=5):
    """应用滤镜
    
    Args:
        input_path: 输入图像路径
        output_path: 输出图像路径
        filter_type: 滤镜类型,如'blur', 'gaussian', 'median', 'bilateral'
        kernel_size: 核大小
    """
    try:
        # 读取图像
        img = cv2.imread(input_path)
        
        # 应用滤镜
        if filter_type == 'blur':
            filtered_img = cv2.blur(img, (kernel_size, kernel_size))
        elif filter_type == 'gaussian':
            filtered_img = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
        elif filter_type == 'median':
            filtered_img = cv2.medianBlur(img, kernel_size)
        elif filter_type == 'bilateral':
            filtered_img = cv2.bilateralFilter(img, kernel_size, 75, 75)
        else:
            print(f"未知滤镜类型: {filter_type}")
            return False
        
        # 保存结果
        cv2.imwrite(output_path, filtered_img)
        
        print(f"已应用 {filter_type} 滤镜")
        return True
    except Exception as e:
        print(f"应用滤镜时出错: {e}")
        return False

# 使用示例
adjust_brightness_contrast('example.jpg', 'example_adjusted_cv2.jpg', brightness=30, contrast=1.2)
apply_filter_cv2('example.jpg', 'example_gaussian_cv2.jpg', 'gaussian')
apply_filter_cv2('example.jpg', 'example_bilateral_cv2.jpg', 'bilateral')

实际应用场景

场景一:产品图片批量处理

python
import os
from PIL import Image, ImageEnhance

def process_product_images(input_folder, output_folder, target_size=(800, 800), enhance=True):
    """批量处理产品图片
    
    Args:
        input_folder: 输入文件夹路径
        output_folder: 输出文件夹路径
        target_size: 目标尺寸
        enhance: 是否增强图像
    """
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 获取所有图像文件
    image_extensions = ('.jpg', '.jpeg', '.png')
    image_files = [f for f in os.listdir(input_folder) 
                  if os.path.isfile(os.path.join(input_folder, f)) 
                  and f.lower().endswith(image_extensions)]
    
    print(f"找到 {len(image_files)} 个产品图片")
    
    # 处理每个图像
    for image_file in image_files:
        input_path = os.path.join(input_folder, image_file)
        output_path = os.path.join(output_folder, image_file)
        
        try:
            # 打开图像
            img = Image.open(input_path)
            
            # 创建白色背景
            background = Image.new('RGB', target_size, (255, 255, 255))
            
            # 调整图像大小,保持宽高比
            img_ratio = min(target_size[0] / img.width, target_size[1] / img.height)
            new_size = (int(img.width * img_ratio), int(img.height * img_ratio))
            img = img.resize(new_size, Image.LANCZOS)
            
            # 将图像居中放置在白色背景上
            offset = ((target_size[0] - new_size[0]) // 2, (target_size[1] - new_size[1]) // 2)
            background.paste(img, offset)
            
            # 增强图像
            if enhance:
                # 增加亮度
                enhancer = ImageEnhance.Brightness(background)
                background = enhancer.enhance(1.1)
                
                # 增加对比度
                enhancer = ImageEnhance.Contrast(background)
                background = enhancer.enhance(1.1)
                
                # 增加锐度
                enhancer = ImageEnhance.Sharpness(background)
                background = enhancer.enhance(1.2)
            
            # 保存处理后的图像
            background.save(output_path, quality=95)
            print(f"已处理: {image_file}")
            
        except Exception as e:
            print(f"处理 {image_file} 时出错: {e}")
    
    print("产品图片批量处理完成")

# 使用示例
# process_product_images('product_images', 'processed_products')

场景二:批量添加水印

python
import os
from PIL import Image, ImageDraw, ImageFont

def batch_add_watermark(input_folder, output_folder, watermark_text, position=None, font_size=30, opacity=0.5):
    """批量添加水印
    
    Args:
        input_folder: 输入文件夹路径
        output_folder: 输出文件夹路径
        watermark_text: 水印文字
        position: 水印位置,格式为(x, y),默认为None(右下角)
        font_size: 字体大小
        opacity: 水印透明度,0-1之间
    """
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 获取所有图像文件
    image_extensions = ('.jpg', '.jpeg', '.png')
    image_files = [f for f in os.listdir(input_folder) 
                  if os.path.isfile(os.path.join(input_folder, f)) 
                  and f.lower().endswith(image_extensions)]
    
    print(f"找到 {len(image_files)} 个图像文件")
    
    # 加载字体
    try:
        font = ImageFont.truetype("Arial.ttf", font_size)
    except IOError:
        font = ImageFont.load_default()
    
    # 处理每个图像
    for image_file in image_files:
        input_path = os.path.join(input_folder, image_file)
        output_path = os.path.join(output_folder, image_file)
        
        try:
            # 打开图像
            img = Image.open(input_path).convert("RGBA")
            
            # 创建透明图层
            txt = Image.new('RGBA', img.size, (255, 255, 255, 0))
            draw = ImageDraw.Draw(txt)
            
            # 计算文本大小
            text_width, text_height = draw.textsize(watermark_text, font)
            
            # 如果未指定位置,则放在右下角
            if position is None:
                position = (img.width - text_width - 20, img.height - text_height - 20)
            
            # 绘制水印文字
            draw.text(position, watermark_text, font=font, fill=(255, 255, 255, int(255 * opacity)))
            
            # 合并图层
            watermarked = Image.alpha_composite(img, txt)
            
            # 保存结果
            watermarked.convert('RGB').save(output_path)
            print(f"已处理: {image_file}")
            
        except Exception as e:
            print(f"处理 {image_file} 时出错: {e}")
    
    print("批量添加水印完成")

# 使用示例
# batch_add_watermark('photos', 'watermarked_photos', '© 2023 公司名称')

场景三:图像格式批量转换

python
import os
from PIL import Image

def batch_convert_format(input_folder, output_folder, target_format='webp', quality=85):
    """批量转换图像格式
    
    Args:
        input_folder: 输入文件夹路径
        output_folder: 输出文件夹路径
        target_format: 目标格式,如'jpg', 'png', 'webp'等
        quality: 图像质量,1-100之间(仅对jpg和webp有效)
    """
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 获取所有图像文件
    image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp')
    image_files = [f for f in os.listdir(input_folder) 
                  if os.path.isfile(os.path.join(input_folder, f)) 
                  and f.lower().endswith(image_extensions)]
    
    print(f"找到 {len(image_files)} 个图像文件")
    
    # 处理每个图像
    for image_file in image_files:
        input_path = os.path.join(input_folder, image_file)
        
        # 获取文件名(不含扩展名)
        filename = os.path.splitext(image_file)[0]
        output_path = os.path.join(output_folder, f"{filename}.{target_format.lower()}")
        
        try:
            # 打开图像
            img = Image.open(input_path)
            
            # 如果图像有透明通道且目标格式是JPG,则添加白色背景
            if img.mode == 'RGBA' and target_format.lower() == 'jpg':
                background = Image.new('RGB', img.size, (255, 255, 255))
                background.paste(img, mask=img.split()[3])  # 使用alpha通道作为mask
                img = background
            
            # 保存为目标格式
            if target_format.lower() in ('jpg', 'jpeg'):
                img.convert('RGB').save(output_path, 'JPEG', quality=quality)
            elif target_format.lower() == 'webp':
                img.save(output_path, 'WEBP', quality=quality)
            else:
                img.save(output_path, target_format.upper())
            
            print(f"已转换: {image_file} -> {os.path.basename(output_path)}")
            
        except Exception as e:
            print(f"转换 {image_file} 时出错: {e}")
    
    print("批量格式转换完成")

# 使用示例
# batch_convert_format('original_images', 'webp_images', 'webp', 85)

通过以上代码示例和应用场景,你可以轻松实现各种图像处理自动化任务,大大提高工作效率。无论是批量处理产品图片、添加水印,还是转换图像格式,Python都能帮你轻松应对。