用PIL库进行图像处理
一、如果系统里没有安装PIL库的,请先到命令提示符输入“pip install pillow”进行安装
二、之后就可以参考以下的代码
from PIL import Image
from pylab import * # 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
figure() pil_im = Image.open('E:\python\Python Computer Vision\Image data\empire.jpg')
gray()
subplot(121)
title(u'原图',fontproperties=font)
axis('off')
imshow(pil_im) pil_im = Image.open('E:\python\Python Computer Vision\Image data\empire.jpg').convert('L')
subplot(122)
title(u'灰度图',fontproperties=font)
axis('off')
imshow(pil_im) show()
三,可以观看效果


四、其还可以进行旋转等操作
from PIL import Image
from pylab import * # 添加中文字体支持
from matplotlib.font_manager import FontProperties font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
figure() # 显示原图
pil_im = Image.open('E:/python/Python Computer Vision/Image data/empire.jpg')
print(pil_im.mode, pil_im.size, pil_im.format)
subplot(231)
title(u'原图', fontproperties=font)
axis('off')
imshow(pil_im) # 显示灰度图
pil_im = Image.open('E:/python/Python Computer Vision/Image data/empire.jpg').convert('L')
gray()
subplot(232)
title(u'灰度图', fontproperties=font)
axis('off')
imshow(pil_im) # 复制并粘贴区域
pil_im = Image.open('E:/python/Python Computer Vision/Image data/empire.jpg')
box = (100, 100, 400, 400)
region = pil_im.crop(box)
region = region.transpose(Image.ROTATE_180)
pil_im.paste(region, box)
subplot(233)
title(u'复制粘贴区域', fontproperties=font)
axis('off')
imshow(pil_im) # 缩略图
pil_im = Image.open('E:/python/Python Computer Vision/Image data/empire.jpg')
size = 128, 128
pil_im.thumbnail(size)
print(pil_im.size)
subplot(234)
title(u'缩略图', fontproperties=font)
axis('off')
imshow(pil_im)
pil_im.save('E:/python/Python Computer Vision/Image data/empire thumbnail.jpg')# 保存缩略图 #调整图像尺寸
pil_im=Image.open('E:/python/Python Computer Vision/Image data/empire thumbnail.jpg')
pil_im=pil_im.resize(size)
print(pil_im.size)
subplot(235)
title(u'调整尺寸后的图像',fontproperties=font)
axis('off')
imshow(pil_im) #旋转图像45°
pil_im=Image.open('E:/python/Python Computer Vision/Image data/empire thumbnail.jpg')
pil_im=pil_im.rotate(45)
subplot(236)
title(u'旋转45°后的图像',fontproperties=font)
axis('off')
imshow(pil_im) show()
查看效果:

用PIL库进行图像处理的更多相关文章
- Python 之 使用 PIL 库做图像处理
http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html Python 之 使用 PIL 库做图像处理 1. 简介. 图像 ...
- [转]Python 之 使用 PIL 库做图像处理
Python 之 使用 PIL 库做图像处理 1. 简介. 图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴.PIL (Python Imaging ...
- PIL库,图像处理第三方库
PIL ---> python imaging library 安装需要安装pillow库,包含了21种类,其中Image类是PIL最重要的一个类,可以通过它来处理图像. Python最常用的 ...
- python爬虫基础15-python图像处理,PIL库
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- python图像处理之PIL库
PIL库是python语言第三方库,需要通过pip工具安装,安装库的名字是pillow. PIL库支持图像存储,显示和处理,它能够处理几乎所有图片格式,可以完成对图像的缩放,裁剪,叠加以及向图像添加线 ...
- PIL库图像处理
PIL有如下几个模块 Image模块.ImageChops模块.ImageCrackCode模块 ImageDraw模块.ImageEnhance模块.ImageFile模块 ImageFileIO模 ...
- 一秒钟带你走进P图世界-----(python)PIL库的使用
python-----PIL库的使用 一.什么是PIL库 1.PIL(Python Image Library)库是python语言的第三方库,具有强大的图像处理能力,不仅包含了丰富的像素.色彩操作功 ...
- python之PIL库(Image模块)
PIL(Python Image Library)是python的第三方图像处理库,PIL的功能非常的强大,几乎被认定是Python的官方图像处理库了. 由于PIL仅支持到python2.7于是一群志 ...
- 玩转PIL库
1.安装pillow库: 在cmd下,输入简单的命令: pip install pillow 即可安装pillow库. 2.PIL库的简介: 1. PIL库主要有2个方面的功能: (1) 图像归档: ...
随机推荐
- 3. Scala运算符
3.1 运算符介绍 运算符是一种特殊的符号,用以表示数据的运算.赋值和比较等 1) 算术运算符 2) 赋值运算符 3) 比较运算符(关系运算符) 4) 逻辑运算符 5) 位运算符 3.2 算术运算符 ...
- Express全系列教程之(五):Express的中间件
一.中间件 从字面意思,我们可以了解到它大概就是做中间代理操作,事实也是如此:大多数情况下,中间件就是在做接收到请求和发送响应中间的一系列操作.事实上,express是一个路由和中间件的web框架,E ...
- Android的Service的创建与使用
Service介绍 Service是Android四大组件中与Activity最为相似的组件,它们都代表可执行的程序,区别是:Service一直在后台运行,没有用户界面.使用service要向Acti ...
- echo 与 printf的区别与联系
echo命令默认是带有换行符的. 如果想让echo命令去掉每一行后面的换行符 方法1; 如果确信自己的脚本程序只运行在bash上,可以使用如下语法来出去空格: echo -n "Is it ...
- 常用的 jQuery 事件
$(document).ready(): $(document).ready() 方法允许我们在文档完全加载完后执行函数. click(): click() 方法是当按钮点击事件被触发时会调用一个函数 ...
- springboot+spring security +oauth2.0 demo搭建(password模式)(认证授权端与资源服务端分离的形式)
项目security_simple(认证授权项目) 1.新建springboot项目 这儿选择springboot版本我选择的是2.0.6 点击finish后完成项目的创建 2.引入maven依赖 ...
- struts 2.5配置
1.jar包的变动 必需jar包,旧版本: 必需jar包,新版本: 在struts-2.5.16版本的lib目录下没有xwork-core的jar包,原因是被合并到struts-core这个jar里了 ...
- git备注
下拉仓库 git clone ""建立本地分支 git checkout -b "分支名字" 修改代码以后切换到主分支 git checkout master更 ...
- python小程序--one
#!/usr/bin/env python # _*_ coding:utf8 _*_ import sys user_lock_file="user_lock.txt" # 用户 ...
- 第九届蓝桥杯C/C++B组省赛感想
因为做了近三年的初赛题,都对了5题+,所以这次比赛前信心满满,心里想最少水个省二没问题.可我怎么知道今年的套路居然和以前不一样了!一题深搜都没有,想想一周前做的第七届初赛题,10题有3.4题深搜题. ...