本文是节选自 PIL handbook online 并做了一些简单的翻译

只能保证自己看懂,不保证翻译质量。欢迎各位给出意见。

------------------------------------------------------

    Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片。例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来:

1 >>>from PIL import Image
2  >>>im = Image.open("j.jpg")
3  >>>im.rotate(45).show()

    下面这个脚本则创建了当前目录下所有以 .jpg 结尾的图片的缩略图。

Create thumbnail
1 from PIL import Image
2  import glob, os
3
4 size = 128, 128
5  for infile in glob.glob("*.jpg"):
6 file, ext = os.path.splitext(infile)
7 im = Image.open(infile)
8 im.thumbnail(size, Image.ANTIALIAS)
9 im.save(file + ".thumbnail", "JPEG")

    Image 类中的函数

0.    new : 这个函数创建一幅给定模式(mode)和尺寸(size)的图片。如果省略 color 参数,则创建的图片被黑色填充满,如果 color 参数是 None 值,则图片还没初始化。

new
1 Image.new( mode, size ) => image
2 Image.new( mode, size, color ) => image

1.    open : 打开并识别所提供的图像文件。不过,使用这函数的时候,真正的图像数据在你进行数据处理之前并没有被读取出来。可使用 load 函数进行强制加载。 mode 参数可以省略,但它只能是 "r" 值。

open
1 Image.open( infile ) => image
2 Image.open( infile, mode ) => image

2.    blend : 使用两幅给出的图片和一个常量 alpha 创建新的图片。两幅图片必须是同样的 size 和 mode 。

blend
1 Image.blend( image1, image2, alpha ) => image
2  # 结果 与 运算过程
3 # out = image1 * ( 1.0 - alpha ) + image2 * alpha

3.    composite : 使用两幅给出的图片和一个与 alpha 参数相似用法的 mask 参数,其值可为:"1", "L", "RGBA" 。两幅图片的 size 必须相同。

composite
1 Image.composite( image1, image2, mask ) => image

4.    eval : 使用带一个参数的函数作用于给定图片的每一个像素。如果给定的图片有超过一个的 频段(band),则该函数也会作用于每一个频段。注意,该函数是每一个像素计算一次,所以不能使用一些随机组件或其他的生成器。

eval
1 Image.eval( image, function ) => image

5.    frombuffer : (PIL 1.1.4 中新添加的)使用标准 "raw" 解码器在像素数据或是对象缓存中创建一个图像副本。不是所有的模式都支持这种用法。支持的 mode 有"L", "RGBX", "RGBA", "CMYK"。

frombuffer
Image.frombuffer( mode, size, data ) => image

6.    fromstring : 注意,这个函数只对像素数据进行解码,而不是一整张图片。如果你有一整张字符串格式的图片,使用 StringIO 对其进行包装并用 open 函数载入它。

fromstring
1 # 使用字符串类型的像素数据和标准解码器 "raw" 来创建图像
2  Image.fromstring( mode, size, data ) => image
3
4  # 同上。不过允许你使用其他 PIL 支持的像素解码器。
5  Image.fromstring( mode, size, data, decoder, parameters ) => image

7.    merge : 使用一系列单一频段(band)的图像来创建新的一幅图像。频段是以一些图像组成的元组或列表,所有的 band 必须有相同大小的 size 。

merge
1 Image.merge( mode, bands ) =>image

    Image 类中的方法

0.    convert : 返回一个转换后的图像的副本。

convert
1 # If mode is omitted, a mode is chosed so that all information in the image and the palette can be representedwithout a palette .
2 # when from a colour image to black and white, the library uses the ITU-R 601-2 luma transfrom:
3 # L = R * 299/1000 + G * 587/1000 + B * 114/1000
4  im.convert( mode ) => image
5
6  # Converts an "RGB" image to "L" or "RGB" using a conversion matrix. The matrix is 4- or 16-tuple.
7  im.convert( mode, matrix ) => image

    下面是一个例子:转换 RGB 为 XYZ 。

