- 论坛徽章:
- 0
|
转:oyzway
PIL 中的 Image 模块
本文是节选自 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 ) => image2 Image.new( mode, size, color ) => image
复制代码 1. open : 打开并识别所提供的图像文件。不过,使用这函数的时候,真正的图像数据在你进行数据处理之前并没有被读取出来。可使用 load 函数进行强制加载。 mode 参数可以省略,但它只能是 "r" 值。
open- 1 Image.open( infile ) => image2 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 : 复制图像。如果你希望粘贴一些东西进图像里面的话可以使用这个方法,但仍然会保留原图像。
copy2. crop : 返回图像某个给定区域。box 是一个 4 元素元组,定义了 left, upper, right, lower 像素坐标。使用这个方法的时候,如果改变原始图像,可能会,也可能不会改变裁剪生成的图像。创建一个完全的复制,裁剪复制的时候使用 load 方法。
crop- 1 im.crop( box ) => image
复制代码 |
|