python PIL Image模块
原地址:http://hi.baidu.com/drunkdream/item/9c9ac638dfc46ec6382ffac5
实验环境:
windows7+python2.6+pycrust+PIL1.1.7
实验操作:
Image模块
例子:打开、旋转、显示一副图像
>>> import Image
>>> infile='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im=Image.open(infile)
>>> im.rotate(45).show()
效果在实验31里有,不贴了
创建缩略图:
>>> import Image
>>> import os
>>> infile='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> file,ext=os.path.splitext(infile)
>>> im=Image.open(infile)
>>> im.thumbnail((128,128),Image.ANTIALIAS)
>>> im.save(file+'.thumbnail','JPEG')
这个在实验31里也有,不贴了
函数:
new:
创建一张新图片(800*600像素,黑色填充):
>>> imnew=Image.new('RGB',(800,600))
>>> imnew.show()
800*600像素,指定颜色:
>>> imnew=Image.new('RGB',(800,600),(100,200,255))
>>> imnew.show()
open:
打开一个图像文件,这个前面已经有很多示例了
blend:
融合,alpha为0则返回第一张图像的拷贝,1则返回第二张图像的拷贝,例子取0.5,手册说这个值没有严格的限制。
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> infile2='e:/doc/program/python/2.6/study/temp/app.jpg'
>>> im1=Image.open(infile1)
>>> im2=Image.open(infile2)
>>> im=Image.blend(im1,im2,0.5)
>>> outfile='e:/doc/program/python/2.6/study/temp/blend.jpg'
>>> im.save(outfile)
im1图:
im2图:
融合后的效果图:
composite:
也是混合成类似的意思
>>> infile3='e:/doc/program/python/2.6/study/temp/l.bmp'
>>> im3=Image.open(infile3)
>>> print im3.mode,im3.size
1 (800, 600)
使用mask图像作为alpha参数:
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> infile2='e:/doc/program/python/2.6/study/temp/app.jpg'
>>> infile3='e:/doc/program/python/2.6/study/temp/l.bmp'
>>> im1=Image.open(infile1)
>>> im2=Image.open(infile2)
>>> im3=Image.open(infile3)
>>> outfile='e:/doc/program/python/2.6/study/temp/blend.jpg'
>>> im=Image.composite(im1,im2,im3)
>>> im.save(outfile)
im1、im2原图前面有不贴了
im3是用windows的画图创建的800*600的单色位图
合成后的效果图:
Image.eval(function,image) =>image
对给定图像的每一个像素应用function
Image.fromstring(mode,size,data) =>image
从字符串里的像素数据创建图像,使用标准的"raw"解码器
Image.fromstring(mode,size,data,decoder,parameters) =>image
跟上面一样的,可以使用PIL支持的像素解码器
有一些注意点,似乎我不太会用到先继续往下。
Image.merge(mode,bands) =>image
也是合并的,跟bands相关的,不懂而且也用不到,先跳过往下
方法:
转换模式:
im.convert(mode) =>image
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im1=Image.open(infile1)
>>> print im1.mode
RGB
>>> outfile1='e:/doc/program/python/2.6/study/temp/freebsd_out.jpg'
>>> im1.convert(mode='L').save(outfile1)
>>> print im1.mode
RGB
>>> imout=Image.open(outfile1)
>>> print imout.mode
L
im1原图:
模式转换后的效果图:
使用变换矩阵来转换图像模式:
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> outfile1='e:/doc/program/python/2.6/study/temp/freebsd_out.jpg'
>>> im1=Image.open(infile1)
>>> rgb2xyz=(0.412453,0.357580,0.180423,0,
... 0.212671,0.715160,0.072169,0,
... 0.019334,0.119193,0.950227,0)
>>> outim=im1.convert("RGB",rgb2xyz)
>>> outim.save(outfile1)
模式转换后的效果图:
拷贝、裁剪、粘帖:
>>> import Image
>>> box=(200,200,500,500)
>>> infile='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> outfile='e:/doc/program/python/2.6/study/temp/freebsd_out.jpg'
>>> im=Image.open(infile)
>>> imout=im.crop(box).save(outfile)
infile原图:
裁剪图:
把上面裁剪的图粘帖到另一幅图:
>>> infile1='e:/doc/program/python/2.6/study/temp/app.jpg'
>>> infile2='e:/doc/program/python/2.6/study/temp/freebsd_out.jpg'
>>> im1=Image.open(infile1)
>>> im=Image.open(infile2).copy()
>>> outfile='e:/doc/program/python/2.6/study/temp/app_out.jpg'
>>> im1.paste(im,(100,100))
>>> im1.save(outfile)
粘帖后的图:
draft(草图?):
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im1=Image.open(infile1)
>>> print im1.mode,im1.size
RGB (800, 600)
>>> outfile='e:/doc/program/python/2.6/study/temp/freebsd_out.jpg'
>>> im1.draft(mode='L',size=(120,100))
<JpegImagePlugin.JpegImageFile image mode=L size=200x150 at 0x286D990>
>>> print im1.mode,im1.size
L (200, 150)
>>> imout.save(outfile)
没按我指定的size?
原图同前
效果图:
im.filter(filter) =>image
用给定的过滤器过滤
im.fromstring(data)
im.fromstring(data,decoder,parameters)
同fromstring函数,但是加载数据到当前图像
im.getbands() bands这个词太专业了,不懂,快速跳过。返回一个字符串元组,例如getbands在一个RGB图像上返回("R","G","B")
im.getbbox() 返回4元组或者None。计算图像非零区域的界限范围
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im1=Image.open(infile1)
>>> result=im1.getbbox()
>>> result
(0, 72, 800, 600)
为什么这个800*600的图返回的不是(0,0,800,600)呢?是因为上面部分接近或者是黑色了?
im.getdata() 以序列的方式返回图像内容,序列对象是内部PIL数据类型,要转换成普通序列要用list(im.getdata())
im.getextrema() 返回图像的最小和最大值
>>> result=im1.getextrema()
>>> result
((0, 255), (0, 255), (0, 255))
当前版本仅仅应用于single-band图像
im.getpixel(xy) 返回给定位置的像素,多层图像则返回元组
>>> result=im1.getpixel((150,150))
>>> result
(3, 3, 3)
这个位置有3个像素?图像知识太缺乏了
im.histogram() 返回图像的柱状图(histogram),以像素数列表的方式返回,每个值是原图像的像素值?多bands时还会连接起来。看不懂这种东西,继续往下
>>> result=im1.histogram()
>>> result
[102456, 5040, 4490, 6720, 4930, 3975, 4296, 5587, 7128, 5951, 4099, 3737, 3177, 2959, ......]
>>> len(result)
768
还可以用mask做参数,具体要用时看手册好了。太专业了基本上我用不到
im.load()
为图像分配存储,并从文件或来源加载。通常不需要调用这个方法,因为Image类会自动智能搞定的
im.offset(xoffset),yoffset) =>image
一个不赞成使用的方法,跳过
im.paste(image,box)
粘帖另一幅图像image到这幅图像im,前面有示例了
im.paste(colour,box)
作用同上,但是用单色来填充区域。多band图像则传递元组参数
im.paste(image,box,mask)
还可以用mask参数来做一些更加复杂一点的操作
im.paste(colour,box,mask)
主要作用都差不多的,看看参数也大体明白是上面这些复杂的操作方式的混合运用
对图片的像素点进行操作?
im.point(table)
im.point(function)
im.point(table,mode)
im.point(function,mode)
im.putalpha(band)
拷贝给定的band到当前图像的alpha(透明?)层
im.putdata(data)
im.putdata(data,scale,offset)
从序列对象拷贝像素值到图像
im.putpalette(sequence)
把一个调色板附加到"P"或"L"模式的图像
im.putpixel(xy,colour)
修改给定位置的像素。这个方法相对比较慢
im.resize(size)
im.resize(size,filter)
调整图像大小,filter可以是NEAREST,BILINEAR,BICUBIC
im.rotate(angle)
im.rotate(angle,filter)
按给定的角度逆时针旋转图像,filter可以是NEAREST,BILINEAR,BICUBIC
im.save(outfile,options)
im.save(outfile,format,options)
用给定的文件名保存图像,format是格式参数
im.seek(frame)
在一个序列文件中寻找指定的帧,seek超过了序列结尾时产生EOFError异常。当一个序列文件被打开时,库自动seek到第0帧,当前版本只允许做seek下一帧的操作
im.show()
显示一幅图像,这个方法主要是为调试目的设计的。在unix平台,这个方法保存文件到一个ppm临时文件,然后调用xv工具来显示。在windows平台,则保存到一个bmp临时文件,然后使用标准的bmp显示工具来显示。方法返回None
im.split() =>sequence
不懂怎么翻译,用个例子来试试看,对一个GRB图像来操作:
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im=Image.open(infile1)
>>> im1,im2,im3=im.split()
>>> im1.save('e:/doc/program/python/2.6/study/temp/freebsd_out1.jpg')
>>> im2.save('e:/doc/program/python/2.6/study/temp/freebsd_out2.jpg')
>>> im3.save('e:/doc/program/python/2.6/study/temp/freebsd_out3.jpg')
infile1原图前面有,得到的三张效果图如下:
im.tell() =>integer
返回当前帧号
im.thumbnail(size)
im.thumbnail(size,filter)
修改图像成缩略图,除非速度比质量更重要,否则filter应该使用ANTIALIAS
im.tobitmap() =>string
返回被转换成X11位图的图像
im.tostring() =>string
返回一个包含像素数据的字符串,未指定解码器时则使用raw解码器
im.tostring(encoder,parameters) =>string
变换:
im.transform(size,method,data)
im.transform(size,method,data,filter)
im.transform(size,EXTENT,data)
im.transform(size,EXTENT,data,filter)
im.transform(size,AFFINE,data)
im.transform(size,AFFINE,data,filter)
...太多了,要用时再看手册
翻转或旋转图像:
im.transpose(method)
method可以是Image.FLIP_LEFT_RIGHT......等等,实验31里已经试过了
im.verify()
试图确定文件是否有破碎损坏,而不会真的去解码图像数据。如果发现有任何问题则会产生适当的异常。如果需要检验后加载图像则需要重新打开图像文件。
属性:
im.format
文件格式,用这个库创建的图像这个属性设置为None
>>> infile1='e:/doc/program/python/2.6/study/temp/freebsd.jpg'
>>> im=Image.open(infile1)
>>> im.format
'JPEG'
im.mode
图像模式
>>> im.mode
'RGB'
im.size
图像尺寸,用像素表示
>>> im.size
(800, 600)
im.palette
色彩调色板表,如果有的话。其他情况用到时再看看手册
im.info
跟图像相关的数据,字典格式
>>> im.info
{'jfif': 258, 'jfif_unit': 0, 'adobe': 100, 'jfif_version': (1, 2), 'adobe_transform': 100, 'jfif_density': (100, 100)}
python PIL Image模块的更多相关文章
- Python PIL模块笔记
利用python pil 实现给图片上添加文字 图片中添加文字#-*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont ttfo ...
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- Python 利用pytesser模块识别图像文字
使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the ...
- python常见的模块
Python内置模块名称 功能简介 详细解释/使用示例 os 和操作系统相关 os.path — Common pathname manipulations sys 和系统相关 sys — Syste ...
- Python 一些常用模块的安装
(1)python PIL(image)模块的安装 sudo apt-get install python-imaging
- Python进阶之模块
在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很 ...
- python PIL 图像处理操作
python PIL 图像处理 # 导入Image库 import Image # 读取图片 im = Image.open("1234.jpg") # 显示图片 im.show( ...
- Python关于导入模块的一些感想:
写项目的时候,碰到这种情况 程序业务为core,里面有两个目录,core1 和core2 core1中有三个模块,business main main1 程序入口为bin目录下的project ...
- python 内建模块与第三方模块
*)datetime模块 包括时间.时间对象.时间戳.时区.时区的转换 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/101764878 ...
随机推荐
- PHP JS JQ 异步上传并立即显示图片
提交页面: <! DOCTYPE html> < html> < head> < meta charset ="GB2312" > ...
- ios 开发中 动态库 与静态库的区别
使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...
- X3850M2安装CertOS 7 KVM 2--VNC
需要安装远程桌面,否则无鼠标的日子比较难. VNC的安装需要步骤较多,重点参考以下文章: http://www.itzgeek.com/how-tos/linux/centos-how-tos/con ...
- poj3693
//Accepted 12004 KB 407 ms /* source:poj3693 time :20150819 by :songt */ /*题解: 搞了一天,总算弄完了 首先,我们来明确一个 ...
- Spark源码学习1.4——MapOutputTracker.scala
相关类:MapOutputTrackerMessage,GetMapOutputStatuses extends MapPutputTrackerMessage,StopMapOutputTracke ...
- JRE JDK JVM是什么
jre: Java Runtime Environment java运行环境 ----------------------------------------- jdk:Java Developmen ...
- JS总结 节点
nodeName 获取节点名称 元素节点:返回标记名称 属性节点:返回属性名称 文本节点:返回文本#text nodeTyle 获取节点类型 元素节点:返回1 属性节点:返回2 文本节点:返回3 n ...
- eclipse中将Maven Dependencies Libraries移除后的恢复办法
在eclipse中,如果你不小心在properties=>Java Build Path中将Maven Dependencies Libraries 移除了怎么恢复呢? 解决办法:1.右键你的项 ...
- MSSQL—字符串分离(Split函数)
前面提到了记录合并,有了合并需求肯定也会有分离需求,说到字符串分离,大家肯定会想到SPLIT函数,这个在.NET,Java和JS中都有函数,很可惜在SQL SERVER中没有,我们只能自己来写这么一个 ...
- Linux 网络配置
1. 查看网卡型号: lspci | grep -i ethernet 上图中显示了网卡的具体型号,可以根据这个网卡型号在Intel官网下载Linux版本的驱动. 2. 以太网开机启动: 以太网的配置 ...