Python图片处理模块Pillow

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/showhilllee/article/details/53883574

原文来自Pillow

安装

警告

* Pillow 不能和PIL 同时存在于一个环境中,在安装Pillow之前需要先卸载PIL
* Pillow 1.0 版本后已经不支持import Image,请使用from PIL import Image来代替
* Pillow 2.1.0版本以后已经不支持import _imaging,请使用from PIL.Image import core as _imaging来代替

提示

* Pillow 2.0.0 版本之前只支持Python 2.4、2.5、2.6、2.7
* Pillow 2.0.0 版本之前只支持Python 2.6、2.7、3.2、3.3、3.4、3.5

基本安装

提示:使用PyPI安装可以工作在Windows、OS X和Linux中,使用源码包需要组建依赖环境

使用pip安装Pillow:

$ pip install Pillow

或者使用easy_install

$ easy_install Pillow

Windows安装

官方提供了wheel,egg和二进制程序32位和64位平台的Pillow二进制文件,这些二进制文件拥有所有的第三方库

$ pip install Pillow

或者:

$ easy_install Pillow

OS X 安装

官方提供了OS X 系统中所有所支持的Python版本的wheel格式的二进制程序,并且已经支持所有可选的库

$ pip install Pillow

Linux 安装

官方在Linux中不提供任何额二进制包,不过这里还是可以使用pip或者easy_install来安装毕竟我们有Python,当然我们也可以通过源码包来安装

这里不对源码包安装进行翻译,如果想要使用那么请点击此链接进行跳转

平台支持

操作系统 是否支持 Python版本 Pillow 最终测试版本 操作系统类型
Mac OS X 10.11 El Capitan Yes 2.7,3.3,3.4,3.5 3.0.0 X86-64
Mac OS X 10.10 Yosemite Yes 2.7,3.3,3.4 3.0.0 X86-64
Mac OS X 10.9 Mavericks Yes 2.7,3.2,3.3,3.4 3.0.0 x86-64
Mac OS X 10.8 Mountain Lion Yes 2.6,2.7,3.2,3.3 x86-64
Redhat Linux 6 Yes 2.6 x86
CentOS 6.3 Yes 2.7,3.3 x86
Fedora 20 Yes 2.7,3.3 2.3.0 x86-64
Ubuntu Linux 10.04 LTS Yes 2.6 2.3.0 x86,x86-64
Ubuntu Linux 12.04 LTS Yes 2.6,2.7,3.2,3.3,3.4,3.5PyPy2.4,PyPy3,v2.32.7,3.2 3.0.0,2.6.1 x86,x86-64,ppc
Ubuntu Linux 14.04 LTS Yes 2.7,3.4 3.0.0 x86-64
Debian 8.2 Jessie Yes 2.7,3.4 3.0.0 x86-64
Raspian Wheezy Yes 2.7,3.2 2.3.0 arm
Gentoo Linux Yes 2.7,3.2,2.1.0 x86-64
FreeBSD 10 Yes 2.7,3.4,2.4.0 x86-64
Windows 7 Pro Yes 2.7,3.2,3.3,2.2.1 x86-64
Windows Server 2008 R2 Enterprise Yes 3.3 x86-64
Windows Server 2012 R2 Yes 2.7,3.3,3.4 3.0.0 x86-64
Windows 8 Pro Yes 2.6,2.7,3.2,3.3,3.4a3,2.2.0 x86,x86-64
Windows 8.1 Pro Yes 2.6,2.7,3.2,3.3,3.4 2.4.0 x86,x86-64

手册

教程

在Python的图像库中使用最多的是Image类,从文件中加载图像可使用Image模块中的open()函数:

使用图像类
# 导入模块
>>>> from PIL import Image
# 以只读模式查看图片
>>> im = Image.open('C:/users/yxn/desktop/1.jpg','r')
# 获取图片属性
>>> print(im)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3396x2000 at 0x23E89E71978>

# 查看图片大小
>>> print("图片宽{0}px,高{1}px".format(im.size[0],im.size[1]))
图片宽3396px,高2000px

# 获取图片源格式
>>> print(im.format)
JPEG

# 获取图片模式,常见有:L(灰度图像),RGB和CMYK(真彩图像)
>>> print(im.mode)

# 只显示加载的图像,show()这个函数会将图片保存到一个临时文件并调用XV程序来显示图像
>>> print(im.show())
读取和写入图像

读取并保存图像
#!/usr/bin/env python3
'''
转换文件格式,将jpg转换为png
'''
import os
from PIL import Image
ImageFile = 'C:/User/xxx/Desktop/1.jpg'
# 分割文件路径和后缀名
FilePath,Fileext = os.path.splitext(ImageFile)
# 设置保存后的文件格式
outImageFile = "{0}.png".format(FilePath)
# 打开并保存
Image.open(ImageFile).save(outImageFile)
创建图片的缩略图
#!/usr/bin/env python3
'''
裁剪图片
'''
import os
from PIL import Image
size = 128, 128
ImageFile = 'C:/Users/yxn/Desktop/1.jpg'
# 分割文件路径和后缀名
FilePath,Fileext = os.path.splitext(ImageFile)
# 设置保存后的文件格式
outImageFile = "{0}.ico".format(FilePath)
# 打开图片
im = Image.open(ImageFile)
# 设置图片裁剪大小
im.thumbnail(size)
# 保存图片模式为JPEG
im.save(outImageFile,'JPEG')
剪切、粘贴

