尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

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

Python图像处理入门:Pillow库核心功能与应用 1. Python图像处理基础与Pillow库简介在数字时代图像处理已成为程序员必备技能之一。Python作为最受欢迎的编程语言通过Pillow库为开发者提供了强大的图像处理能力。Pillow是Python Imaging LibraryPIL的一个友好分支它继承了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提供了丰富的图像变换功能调整大小new_img img.resize((800, 600))旋转图像rotated_img img.rotate(45) # 旋转45度裁剪图像box (100, 100, 400, 400) # 左,上,右,下 cropped_img img.crop(box)颜色转换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_size40): # 创建空白图像 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), fontfont) # 添加干扰线 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)), width2) return img captcha generate_captcha(PYTHON) captcha.save(captcha.png)5. 性能优化与最佳实践5.1 处理大型图像处理大图像时内存消耗可能成为问题。可以采用以下策略使用thumbnail方法而不是resize它会保持宽高比img.thumbnail((800, 800))分块处理大图像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(itile_size, img.width), min(jtile_size, img.height)) region img.crop(box) # 处理每个区域5.2 图像格式选择不同格式有不同特点JPEG适合照片有损压缩PNG适合图形无损压缩支持透明度WebP现代格式压缩率优于JPEG和PNG选择格式时考虑# 高质量JPEG img.save(output.jpg, quality95) # 优化PNG img.save(output.png, optimizeTrue)5.3 多线程处理对于批量图像处理可以使用多线程加速from concurrent.futures import ThreadPoolExecutor def process_image(filepath): img Image.open(filepath) # 处理图像 return img with ThreadPoolExecutor(max_workers4) as executor: results list(executor.map(process_image, image_files))6. 常见问题与解决方案6.1 图像打开失败可能原因及解决方法文件路径错误 - 检查路径是否正确文件损坏 - 尝试其他图像查看器打开格式不支持 - 检查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(radius3)) # 颜色减淡混合 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_allTrue, append_imagesframes[1:], duration100, loop0)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((x1, y1), text, (0, 0, 0), fontfont) draw.text((x, y), text, (255, 255, 255), fontfont) img.save(output_path) add_watermark(photo.jpg, watermarked.jpg, © Your Name)
返回列表