Python编程图形库之Pillow使用方法讲解

作者:liumiaocn 时间:2022-04-07 06:10:57 

PIL vs Pillow

PIL: Python Imaging Library,是python的图像处理库。由于PIL不兼容setuptools,再加上更新缓慢等因素,Alex Clark等一些社区好心人还是希望能够继续支持PIL,所以fork了PIL,这就是Pillow的缘起。

Pillow的目标

推动和促进PIL的发展是Pillow的目标,主要通过如下的方式来进行

  • 结合Travis CI和AppVeyor进行持续集成测试

  • 活用github进行开发

  • 结合Python Package Index进行例行发布

其实可以看出,所做的改善就是在CI和CD,改善用户感知,定期/快速地与使用者进行沟通和交流,是pillow获得好感的一个重要因素。

安装

安装可以通过pip,只需要执行pip install pillow即可


liumiaocn:~ liumiao$ pip install pillow
Collecting pillow
Downloading https://files.pythonhosted.org/packages/df/aa/a25f211a4686f363d8ca5a1752c43a8f42459e70af13e20713d3e636f0af/Pillow-5.1.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB)
 100% |████████████████████████████████| 3.6MB 157kB/s
Installing collected packages: pillow
Successfully installed pillow-5.1.0
liumiaocn:~ liumiao$

安装确认


liumiaocn:~ liumiao$ pip show pillow
Name: Pillow
Version: 5.1.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (Fork Author)
Author-email: aclark@aclark.net
License: Standard PIL License
Location: /usr/local/lib/python2.7/site-packages
Requires:
Required-by:
liumiaocn:~ liumiao$

使用

图形库有很多实用的功能,这里列举几个进行简单演示。

ImageGrab.grab()

使用这个方法可以实现抓屏:


liumiaocn:tmp liumiao$ cat grab.python
#!/usr/local/bin/python
from PIL import ImageGrab
#get current screen copy
image = ImageGrab.grab()
#display image size
print("Current screen shot size :",image.size)
#display image mode
print("Screen shot picture mode :", image.mode)
#save picture to /tmp/screen-grab-1.bmp
image.save('/tmp/screen-grab-1.bmp')
#show picture
image.show()
liumiaocn:tmp liumiao$

因为代码中使用了image.show()进行了显示,执行之后可以直接看到显示,同时也能确认到/tmp下所生成的文件


liumiaocn:tmp liumiao$ python grab.python
('Current screen shot size :', (2880, 1800))
('Screen shot picture mode :', 'RGBA')
liumiaocn:tmp liumiao$ ls -l /tmp/screen-grab-1.bmp
-rw-r--r-- 1 liumiao wheel 20736054 Jun 23 05:41 /tmp/screen-grab-1.bmp
liumiaocn:tmp liumiao$

Python编程图形库之Pillow使用方法讲解

滤镜

PIL中的ImageFilter支持近十种滤镜, 比如对刚刚抓取的图片使用CONTOUR滤镜


liumiaocn:tmp liumiao$ cat filter-contour.py
#!/usr/local/bin/python
from PIL import ImageFilter, Image
src_image = Image.open('/tmp/screen-grab-1.bmp')
print("begin to filter the pic")
dst_image = src_image.filter(ImageFilter.CONTOUR)
print("picture through filter")
dst_image.show()
liumiaocn:tmp liumiao$

执行之后可以得到如下图片

Python编程图形库之Pillow使用方法讲解

旋转

使用rotate即可对图片进行旋转操作:


liumiaocn:tmp liumiao$ cat rotate.py
#!/usr/local/bin/python
from PIL import Image
src_image = Image.open('/tmp/screen-grab-1.bmp')
print("begin to rotate the pic")
dst_image = src_image.rotate(90)
print("picture after rotating")
dst_image.show()
liumiaocn:tmp liumiao$

执行之后,即可确认

Python编程图形库之Pillow使用方法讲解

Pillow功能非常之多,而且使用也很方便,比如resize对尺寸进行调节,还可以添加文字等等常见的图形处理操作,这里就不再一一介绍,具体的需要可以参看如下链接进行了解:https://pypi.org/project/Pillow/

来源:https://blog.csdn.net/liumiaocn/article/details/80780342

标签:python,pillow,图形库
0
投稿

猜你喜欢

  • 一个不错的js软键盘代码而且移植方便

    2007-08-14 12:56:00
  • 类似google的ASP分页代码[测试通过]

    2009-03-13 12:43:00
  • Python连接Redis的基本配置方法

    2023-09-29 21:28:27
  • 用Dreamweaver设计自动关闭的网页

    2010-09-02 12:29:00
  • python SMTP实现发送带附件电子邮件

    2023-09-29 11:31:54
  • Python开根号的几种方式详解

    2021-07-10 20:37:34
  • 由浅到深了解JavaScript类

    2008-06-16 13:20:00
  • 在ie6下的hover伪类的使用

    2008-06-01 13:51:00
  • 最简洁的SQL多重查询的解决方案

    2005-09-12 16:31:00
  • 网页栅格系统研究:蛋糕的切法

    2008-10-24 17:07:00
  • pycharm 实现光标快速移动到括号外或行尾的操作

    2023-07-17 19:52:31
  • 谈谈网页一屏有多大?

    2007-12-21 12:28:00
  • Python实现判断并移除列表指定位置元素的方法

    2023-03-21 03:06:19
  • Python连接SQLServer2000的方法详解

    2023-07-23 09:02:12
  • python+flask编写一个简单的登录接口

    2021-06-09 21:19:02
  • 网页制作 JSP与ASP 的比较

    2005-08-10 16:00:00
  • IE中雅黑字体给布局带来的变化

    2008-06-13 11:22:00
  • Python基于socket实现TCP客户端和服务端

    2023-09-29 15:30:05
  • SQL server不支持utf8 php却用utf8的矛盾问题解决方法

    2023-07-20 12:01:28
  • 用js更好地截取定长字符串

    2008-01-16 12:48:00
  • asp之家 网络编程 m.aspxhome.com