从图片获取一个矩形

该区域由一个元祖组成,这个元祖中包含4个值分别是(左、上、右、下的坐标)那么复制的内容实际 宽度=右坐标-左坐标,高度的计算方式:下坐标-上坐标=高度

蜗牛小注:这里的四个值为想要进行操作的图片矩形的四条边的坐标,即分别为左边距(要操作的矩形左侧距离整个图片最左边的距离)、上边距(要操作的矩形上侧距离整个图片最上边的距离)、右边距(要操作的矩形右侧距离整个图片最左边的距离)、下边距(要操作的矩形下侧距离整个图片最上边的距离)。与移动端开发里,四个值的含义是不同的,移动端的四个值含义分别为x(即左边距)y(上边距),要操作的宽,要操作的高。所以如果在Pillow里坐标值写错了的话会报错ValueError: images do not match。这时候就要注意你的右边距是不是比左边距小了或者下边距比上边距小了。如图:
这里写图片描述

#!/usr/bin/env python3
'''
裁剪图片
'''
import os
from PIL import Image

size = 128, 128
ImageFile = 'C:/Users/xxx/Desktop/2.jpg'
# 分割文件路径和后缀名
FilePath, Fileext = os.path.splitext(ImageFile)
# 设置保存后的文件格式
outImageFile = "C:/Users/xxx/Desktop/3.jpg"
# 打开图片
im = Image.open(ImageFile)
# 设置图片复制的4个值
box = 200,0, 1024,768
# 使用crop进行复制
region = im.crop(box)
'''
使用transpose翻转图像
可选值有Image.Rotate90,Image.ROTATE_180,Image.Rotate270,Image.FLIP_LEFT_RIGHT,Image.FLIP_TOP_BOTTOM
分别代表翻转90°,180°270°,左右翻转,上下翻转
'''
region = region.transpose(Image.ROTATE_180)
# 粘贴图片,此处的值不能修改否则会报错
im.paste(region, box)
# 保存修改后的图片
im.save(outImageFile, 'JPEG')

练习

制作微信+1头像

#!/usr/bin/env python3
'''
仿作微信+1头像
'''

from PIL import Image,ImageFont,ImageDraw

ImageFile = ''
SaveFile = ''

def AddNumToImg(Imagefile,SaveFile):
    # 打开Imagefile,将其模式转换为RGBA
    with Image.open(Imagefile).convert('RGBA') as im:
        # 创建一个新图片,大小和模式直接使用Imagefile的
        txt = Image.new(im.mode,im.size)
        # 设置字体和字号
        font = ImageFont.truetype('msyh.ttc',66)
        # 编辑txt新图片
        d = ImageDraw.Draw(txt)
        # 画一个圆,并且设置为红色
        d.ellipse((575,50,675,150),('red'))
        # 增加一个数字,位置要处于上面的圆的中间,内容为1,字体为微软雅黑,填充颜色为白色,最后的fill里面的值可以为色值和颜色名称
        d.text((605,55),'1',font=font,fill=(255,255,255))

        # 合并图片
        out = Image.alpha_composite(im,txt)
        # 保存图片
        out.save(SaveFile)
        # 展示保存后的图片
        out.show()


AddNumToImg(ImageFile,SaveFile)

注:画圆和增加文字的时候第一个参数是位置参数,根据图片大小来输入合适的位置

生成图片4位随机验证码

#!/usr/bin/env python3
'''
4位验证码
'''
import random

from PIL import Image,ImageFont,ImageDraw

def rndtxt():
    txt_list = []
    # 大写字母
    txt_list.extend([i for i in range(65,90)])
    # 小写字母
    txt_list.extend([i for i in range(97,123)])
    # 数字
    txt_list.extend([i for i in range(48,57)])
    return chr(txt_list[random.randint(0,len(txt_list)-1)])


def rndbgcolor():
    # 背景颜色
    return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

def rndtxtcolor2():
    # 字体颜色
    return (random.randint(32,127),random.randint(32,127),random.randint(32,127))

def code():
    weight = 240
    hight = 60
    image = Image.new('RGB',(weight,hight),(255,255,255))
    font = ImageFont.truetype('msyh.ttc',36)
    draw = ImageDraw.Draw(image)

    # 填充背景颜色
    for x in range(weight):
        for y in range(hight):
            draw.point((x,y),fill=rndbgcolor())
    # 生成随机验证码
    for t in range(4):
        draw.text((60 * t + 10,10),rndtxt(),font=font,fill=rndtxtcolor2())

    image.show()

code()

写在结尾的话

验证码模块参考廖雪峰的官方网站

此篇文章并没有过多的介绍Pillow模块使用,只是介绍了简单用法如果觉得对此模块感兴趣请直接跳转至官方文档 进行查阅.

原文地址:https://yxnt.github.io/2016/05/15/Pillow-Python3.5/

猜你喜欢

转载自blog.csdn.net/showhilllee/article/details/53883574
今日推荐