亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 图像处理库 Pillow 完全指南

Python 图像处理库 Pillow 完全指南

Pillow(Python Imaging Library 续作)专为图像处理设计,典型用于归档与批处理,当然也可随心所欲用于其他场景。功能包括:

目录

安装图像处理包

安装 Pillow(原 PIL 分支延续版):

pip3 install Pillow

处理图像

导入 ImageFilter 解锁多种滤镜。模糊示例:

from PIL import Image, ImageFilter
im = Image.open("kittens.jpg")
blurred = im.filter(ImageFilter.BLUR)

其他滤镜含 SHARPEN、SMOOTH、EDGE_ENHANCE。完整列表见 ImageFilter 参考文档。

旋转 180 度:

rotated_image = im.rotate(180)

保存成果:

rotated_image.save("rotated.jpg")

显示图像

除处理外,库也可屏显图像。显示 kittens.jpg 示例:

from PIL import Image
im = Image.open("kittens.jpg")
im.show()
print(im.format, im.size, im.mode)
# JPEG (1920, 1357) RGB

下动图演示在 IPython 中直接用 Pillow:

Python 图像处理库 Pillow 完全指南

延伸阅读

库功能远不止此。想全面掌握 Python 图像处理,请移步官方教程!

Python

  1. 树莓派与Python构建机器人:完整指南
  2. Python print() 函数完全指南:基础用法、空行打印与自定义结束符
  3. Python strip() 方法完全指南:去除首尾空白与自定义字符实战
  4. Python str.count() 完全指南:统计字符与子串出现次数实战
  5. Python format() 字符串格式化完全指南:占位符、格式化规范、类与字典应用
  6. Python 字符串 find() 方法完全指南:语法、参数、替代方案与计数技巧
  7. 精通 Python Lambda 函数:实用示例与最佳实践
  8. Python round() 函数详解与实战示例
  9. 精通 Python map() 函数:语法、示例与最佳实践
  10. Python timeit():精准测量代码执行时间与实战示例
  11. Python collections.Counter:高效计数、更新与算术运算
  12. 理解 Python type() 与 isinstance():实战示例详解