Python编程图形库之Pillow使用方法讲解
Python  /  管理员 发布于 7年前   370
PIL vs Pillow
PIL: Python Imaging Library,是python的图像处理库。由于PIL不兼容setuptools,再加上更新缓慢等因素,Alex Clark等一些社区好心人还是希望能够继续支持PIL,所以fork了PIL,这就是Pillow的缘起。
Pillow的目标
推动和促进PIL的发展是Pillow的目标,主要通过如下的方式来进行
其实可以看出,所做的改善就是在CI和CD,改善用户感知,定期/快速地与使用者进行沟通和交流,是pillow获得好感的一个重要因素。
安装
安装可以通过pip,只需要执行pip install pillow即可
liumiaocn:~ liumiao$ pip install pillowCollecting 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: pillowSuccessfully installed pillow-5.1.0liumiaocn:~ liumiao$
安装确认
liumiaocn:~ liumiao$ pip show pillowName: PillowVersion: 5.1.0Summary: Python Imaging Library (Fork)Home-page: https://python-pillow.orgAuthor: Alex Clark (Fork Author)Author-email: [email protected]: Standard PIL LicenseLocation: /usr/local/lib/python2.7/site-packagesRequires: Required-by: liumiaocn:~ liumiao$
使用
图形库有很多实用的功能,这里列举几个进行简单演示。
ImageGrab.grab()
使用这个方法可以实现抓屏:
liumiaocn:tmp liumiao$ cat grab.python #!/usr/local/bin/pythonfrom PIL import ImageGrab#get current screen copyimage = ImageGrab.grab()#display image sizeprint("Current screen shot size :",image.size)#display image modeprint("Screen shot picture mode :", image.mode)#save picture to /tmp/screen-grab-1.bmpimage.save('/tmp/screen-grab-1.bmp')#show pictureimage.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.bmpliumiaocn:tmp liumiao$
滤镜
PIL中的ImageFilter支持近十种滤镜, 比如对刚刚抓取的图片使用CONTOUR滤镜
liumiaocn:tmp liumiao$ cat filter-contour.py #!/usr/local/bin/pythonfrom PIL import ImageFilter, Imagesrc_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$
执行之后可以得到如下图片
旋转
使用rotate即可对图片进行旋转操作:
liumiaocn:tmp liumiao$ cat rotate.py #!/usr/local/bin/pythonfrom PIL import Imagesrc_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$
执行之后,即可确认
Pillow功能非常之多,而且使用也很方便,比如resize对尺寸进行调节,还可以添加文字等等常见的图形处理操作,这里就不再一一介绍,具体的需要可以参看如下链接进行了解:https://pypi.org/project/Pillow/
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家的支持。如果你想了解更多相关内容请查看下面相关链接
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号