安装警告
- * 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 CapitanYes2.7,3.3,3.4,3.53.0.0X86-64 Mac OS X 10.10 YosemiteYes2.7,3.3,3.43.0.0X86-64 Mac OS X 10.9 MavericksYes2.7,3.2,3.3,3.43.0.0x86-64 Mac OS X 10.8 Mountain LionYes2.6,2.7,3.2,3.3x86-64 Redhat Linux 6Yes2.6x86 CentOS 6.3Yes2.7,3.3x86 Fedora 20Yes2.7,3.3 2.3.0x86-64 Ubuntu Linux 10.04 LTSYes2.6 2.3.0x86,x86-64 Ubuntu Linux 12.04 LTSYes2.6,2.7,3.2,3.3,3.4,3.5PyPy2.4,PyPy3,v2.32.7,3.23.0.0,2.6.1x86,x86-64,ppc Ubuntu Linux 14.04 LTSYes2.7,3.4 3.0.0x86-64 Debian 8.2 JessieYes2.7,3.4 3.0.0x86-64 Raspian WheezyYes2.7,3.2 2.3.0arm Gentoo LinuxYes2.7,3.2,2.1.0x86-64 FreeBSD 10Yes2.7,3.4,2.4.0x86-64 Windows 7 ProYes2.7,3.2,3.3,2.2.1x86-64 Windows Server 2008 R2 EnterpriseYes3.3x86-64 Windows Server 2012 R2Yes2.7,3.3,3.4 3.0.0x86-64 Windows 8 ProYes2.6,2.7,3.2,3.3,3.4a3,2.2.0x86,x86-64 Windows 8.1 ProYes2.6,2.7,3.2,3.3,3.4 2.4.0x86,x86-64 手册教程
在 Python 的图像库中使用最多的是 Image 类,从文件中加载图像可使用 Image 模块中的 open() 函数:
使用图像类
- #导入模块 >>> >from PIL import Image#以只读模式查看图片 >>> im = Image.open('C:/users/yxn/desktop/1.jpg', 'r')#获取图片属性 >>> print(im)#查看图片大小 >>> 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 osfrom PIL import ImageImageFile = '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 osfrom PIL import Imagesize = 128,
- 128ImageFile = 'C:/Users/yxn/Desktop/1.jpg'#分割文件路径和后缀名FilePath,
- Fileext = os.path.splitext(ImageFile)#设置保存后的文件格式outImageFile = "{0}.ico".format(FilePath)#打开图片im = Image.open(ImageFile)#设置图片裁剪大小im.thumbnail(size)#保存图片模式为JPEGim.save(outImageFile, 'JPEG')
剪切、粘贴从图片获取一个矩形
该区域由一个元祖组成,这个元祖中包含 4 个值分别是(左、上、右、下的坐标)那么复制的内容实际 宽度 = 右坐标 - 左坐标,高度的计算方式:下坐标 - 上坐标 = 高度
蜗牛小注:这里的四个值为想要进行操作的图片矩形的四条边的坐标,即分别为左边距(要操作的矩形左侧距离整个图片最左边的距离)、上边距(要操作的矩形上侧距离整个图片最上边的距离)、右边距(要操作的矩形右侧距离整个图片最左边的距离)、下边距(要操作的矩形下侧距离整个图片最上边的距离)。与移动端开发里,四个值的含义是不同的,移动端的四个值含义分别为 x(即左边距)y(上边距),要操作的宽,要操作的高。所以如果在 Pillow 里坐标值写错了的话会报错 ValueError: images do not match。这时候就要注意你的右边距是不是比左边距小了或者下边距比上边距小了。如图:
- # ! /usr/bin / env python3 '''裁剪图片'''import osfrom PIL import Imagesize = 128,
- 128ImageFile = '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,
- ImageDrawImageFile = ''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 randomfrom PIL import Image,
- ImageFont,
- ImageDrawdef 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()
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: