三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

Python图像处理入门:Pillow库核心功能与应用

Python图像处理入门:Pillow库核心功能与应用

1. Python图像处理基础与Pillow库简介

在数字时代,图像处理已成为程序员必备技能之一。Python作为最受欢迎的编程语言,通过Pillow库为开发者提供了强大的图像处理能力。Pillow是Python Imaging Library(PIL)的一个友好分支,它继承了PIL的所有功能并进行了现代化改进。

安装Pillow非常简单,只需一条pip命令:

pip install pillow

这个库支持多种图像格式,包括JPEG、PNG、GIF、BMP等,能够完成从基本操作(如裁剪、旋转)到高级处理(如滤镜应用、色彩空间转换)的各种任务。特别值得一提的是,Pillow对Python 3.x系列有很好的支持,解决了原PIL库在Python 3环境下的兼容性问题。

2. Pillow核心功能解析

2.1 图像基本操作

使用Pillow打开和保存图像非常简单:

from PIL import Image # 打开图像 img = Image.open('example.jpg') # 显示图像 img.show() # 保存图像 img.save('output.png')

图像的基本属性可以通过以下方式获取:

print(img.format) # 图像格式 print(img.size) # 图像尺寸(宽度,高度) print(img.mode) # 图像模式(RGB, L等)

2.2 图像变换与处理

Pillow提供了丰富的图像变换功能:

  1. 调整大小:
new_img = img.resize((800, 600))
  1. 旋转图像:
rotated_img = img.rotate(45) # 旋转45度
  1. 裁剪图像:
box = (100, 100, 400, 400) # 左,上,右,下 cropped_img = img.crop(box)
  1. 颜色转换:
gray_img = img.convert('L') # 转换为灰度图像

3. 高级图像处理技术

3.1 滤镜应用

Pillow内置了多种图像滤镜:

from PIL import ImageFilter # 模糊效果 blurred = img.filter(ImageFilter.BLUR) # 轮廓提取 contour = img.filter(ImageFilter.CONTOUR) # 细节增强 detail = img.filter(ImageFilter.DETAIL)

3.2 像素级操作

对于需要精细控制的情况,可以直接操作像素:

pixels = img.load() for i in range(img.size[0]): for j in range(img.size[1]): r, g, b = pixels[i, j] # 对每个像素进行处理 pixels[i, j] = (r, g, b)

3.3 图像合成

Pillow可以轻松实现图像合成:

from PIL import Image base_img = Image.open('background.jpg') overlay_img = Image.open('logo.png') # 将logo粘贴到背景上,位置为(100,100) base_img.paste(overlay_img, (100, 100), overlay_img) base_img.save('result.jpg')

4. 实际应用案例

4.1 批量图像处理

Pillow非常适合批量处理图像文件:

import os from PIL import Image input_folder = 'input_images' output_folder = 'processed_images' if not os.path.exists(output_folder): os.makedirs(output_folder) for filename in os.listdir(input_folder): if filename.endswith(('.jpg', '.png')): img_path = os.path.join(input_folder, filename) img = Image.open(img_path) # 执行处理操作,例如调整大小 img = img.resize((800, 600)) output_path = os.path.join(output_folder, filename) img.save(output_path)

4.2 生成验证码图片

利用Pillow可以轻松生成简单的验证码:

from PIL import Image, ImageDraw, ImageFont import random def generate_captcha(text, font_size=40): # 创建空白图像 img = Image.new('RGB', (200, 80), color=(255, 255, 255)) # 获取绘图对象 draw = ImageDraw.Draw(img) # 加载字体 try: font = ImageFont.truetype('arial.ttf', font_size) except: font = ImageFont.load_default() # 绘制文本 draw.text((10, 10), text, fill=(0, 0, 0), font=font) # 添加干扰线 for i in range(5): x1 = random.randint(0, 200) y1 = random.randint(0, 80) x2 = random.randint(0, 200) y2 = random.randint(0, 80) draw.line((x1, y1, x2, y2), fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), width=2) return img captcha = generate_captcha("PYTHON") captcha.save('captcha.png')

5. 性能优化与最佳实践

5.1 处理大型图像

处理大图像时,内存消耗可能成为问题。可以采用以下策略:

  1. 使用thumbnail方法而不是resize,它会保持宽高比:
