14.Python使用Pillow教程

1.打算开始学习PIL,在命令行输入:pip3 install PIL,报错信息如下所示,后百度了下,发现:PIL仅支持到Python2.7,后来一群志愿者在此基础上创建了兼容性版本,为Pillow,因此可以直接安装使用Pillow。

2.在命令行输入:pip3 install Pillow,运行一段时间后报错:Read timed out.后输入命令:pip --default-timeout=100 install -U Pillow成功安装

3.使用Image类

 

from PIL import Image

im = Image.open('test.jpg')   #返回Image对象

print(im)
print(im.size)     #二元tuple,包含width和height(单位都是px)
print(im.format)   #属性标识了图像来源
print(im.mode)     #定义图像的数量、名称、像素的类型和深度,L==灰度图像,RGB==真彩色图像,CMYK==出版图像 

 

4.创建缩略图

 

from PIL import Image

im = Image.open('test.jpg')   #返回Image对象

size = (100,100)    #缩小的比例(width和height)
im.thumbnail(size,Image.ANTIALIAS)   #缩略图
im.save('thumbnail.jpg')   #保存

 

  

 

posted @ 2018-08-30 17:47  廖丹  阅读(2343)  评论(1编辑  收藏  举报