脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python编程图形库之Pillow使用方法讲解

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

2021-05-09 00:28liumiaocn Python

今天小编就为大家分享一篇关于Python编程图形库之Pillow使用方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

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即可

?
1
2
3
4
5
6
7
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$

安装确认

?
1
2
3
4
5
6
7
8
9
10
11
12
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()

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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下所生成的文件

?
1
2
3
4
5
6
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滤镜

?
1
2
3
4
5
6
7
8
9
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即可对图片进行旋转操作:

?
1
2
3
4
5
6
7
8
9
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

延伸 · 阅读

精彩推荐