img.thumbnail((800, 800))
  1. 分块处理大图像:
tile_size = 512 for i in range(0, img.width, tile_size): for j in range(0, img.height, tile_size): box = (i, j, min(i+tile_size, img.width), min(j+tile_size, img.height)) region = img.crop(box) # 处理每个区域

5.2 图像格式选择

不同格式有不同特点:

  • JPEG:适合照片,有损压缩
  • PNG:适合图形,无损压缩,支持透明度
  • WebP:现代格式,压缩率优于JPEG和PNG

选择格式时考虑:

# 高质量JPEG img.save('output.jpg', quality=95) # 优化PNG img.save('output.png', optimize=True)

5.3 多线程处理

对于批量图像处理,可以使用多线程加速:

from concurrent.futures import ThreadPoolExecutor def process_image(filepath): img = Image.open(filepath) # 处理图像 return img with ThreadPoolExecutor(max_workers=4) as executor: results = list(executor.map(process_image, image_files))

6. 常见问题与解决方案

6.1 图像打开失败

可能原因及解决方法:

  1. 文件路径错误 - 检查路径是否正确
  2. 文件损坏 - 尝试其他图像查看器打开
  3. 格式不支持 - 检查Pillow支持的格式

6.2 内存不足

处理大图像时可能出现内存不足:

  • 使用分块处理技术
  • 降低图像分辨率
  • 增加系统交换空间

6.3 颜色失真

颜色问题通常由色彩空间转换引起:

  • 检查原始图像模式(img.mode)
  • 在转换前保留原始色彩空间
  • 使用convert()时指定正确的模式

7. Pillow与其他库的集成

7.1 与NumPy结合

Pillow图像可以转换为NumPy数组:

import numpy as np from PIL import Image img = Image.open('example.jpg') img_array = np.array(img) # 对数组进行处理后转回图像 processed_img = Image.fromarray(img_array)

7.2 与OpenCV结合

Pillow与OpenCV图像格式转换:

import cv2 from PIL import Image import numpy as np # Pillow转OpenCV pil_img = Image.open('example.jpg') cv_img = np.array(pil_img) cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR) # OpenCV转Pillow cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) pil_img = Image.fromarray(cv_img)

7.3 与Matplotlib结合

在Jupyter notebook中显示图像:

from PIL import Image import matplotlib.pyplot as plt img = Image.open('example.jpg') plt.imshow(img) plt.axis('off') plt.show()

8. 扩展应用与创意项目

8.1 生成艺术效果

创建素描效果:

from PIL import Image, ImageFilter, ImageOps def sketch_effect(img_path): img = Image.open(img_path) # 转换为灰度 img_gray = img.convert('L') # 反色 img_invert = ImageOps.invert(img_gray) # 高斯模糊 img_blur = img_invert.filter(ImageFilter.GaussianBlur(radius=3)) # 颜色减淡混合 final = Image.blend(img_gray, img_blur, 0.5) return final sketch = sketch_effect('portrait.jpg') sketch.save('sketch.jpg')

8.2 创建GIF动画

使用Pillow制作简单动画:

from PIL import Image # 准备帧图像 frames = [] for i in range(10): frame = Image.new('RGB', (200, 200), color=(i*25, i*25, i*25)) frames.append(frame) # 保存为GIF frames[0].save('animation.gif', save_all=True, append_images=frames[1:], duration=100, loop=0)

8.3 图像水印添加

批量添加水印:

from PIL import Image, ImageDraw, ImageFont def add_watermark(input_path, output_path, text): img = Image.open(input_path) # 创建绘图对象 draw = ImageDraw.Draw(img) # 使用合适字体 try: font = ImageFont.truetype('arial.ttf', 36) except: font = ImageFont.load_default() # 计算文本位置(右下角) text_width, text_height = draw.textsize(text, font) x = img.width - text_width - 10 y = img.height - text_height - 10 # 绘制文本(带阴影效果) draw.text((x+1, y+1), text, (0, 0, 0), font=font) draw.text((x, y), text, (255, 255, 255), font=font) img.save(output_path) add_watermark('photo.jpg', 'watermarked.jpg', '© Your Name')
← 返回列表