图像处理是对数字图像进行分析、修改、增强、转换等操作的技术,是计算机视觉和人工智能的基础。
理解图像的内部表示对于有效的图像处理至关重要:
# 图像的基本属性
from PIL import Image
import numpy as np
# 打开图像
img = Image.open('example.jpg')
# 基本属性
print(f"图像尺寸: {img.size}") # (宽度, 高度)
print(f"图像模式: {img.mode}") # RGB, RGBA, L(灰度), CMYK等
print(f"图像格式: {img.format}") # JPEG, PNG, BMP等
print(f"图像信息: {img.info}") # 元数据信息
# 转换为numpy数组进行分析
img_array = np.array(img)
print(f"数组形状: {img_array.shape}") # (高度, 宽度, 通道数)
print(f"数据类型: {img_array.dtype}") # uint8 (0-255)
print(f"数值范围: {img_array.min()} - {img_array.max()}")
Pillow是Python图像处理的标准库,是PIL(Python Imaging Library)的现代化分支。
pip install pillow
from PIL import Image, ImageFilter, ImageEnhance
掌握这些基础操作是图像处理的第一步:
Image.open(filename)
:打开图像文件img.save(filename, format, quality)
:保存图像,可指定格式和质量img.size
:获取图像尺寸 (宽度, 高度)img.mode
:获取图像颜色模式img.format
:获取图像文件格式# 基础操作完整示例
from PIL import Image
import os
# 注意:JPEG格式不支持透明度,RGBA图像需要转换为RGB模式
def basic_image_operations(image_path):
"""演示基础图像操作"""
# 1. 打开图像
if not os.path.exists(image_path):
print(f"图像文件 {image_path} 不存在")
return
img = Image.open(image_path)
print(f"原始图像: {img.size}, 模式: {img.mode}, 格式: {img.format}")
# 如果图像是RGBA模式,打印提示信息
if img.mode == 'RGBA':
print("检测到RGBA图像,将自动转换为RGB模式以保存为JPEG格式")
# 2. 创建输出目录
output_dir = "processed_images"
os.makedirs(output_dir, exist_ok=True)
# 3. 基础变换操作
# 缩放 - 保持宽高比
width, height = img.size
scale_factor = 0.5
new_size = (int(width * scale_factor), int(height * scale_factor))
img_resized = img.resize(new_size, Image.Resampling.LANCZOS)
# 裁剪 - 中心裁剪
crop_size = min(width, height) // 2
left = (width - crop_size) // 2
top = (height - crop_size) // 2
right = left + crop_size
bottom = top + crop_size
img_cropped = img.crop((left, top, right, bottom))
# 旋转 - 45度
img_rotated = img.rotate(45, expand=True, fillcolor='white')
# 4. 保存结果
# 转换为RGB模式再保存为JPEG(JPEG不支持透明度)
img_resized_rgb = img_resized.convert('RGB')
img_cropped_rgb = img_cropped.convert('RGB')
img_rotated_rgb = img_rotated.convert('RGB')
img_resized_rgb.save(os.path.join(output_dir, "resized.jpg"), quality=95)
img_cropped_rgb.save(os.path.join(output_dir, "cropped.jpg"), quality=95)
img_rotated_rgb.save(os.path.join(output_dir, "rotated.jpg"), quality=95)
print("基础操作完成!")
return img_resized, img_cropped, img_rotated
# 使用示例
# basic_image_operations('cat.jpg')
除了基础操作,Pillow还提供了许多高级变换功能:
# 高级变换操作
from PIL import Image, ImageOps, ImageEnhance
def advanced_transformations(img):
"""演示高级图像变换"""
# 1. 镜像翻转
img_flip_h = ImageOps.mirror(img) # 水平翻转
img_flip_v = ImageOps.flip(img) # 垂直翻转
# 2. 图像增强
enhancer = ImageEnhance.Contrast(img)
img_contrast = enhancer.enhance(1.5) # 增加对比度
enhancer = ImageEnhance.Brightness(img)
img_bright = enhancer.enhance(1.3) # 增加亮度
enhancer = ImageEnhance.Color(img)
img_saturated = enhancer.enhance(1.4) # 增加饱和度
# 3. 图像滤镜
img_blur = img.filter(ImageFilter.BLUR)
img_sharpen = img.filter(ImageFilter.SHARPEN)
img_edge = img.filter(ImageFilter.FIND_EDGES)
return {
'flip_h': img_flip_h,
'flip_v': img_flip_v,
'contrast': img_contrast,
'bright': img_bright,
'saturated': img_saturated,
'blur': img_blur,
'sharpen': img_sharpen,
'edge': img_edge
}
理解像素操作是图像处理的核心,让我们深入了解像素级别的操作:
img.getpixel((x, y))
:获取指定坐标的像素值img.putpixel((x, y), color)
:设置指定坐标的像素值img.load()
:获取像素访问对象,提高访问效率img.convert(mode)
:转换图像颜色模式
# 像素操作完整示例
from PIL import Image
import numpy as np
def pixel_operations_demo(image_path):
"""演示像素级操作"""
# 打开图像
img = Image.open(image_path)
print(f"原始图像: {img.size}, 模式: {img.mode}")
# 1. 基本像素读取
width, height = img.size
center_x, center_y = width // 2, height // 2
# 获取中心像素颜色
center_color = img.getpixel((center_x, center_y))
print(f"中心像素颜色: {center_color}")
# 2. 像素修改 - 创建红色十字
img_copy = img.copy()
pixels = img_copy.load()
# 水平红线
for x in range(width):
pixels[x, center_y] = (255, 0, 0)
# 垂直红线
for y in range(height):
pixels[center_x, y] = (255, 0, 0)
# 3. 批量像素操作 - 创建渐变效果
img_gradient = img.copy()
pixels_grad = img_gradient.load()
for y in range(height):
for x in range(width):
# 创建从左上到右下的渐变
intensity = int(255 * (x + y) / (width + height))
pixels_grad[x, y] = (intensity, intensity, intensity)
# 4. 保存结果
img_copy.save('cross.jpg')
img_gradient.save('gradient.jpg')
print("像素操作完成!")
return img_copy, img_gradient
# 使用示例
# pixel_operations_demo('cat.jpg')
不同的颜色模式适用于不同的应用场景:
# 颜色模式转换
from PIL import Image
def color_mode_conversions(img):
"""演示各种颜色模式转换"""
# 1. RGB转灰度
img_gray = img.convert('L')
# 2. RGB转RGBA (添加透明通道)
img_rgba = img.convert('RGBA')
# 3. RGB转CMYK (印刷色彩模式)
img_cmyk = img.convert('CMYK')
# 4. RGB转HSV (色调、饱和度、明度)
img_hsv = img.convert('HSV')
# 5. 自定义颜色映射
def create_custom_map():
"""创建自定义颜色映射"""
custom_map = []
for i in range(256):
# 创建蓝绿色调
custom_map.extend([0, i, i])
return custom_map
img_custom = img.convert('P', palette=Image.ADAPTIVE, colors=256)
return {
'gray': img_gray,
'rgba': img_rgba,
'cmyk': img_cmyk,
'hsv': img_hsv,
'custom': img_custom
}
理解颜色空间对于高级图像处理至关重要:
# 颜色通道操作
import numpy as np
from PIL import Image
def channel_operations(img):
"""演示颜色通道操作"""
# 转换为numpy数组
img_array = np.array(img)
# 1. 分离RGB通道
red_channel = img_array[:, :, 0]
green_channel = img_array[:, :, 1]
blue_channel = img_array[:, :, 2]
# 2. 创建单通道图像
red_img = Image.fromarray(red_channel, mode='L')
green_img = Image.fromarray(green_channel, mode='L')
blue_img = Image.fromarray(blue_channel, mode='L')
# 3. 通道混合 - 创建伪彩色效果
# 交换红色和蓝色通道
img_swapped = img_array.copy()
img_swapped[:, :, 0] = blue_channel
img_swapped[:, :, 2] = red_channel
img_swapped = Image.fromarray(img_swapped)
# 4. 通道增强
img_enhanced = img_array.copy()
img_enhanced[:, :, 1] = np.clip(img_enhanced[:, :, 1] * 1.5, 0, 255) # 增强绿色
img_enhanced = Image.fromarray(img_enhanced.astype(np.uint8))
return {
'red': red_img,
'green': green_img,
'blue': blue_img,
'swapped': img_swapped,
'enhanced': img_enhanced
}
图像增强是改善图像视觉效果的重要技术:
# 图像增强与滤镜
from PIL import Image, ImageFilter, ImageEnhance, ImageOps
def image_enhancement_demo(img):
"""演示各种图像增强技术"""
# 1. 亮度、对比度、饱和度调整
enhancers = {
'brightness': ImageEnhance.Brightness(img),
'contrast': ImageEnhance.Contrast(img),
'color': ImageEnhance.Color(img),
'sharpness': ImageEnhance.Sharpness(img)
}
# 应用不同的增强参数
enhanced_images = {}
for name, enhancer in enhancers.items():
if name == 'brightness':
enhanced_images[name] = enhancer.enhance(1.3) # 增加30%亮度
elif name == 'contrast':
enhanced_images[name] = enhancer.enhance(1.5) # 增加50%对比度
elif name == 'color':
enhanced_images[name] = enhancer.enhance(1.2) # 增加20%饱和度
elif name == 'sharpness':
enhanced_images[name] = enhancer.enhance(2.0) # 增加100%锐度
# 2. 滤镜效果
filter_effects = {
'blur': img.filter(ImageFilter.BLUR),
'sharpen': img.filter(ImageFilter.SHARPEN),
'edge_enhance': img.filter(ImageFilter.EDGE_ENHANCE),
'emboss': img.filter(ImageFilter.EMBOSS),
'find_edges': img.filter(ImageFilter.FIND_EDGES),
'smooth': img.filter(ImageFilter.SMOOTH),
'median': img.filter(ImageFilter.MedianFilter(size=3))
}
# 3. 特殊效果
# 创建素描效果
img_gray = img.convert('L')
img_inverted = ImageOps.invert(img_gray)
img_blurred = img_inverted.filter(ImageFilter.GaussianBlur(radius=2))
img_sketch = ImageOps.invert(img_blurred)
# 创建老照片效果
img_sepia = img.copy()
img_sepia = img_sepia.convert('RGB')
pixels = img_sepia.load()
width, height = img_sepia.size
for y in range(height):
for x in range(width):
r, g, b = pixels[x, y]
# 应用棕褐色调
tr = int(0.393*r + 0.769*g + 0.189*b)
tg = int(0.349*r + 0.686*g + 0.168*b)
tb = int(0.272*r + 0.534*g + 0.131*b)
pixels[x, y] = (min(tr, 255), min(tg, 255), min(tb, 255))
return {
'enhanced': enhanced_images,
'filters': filter_effects,
'sketch': img_sketch,
'sepia': img_sepia
}
在实际应用中,我们经常需要处理大量图像:
# 批量图像处理
import os
from PIL import Image
from pathlib import Path
class BatchImageProcessor:
"""批量图像处理器"""
def __init__(self, input_dir, output_dir):
self.input_dir = Path(input_dir)
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True)
# 支持的图像格式
self.supported_formats = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
def get_image_files(self):
"""获取所有图像文件"""
image_files = []
for file_path in self.input_dir.rglob('*'):
if file_path.suffix.lower() in self.supported_formats:
image_files.append(file_path)
return image_files
def resize_all_images(self, target_size, keep_aspect=True):
"""批量调整图像尺寸"""
image_files = self.get_image_files()
for file_path in image_files:
try:
with Image.open(file_path) as img:
if keep_aspect:
# 保持宽高比
img.thumbnail(target_size, Image.Resampling.LANCZOS)
else:
# 强制调整尺寸
img = img.resize(target_size, Image.Resampling.LANCZOS)
# 生成输出文件名
output_path = self.output_dir / f"resized_{file_path.name}"
img.save(output_path, quality=95)
print(f"处理完成: {file_path.name}")
except Exception as e:
print(f"处理 {file_path.name} 时出错: {e}")
def convert_all_to_format(self, target_format, quality=95):
"""批量转换图像格式"""
image_files = self.get_image_files()
for file_path in image_files:
try:
with Image.open(file_path) as img:
# 转换为RGB模式(如果需要)
if img.mode in ('RGBA', 'LA', 'P'):
img = img.convert('RGB')
# 生成输出文件名
output_name = file_path.stem + f'.{target_format.lower()}'
output_path = self.output_dir / output_name
# 保存为指定格式
if target_format.lower() == 'jpg':
img.save(output_path, 'JPEG', quality=quality)
else:
img.save(output_path, target_format.upper())
print(f"转换完成: {file_path.name} -> {output_name}")
except Exception as e:
print(f"转换 {file_path.name} 时出错: {e}")
def apply_filter_to_all(self, filter_func, **kwargs):
"""批量应用自定义滤镜"""
image_files = self.get_image_files()
for file_path in image_files:
try:
with Image.open(file_path) as img:
# 应用滤镜
processed_img = filter_func(img, **kwargs)
# 生成输出文件名
output_path = self.output_dir / f"filtered_{file_path.name}"
processed_img.save(output_path, quality=95)
print(f"滤镜应用完成: {file_path.name}")
except Exception as e:
print(f"应用滤镜到 {file_path.name} 时出错: {e}")
# 使用示例
def example_batch_processing():
"""批量处理示例"""
# 创建处理器
processor = BatchImageProcessor('input_images', 'output_images')
# 批量调整尺寸
processor.resize_all_images((800, 600), keep_aspect=True)
# 批量转换为JPEG
processor.convert_all_to_format('jpg', quality=90)
# 批量应用黑白滤镜
def black_white_filter(img):
return img.convert('L')
processor.apply_filter_to_all(black_white_filter)
print("批量处理完成!")
# 运行示例
# example_batch_processing()
图像分割是计算机视觉中的核心技术:
# 图像分割示例
import numpy as np
from PIL import Image, ImageDraw
def simple_image_segmentation(img, threshold=128):
"""简单的基于阈值的图像分割"""
# 转换为灰度图
img_gray = img.convert('L')
img_array = np.array(img_gray)
# 创建二值掩码
mask = img_array > threshold
# 创建分割结果
segmented = np.zeros_like(img_array)
segmented[mask] = 255
# 转换为PIL图像
segmented_img = Image.fromarray(segmented, mode='L')
return segmented_img, mask
def region_growing_segmentation(img, seed_point, threshold=30):
"""区域生长分割算法"""
img_gray = img.convert('L')
img_array = np.array(img_gray)
height, width = img_array.shape
# 初始化
visited = np.zeros((height, width), dtype=bool)
segmented = np.zeros((height, width), dtype=bool)
# 种子点
seed_x, seed_y = seed_point
seed_value = img_array[seed_y, seed_x]
# 待处理的像素队列
queue = [(seed_x, seed_y)]
segmented[seed_y, seed_x] = True
visited[seed_y, seed_x] = True
# 8邻域
neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
while queue:
x, y = queue.pop(0)
for dx, dy in neighbors:
nx, ny = x + dx, y + dy
# 检查边界
if 0 <= nx < width and 0 <= ny < height and not visited[ny, nx]:
visited[ny, nx] = True
# 检查相似性
if abs(img_array[ny, nx] - seed_value) <= threshold:
segmented[ny, nx] = True
queue.append((nx, ny))
# 创建结果图像
result = np.zeros((height, width), dtype=np.uint8)
result[segmented] = 255
return Image.fromarray(result, mode='L')
图像压缩对于网络传输和存储优化至关重要:
# 图像压缩与优化
from PIL import Image
import os
class ImageOptimizer:
"""图像优化器"""
def __init__(self):
self.quality_presets = {
'web_low': {'quality': 60, 'optimize': True},
'web_medium': {'quality': 80, 'optimize': True},
'web_high': {'quality': 90, 'optimize': True},
'print': {'quality': 95, 'optimize': False},
'archive': {'quality': 100, 'optimize': False}
}
def optimize_jpeg(self, img, preset='web_medium', progressive=True):
"""优化JPEG图像"""
settings = self.quality_presets[preset].copy()
# 保存为JPEG
output = io.BytesIO()
img.save(output, format='JPEG',
quality=settings['quality'],
optimize=settings['optimize'],
progressive=progressive)
return output.getvalue()
def create_webp(self, img, quality=80, lossless=False):
"""创建WebP格式图像"""
output = io.BytesIO()
img.save(output, format='WebP',
quality=quality,
lossless=lossless)
return output.getvalue()
def progressive_jpeg(self, img, quality=85):
"""创建渐进式JPEG"""
return self.optimize_jpeg(img, preset='web_high', progressive=True)
def compare_formats(self, img, filename_base):
"""比较不同格式的压缩效果"""
results = {}
# JPEG
jpeg_data = self.optimize_jpeg(img, 'web_medium')
results['JPEG'] = len(jpeg_data)
# WebP
webp_data = self.create_webp(img, quality=80)
results['WebP'] = len(webp_data)
# PNG (无损)
png_output = io.BytesIO()
img.save(png_output, format='PNG', optimize=True)
results['PNG'] = len(png_output.getvalue())
# 计算压缩率
original_size = len(img.tobytes())
for format_name, size in results.items():
compression_ratio = (1 - size / original_size) * 100
print(f"{format_name}: {size} bytes, 压缩率: {compression_ratio:.1f}%")
return results
创建一个功能完整的照片编辑器:
# 照片编辑器
import tkinter as tk
from tkinter import filedialog, ttk
from PIL import Image, ImageTk, ImageEnhance, ImageFilter
import os
class PhotoEditor:
"""简单的照片编辑器"""
def __init__(self, root):
self.root = root
self.root.title("照片编辑器")
self.root.geometry("800x600")
self.original_image = None
self.current_image = None
self.photo = None
self.setup_ui()
def setup_ui(self):
"""设置用户界面"""
# 菜单栏
menubar = tk.Menu(self.root)
self.root.config(menu=menubar)
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="文件", menu=file_menu)
file_menu.add_command(label="打开", command=self.open_image)
file_menu.add_command(label="保存", command=self.save_image)
file_menu.add_separator()
file_menu.add_command(label="退出", command=self.root.quit)
# 工具栏
toolbar = ttk.Frame(self.root)
toolbar.pack(fill=tk.X, padx=5, pady=5)
ttk.Button(toolbar, text="重置", command=self.reset_image).pack(side=tk.LEFT, padx=2)
ttk.Button(toolbar, text="黑白", command=self.convert_grayscale).pack(side=tk.LEFT, padx=2)
ttk.Button(toolbar, text="模糊", command=self.apply_blur).pack(side=tk.LEFT, padx=2)
ttk.Button(toolbar, text="锐化", command=self.apply_sharpen).pack(side=tk.LEFT, padx=2)
# 调整控件
adjust_frame = ttk.LabelFrame(self.root, text="图像调整")
adjust_frame.pack(fill=tk.X, padx=5, pady=5)
# 亮度调整
ttk.Label(adjust_frame, text="亮度:").grid(row=0, column=0, padx=5, pady=2)
self.brightness_var = tk.DoubleVar(value=1.0)
brightness_scale = ttk.Scale(adjust_frame, from_=0.1, to=3.0,
variable=self.brightness_var, orient=tk.HORIZONTAL,
command=self.adjust_brightness)
brightness_scale.grid(row=0, column=1, sticky=(tk.W, tk.E), padx=5, pady=2)
# 对比度调整
ttk.Label(adjust_frame, text="对比度:").grid(row=1, column=0, padx=5, pady=2)
self.contrast_var = tk.DoubleVar(value=1.0)
contrast_scale = ttk.Scale(adjust_frame, from_=0.1, to=3.0,
variable=self.contrast_var, orient=tk.HORIZONTAL,
command=self.adjust_contrast)
contrast_scale.grid(row=1, column=1, sticky=(tk.W, tk.E), padx=5, pady=2)
# 图像显示区域
self.canvas = tk.Canvas(self.root, bg='white')
self.canvas.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# 状态栏
self.status_var = tk.StringVar()
self.status_var.set("就绪")
status_bar = ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN)
status_bar.pack(fill=tk.X, side=tk.BOTTOM)
def open_image(self):
"""打开图像文件"""
file_path = filedialog.askopenfilename(
filetypes=[("图像文件", "*.jpg *.jpeg *.png *.bmp *.gif *.tiff")]
)
if file_path:
try:
self.original_image = Image.open(file_path)
self.current_image = self.original_image.copy()
self.display_image()
self.status_var.set(f"已打开: {os.path.basename(file_path)}")
except Exception as e:
self.status_var.set(f"打开文件失败: {e}")
def display_image(self):
"""显示图像"""
if self.current_image:
# 调整图像大小以适应画布
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
if canvas_width > 1 and canvas_height > 1:
# 计算缩放比例
img_width, img_height = self.current_image.size
scale_x = canvas_width / img_width
scale_y = canvas_height / img_height
scale = min(scale_x, scale_y, 1.0) # 不放大图像
new_width = int(img_width * scale)
new_height = int(img_height * scale)
# 缩放图像
display_img = self.current_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
self.photo = ImageTk.PhotoImage(display_img)
# 清除画布并显示图像
self.canvas.delete("all")
x = (canvas_width - new_width) // 2
y = (canvas_height - new_height) // 2
self.canvas.create_image(x, y, anchor=tk.NW, image=self.photo)
def reset_image(self):
"""重置图像到原始状态"""
if self.original_image:
self.current_image = self.original_image.copy()
self.display_image()
self.brightness_var.set(1.0)
self.contrast_var.set(1.0)
def convert_grayscale(self):
"""转换为灰度图"""
if self.current_image:
self.current_image = self.current_image.convert('L')
self.display_image()
def apply_blur(self):
"""应用模糊滤镜"""
if self.current_image:
self.current_image = self.current_image.filter(ImageFilter.BLUR)
self.display_image()
def apply_sharpen(self):
"""应用锐化滤镜"""
if self.current_image:
self.current_image = self.current_image.filter(ImageFilter.SHARPEN)
self.display_image()
def adjust_brightness(self, value):
"""调整亮度"""
if self.original_image:
enhancer = ImageEnhance.Brightness(self.original_image)
self.current_image = enhancer.enhance(float(value))
self.display_image()
def adjust_contrast(self, value):
"""调整对比度"""
if self.original_image:
enhancer = ImageEnhance.Contrast(self.original_image)
self.current_image = enhancer.enhance(float(value))
self.display_image()
def save_image(self):
"""保存图像"""
if self.current_image:
file_path = filedialog.asksaveasfilename(
defaultextension=".png",
filetypes=[("PNG文件", "*.png"), ("JPEG文件", "*.jpg"), ("所有文件", "*.*")]
)
if file_path:
try:
self.current_image.save(file_path)
self.status_var.set(f"已保存: {os.path.basename(file_path)}")
except Exception as e:
self.status_var.set(f"保存失败: {e}")
# 运行编辑器
def run_photo_editor():
"""运行照片编辑器"""
root = tk.Tk()
app = PhotoEditor(root)
root.mainloop()
# 使用示例
# run_photo_editor()
处理大图像时,性能优化至关重要:
# 性能优化技巧
import time
import psutil
from PIL import Image
import numpy as np
def memory_efficient_processing(image_path):
"""内存高效的图像处理"""
# 1. 使用生成器处理大图像
def process_in_tiles(img, tile_size=512):
"""分块处理大图像"""
width, height = img.size
for y in range(0, height, tile_size):
for x in range(0, width, tile_size):
# 计算当前块的边界
right = min(x + tile_size, width)
bottom = min(y + tile_size, height)
# 裁剪当前块
tile = img.crop((x, y, right, bottom))
# 处理当前块
processed_tile = process_tile(tile)
# 返回处理结果和位置
yield (x, y, processed_tile)
def process_tile(tile):
"""处理单个图像块"""
# 这里可以应用任何图像处理算法
return tile.filter(ImageFilter.EDGE_ENHANCE)
# 2. 使用上下文管理器
with Image.open(image_path) as img:
# 获取内存使用情况
process = psutil.Process()
initial_memory = process.memory_info().rss / 1024 / 1024 # MB
print(f"初始内存使用: {initial_memory:.2f} MB")
# 分块处理
start_time = time.time()
# 创建新图像用于存储结果
result_img = Image.new(img.mode, img.size)
for x, y, processed_tile in process_in_tiles(img):
result_img.paste(processed_tile, (x, y))
processing_time = time.time() - start_time
final_memory = process.memory_info().rss / 1024 / 1024 # MB
print(f"处理时间: {processing_time:.2f} 秒")
print(f"最终内存使用: {final_memory:.2f} MB")
print(f"内存增长: {final_memory - initial_memory:.2f} MB")
return result_img
def benchmark_operations():
"""性能基准测试"""
# 创建测试图像
test_size = (1024, 1024)
test_image = Image.new('RGB', test_size, color='white')
operations = {
'resize': lambda img: img.resize((512, 512)),
'blur': lambda img: img.filter(ImageFilter.BLUR),
'sharpen': lambda img: img.filter(ImageFilter.SHARPEN),
'grayscale': lambda img: img.convert('L'),
'rotate': lambda img: img.rotate(45),
'crop': lambda img: img.crop((100, 100, 900, 900))
}
results = {}
for name, operation in operations.items():
# 预热
for _ in range(3):
operation(test_image.copy())
# 实际测试
start_time = time.time()
for _ in range(10):
operation(test_image.copy())
end_time = time.time()
avg_time = (end_time - start_time) / 10 * 1000 # 毫秒
results[name] = avg_time
# 输出结果
print("性能基准测试结果 (毫秒/操作):")
for name, time_ms in sorted(results.items(), key=lambda x: x[1]):
print(f"{name:12}: {time_ms:8.2f} ms")
return results
选择一个你感兴趣的图像处理应用场景,设计并实现一个完整的解决方案。项目应该包含:
提交截止日期:课程结束后一周内