RGB2XYZ
1 rgb2xyz = (
2 0.412453, 0.357580, 0.180423, 0,
3 0.212671, 0.715160, 0.072169, 0,
4 0.019334, 0.119193, 0.950227, 0 )
5 out = im.convert("RGB", rgb2xyz)

1.    copy : 复制图像。如果你希望粘贴一些东西进图像里面的话可以使用这个方法,但仍然会保留原图像。

copy
1 im.copy() => image

2.    crop : 返回图像某个给定区域。box 是一个 4 元素元组,定义了 left, upper, right, lower 像素坐标。使用这个方法的时候,如果改变原始图像,可能会,也可能不会改变裁剪生成的图像。创建一个完全的复制,裁剪复制的时候使用 load 方法。

crop
1 im.crop( box ) => image

3.    draft : 按给出的 mode 和 size 进行配置。可以使用这个方法将彩色JPEG图片转为灰度图。

draft
1 im.draft(mode, size)

4.    filter : 返回图像使用滤波器后的副本。可以看 这里 获取更多有用的滤波器。

filter
1 im.filter( filter ) => image

5.    fromstring : 和前面的函数是一样的功能,不过这个方法是将数据载入到当前图像。

fromstring
1 im.fromstring( data )
2 im.fromstring( data, decoder, parameters )
6.    getbands : 返回一个元组,包含每一个 band 的名字,比如,在一幅 RGB 格式的图像上使用 getbands 则返回("R", "G", "B")。
getbands
1 im.getbands( ) => tuple of strings

7.    getbbox : 计算图像边框值,返回一个 4-元组 ,值为(左,上,右,下)。

getbbox
1 im.getbbox() => 4-tuple or None

8.    getcolors : 在 1.1.5 版本中新添加的。返回一个未排序列表,其元素是元组(count, color)。the count is the number of times the corresponding color occurs in the image 。If the maxcolors value is exceeded, the method stops counting and returns None。

getcolors
1 im.getcolors() => a list of (count, color) tuples or None
2 im.getcolors( maxcolors ) => a list of (count, color) tuples or None

9.    getdata : 返回一个图像内容的像素值序列。不过,这个返回值是 PIL 内部的数据类型,只支持确切的序列操作符,包括迭代器和基本序列方法。我们可以通过 list(im.getdata())  为其生成普通的序列。

getdata
1 im.getdata() => sequence

10.    getextrema : 返回一个 2-元组 ,值为图像的最小最大值。在当前PIL版本中,仅支持单一频段(single-band)的图像。

getextreme
1 im.getextrema() => 2-tuple

11.    getpixel : 返回指定位置的像素,如果所打开的图像是多层次的图片,那这个方法就返回一个元组。

getpixel
1 im.getpixel( xy ) => value or tuple

12.    histogram : 返回图像直方图,值为像素计数组成的列表。如果有参数 mask ,则返回图像所有部分的直方图。

histogram
1 im.histogram() => list
2
3 im.histogram( mask ) => list

13.    load : 版本 1.1.6 新添加的。load 返回对象的像素值,可以用来修改像素值。

load
1 im.load()
2
3  #
4  pix = im.load()
5  print pix[x, y]
6 pix[x, y] = value

14.    paste : 1). 粘贴新图片至图片中,box 参数可以为 2-元组(upper, left)或是 4-元组(left, upper, right, lower),或者是 None(0, 0)。2). 功能同上。不过是将指定位置填充为某种颜色。

paste
1 im.paste( image, box )
2
3 im.paste( colour, box )
4
5 im.paste( image, box, mask )
6
7 im.paste( colour, box, mask )

15.    point : 

point
1 im.point( bable ) => image
2 im.point( function ) => image

  ------------

未完待续。

posted on 2011-04-20 23:13  oyzway  阅读(69494)  评论(2编辑  收藏  举报