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$
滤镜
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$
执行之后可以得到如下图片
旋转
使用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$
执行之后,即可确认
Pillow功能非常之多,而且使用也很方便,比如resize对尺寸进行调节,还可以添加文字等等常见的图形处理操作,这里就不再一一介绍,具体的需要可以参看如下链接进行了解:https://pypi.org/project/Pillow/
来源:https://blog.csdn.net/liumiaocn/article/details/80780342


猜你喜欢
- 如下所示:a = int(input("请输入菱形行数:"))m = a #空格d = a #倒三角for i in r
- 前言通常在读写文件之前,我们需要先判断文件或者目录是否存在。不然在接下来的处理中可能会报错。所以在做任何操作之前,最好还是先判断文件、目录是
- 一、auth模块# 创建超级用户python manage.py createsuperuserfrom django.contrib im
- 自从接触python以后就想着爬pixiv,之前因为梯子有点问题就一直搁置,最近换了个梯子就迫不及待试了下。爬虫无非request获取htm
- 1:在终端下:mysql -V。 以下是代码片段:[shengting@login ~]$ mysql -Vmysql Ver 14.7 D
- 1.游标方式 代码如下:DECLARE @Data NVARCHAR(max) SET @Data='1,tanw,2,
- 如何制作一个倒计时的程序? 见下:<%CountdownDate = #1/1
- 安装配置MongoDB驱动安装驱动go get -u github.com/mongodb/mongo-go-driver初始化模块go m
- 这里使用FSO对象来删除指定文件夹 代码和说明如下:<% set fs=createobject(&qu
- 一、系统资源使用限制的必要性探讨对于一个脚本,最基础的限制是要限制单进程实例以保证了不会存在多个进程实例、在运行程序主体逻辑前检测系统资源剩
- 目录一、使用说明二、代码分析1. 功能函数2. 回调函数3. 线程生成函数4. 效果展示写在最后在科研学习的过程中,我们难免需要查询相关的文
- 如下所示:L = ['adam', 'Lisa', 'bart', 'Paul
- 首先,创建一个存储过程 get_clob:t_name:要查询的表名;f_name:要查询的字段名;u_id:表的主键,查询条件;l_pos
- 1,CSS,JS,IMG一个都不能少运行代码框<style type="text/css">&l
- 本文实例讲述了Python实现两个list求交集,并集,差集的方法。分享给大家供大家参考,具体如下:在python中,数组可以用list来表
- asp代码 <% Dim Rs,Conn Set Conn=Server.CreateObject("Adodb.Conne
- 对于不可变类型传递值(不会影响原数据) 不可变类型对于可变类型传递引用(会影响原数据) 不可变类型传递引用python3
- 让我们看看如何在 Python 中复制数组。 有 3 种复制数组的方法:只需使用赋值运算符。浅拷贝深拷贝1 使用赋值运算符我们可以使用赋值运
- 弹出窗口,加载页面。弹出窗口初始位置为居中。可在关闭窗口时,回调主页面按钮。要求jquery。效果演示首先,演示主窗口两个按钮作用。然后,演
- pip安装的话,找到自己安装python的路径,在安装路径下会有一个文件夹,比如我的安装路径是c盘我是默认安装路径,在里面有一个python