zoukankan      html  css  js  c++  java
  • 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)}

  • 相关阅读:
    C++实现多项式曲线拟合--polyfit-超定方程
    C# XmlDocument操作XML
    C#下使用XmlDocument详解
    前端常见的9种设计模式
    前端常用的设计模式
    前端需要了解的9种设计模式
    TCP协议详解
    请UI小姐姐喝了一杯奶茶要来的网站
    nodemon 基本配置与使用
    wireshark抓包新手使用教程
  • 原文地址:https://www.cnblogs.com/cnsanshao/p/6234908.html
Copyright © 2011-2022 走看看