052-python库PIL(二)

上一篇:051-python库PIL(一)

一、The Image Module(图像模块)

The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

图像模块提供了一个具有相同名称的类,用于表示一个库的图像。工厂的模块还提供了许多功能,包括函数加载图片文件,创建新的图像。

Examples

The following script loads an image, rotates it 45 degrees, and displays it using an external viewer (usually xv on Unix, and the paint program on Windows).

下面的脚本加载一个图像,旋转45度,并显示它使用一个外部查看器(通常在Unix中,十五和油漆程序在Windows上)。

  • Open, rotate, and display an image (using the default viewer)(打开,旋转,并显示一个图像(使用默认查看器))
from PIL import Image
im = Image.open("bride.jpg")
im.rotate(45).show()

The following script creates nice 128x128 thumbnails of all JPEG images in the current directory.

下面的脚本创建好128 x128当前目录中的所有JPEG图像的缩略图。

  • Create thumbnails(创建缩略图)
from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save(file + ".thumbnail", "JPEG")
Functions
  • new

Image.new(mode, size) ⇒ image

Image.new(mode, size, color) ⇒ image

Creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels. The color is given as a single value for single-band images, and a tuple for multi-band images (with one value for each band). In 1.1.4 and later, you can also use color names (see the ImageColor module documentation for details) If the color argument is omitted, the image is filled with zero (this usually corresponds to black). If the color is None, the image is not initialised. This can be useful if you’re going to paste or draw things in the image.

创建一个新的形象与给定的模式和规模。大小是作为(宽度、高度)元组,以像素为单位。给出的颜色作为一个单一值的单波段图像,以及多波段图像的元组(每带一个值)。1.1.4和后,您还可以使用颜色名称(有关详细信息,请参阅ImageColor模块文档)如果省略颜色参数,图像充满零(这通常对应于黑)。如果没有颜色,图像不是初始化。这可以有用,如果你要粘贴或画的形象。

from PIL import Image
im = Image.new("RGB", (512, 512), "white")
  • open

Image.open(file) ⇒ image

Image.open(file, mode) ⇒ image

Opens and identifies the given image file. This is a lazy operation; the function reads the file header, but the actual image data is not read from the file until you try to process the data (call the load method to force loading). If the mode argument is given, it must be “r”.

打开并识别给定的图像文件。这是一个懒散的操作;函数读取文件头,但实际图像数据不是从文件读取,直到你尝试处理数据(调用load方法强制加载)。如果模式参数是已知的,它必须是“r”。

You can use either a string (representing the filename) or a file object as the file argument. In the latter case, the file object must implement read, seek, and tell methods, and be opened in binary mode.

您可以使用一个字符串(代表文件名)或一个文件对象作为参数的文件。在后一种情况下,文件对象必须实现阅读、探索,并告诉方法,是在二进制模式下打开。

from PIL import Image
im = Image.open("lenna.jpg")
from PIL import image
from StringIO import StringIO

# read data from string
im = Image.open(StringIO(data))
  • blend

Image.blend(image1, image2, alpha) ⇒ image

Creates a new image by interpolating between the given images, using a constant alpha. Both images must have the same size and mode.

创建一个新的图像之间的插值给定的图像,使用一个常数α。图像都必须有相同的大小和模式。

out = image1 * (1.0 - alpha) + image2 * alpha

If the alpha is 0.0, a copy of the first image is returned. If the alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range.

如果α为0.0,返回第一张照片的副本。如果α为1.0,第二个返回图像的一个副本。在alpha值没有限制。如果有必要,结果剪适应允许输出范围。

  • composite

Image.composite(image1, image2, mask) ⇒ image

Creates a new image by interpolating between the given images, using the corresponding pixels from a mask image as alpha. The mask can have mode “1”, “L”, or “RGBA”. All images must be the same size.

之间创建一个新的图像插值给定的图像,使用对应的像素从面具α的形象。面具可以模式“1”、“L”或“RGBA”。所有图像都必须是相同的大小。

  • eval

Image.eval(image, function) ⇒ image

Applies the function (which should take one argument) to each pixel in the given image. If the image has more than one band, the same function is applied to each band. Note that the function is evaluated once for each possible pixel value, so you cannot use random components or other generators.

应用函数(这应该采取一个参数)在给定的图像每个像素。如果图像有多个乐队,相同的功能应用到每一个乐队。注意,函数为每个可能的像素值评估一次,所以你不能使用随机组件或其他发电机。

  • frombuffer

Image.frombuffer(mode, size, data) ⇒ image

(New in PIL 1.1.4). Creates an image memory from pixel data in a string or buffer object, using the standard “raw” decoder. For some modes, the image memory will share memory with the original buffer (this means that changes to the original buffer object are reflected in the image). Not all modes can share memory; supported modes include “L”, “RGBX”, “RGBA”, and “CMYK”. For other modes, this function behaves like a corresponding call to the fromstring function.

Image.frombuffer(模式、大小数据)⇒形象在库1.1.4(新)。创建一个图像像素数据的内存一个字符串或缓冲区对象,使用标准的“原始”解码器。对于一些模式,图像与原缓冲区内存将共享内存(这意味着改变原始缓冲区对象反映在图像)。不是所有的模式都能共享内存;支持的模式包括“L”、“RGBX”、“RGBA”,“CMYK”。这个函数对于其他模式,像一个相应的调用fromstring函数。

Note: In versions up to and including 1.1.6, the default orientation differs from that of fromstring. This may be changed in future versions, so for maximum portability, it’s recommended that you spell out all arguments when using the “raw” decoder:

注意:在版本包括1.1.6,默认fromstring取向不同。这可能会改变在未来的版本中,为了获得最大的可移植性,建议你拼出所有参数在使用“原始”译码器:

im = Image.frombuffer(mode, size, data, "raw", mode, 0, 1)

Image.frombuffer(mode, size, data, decoder, parameters) ⇒ image

Same as the corresponding fromstring call.

相应的fromstring调用一样。

  • fromstring

Image.fromstring(mode, size, data) ⇒ image

Creates an image memory from pixel data in a string, using the standard “raw” decoder.

创建一个图像像素数据的内存一个字符串,使用标准的“原始”解码器。

Image.fromstring(mode, size, data, decoder, parameters) ⇒ image

Same, but allows you to use any pixel decoder supported by PIL. For more information on available decoders, see the section Writing Your Own File Decoder.

相同,但允许您使用任何像素译码器支持的库。有关可用解码器的更多信息,请参见。

Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a StringIO object, and use open to load it.

注意,这个函数解码像素数据,不完整的图像。如果你有一个完整的图像文件在一个字符串,用StringIO对象,并使用打开加载它。

  • merge

Image.merge(mode, bands) ⇒ image

Creates a new image from a number of single band images. The bands are given as a tuple or list of images, one for each band described by the mode. All bands must have the same size.

创建一个新的图像的单波段图像。给出了带一个元组或图片列表,每个乐队所描述的一个模式。所有的乐队都必须有相同的大小。

Methods

An instance of the Image class has the following methods. Unless otherwise stated, all methods return a new instance of the Image class, holding the resulting image.

图像类的实例有以下方法。除非另有说明,所有方法都返回一个新实例的图像类,生成图像。

  • convert

im.convert(mode) ⇒ image

Converts an image to another mode, and returns the new image.

When converting from a palette image, this translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.

将图像转换为另一个模式,并返回新形象。当转换从一个调色板图像,这就意味着像素通过面板。如果省略方式,选择一个模式,使所有图像和调色板中的信息可以表示没有一个调色板。

When converting from a colour image to black and white, the library uses the ITU-R 601-2 luma transform:

当将从彩色图像转换为黑白,图书馆使用ITU-R 601 - 2亮度变换:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

When converting to a bilevel image (mode “1”), the source image is first converted to black and white. Resulting values larger than 127 are then set to white, and the image is dithered. To use other thresholds, use the point method. To disable dithering, use the dither= option (see below).

当转换到一个上下两层的图像(模式“1”),源图像首先转换为黑白。产生的值大于127被设置成白色,和图像是犹豫。使用其他阈值,使用的方法。禁用犹豫不决,使用高频脉动=选项(见下文)。

im.convert(“P”, **options) ⇒ image

Same, but provides better control when converting an “RGB” image to an 8-bit palette image. Available options are:

相同,但是提供了更好的控制将“RGB图像转换为一个8位调色板图像。可用的选项是:

dither=. Controls dithering. The default is FLOYDSTEINBERG, which distributes errors to neighboring pixels. To disable dithering, use NONE.

控制抖动。默认是FLOYDSTEINBERG,邻近的像素分配错误。禁用犹豫不决,没有使用。

palette=. Controls palette generation. The default is WEB, which is the standard 216-color “web palette”. To use an optimized palette, use ADAPTIVE.

控制面板的一代。默认的网络,这是标准的216 -颜色“网络面板”。使用一个优化面板,使用自适应。

colors=. Controls the number of colors used for the palette when palette is ADAPTIVE. Defaults to the maximum value, 256 colors.

控制颜色的数量用于面板面板时自适应。默认值为最大值,256种颜色。

im.convert(mode, matrix) ⇒ image

Converts an “RGB” image to “L” or “RGB” using a conversion matrix. The matrix is a 4- or 16-tuple.

将“RGB图像转换为“L”或“RGB”使用一个转换矩阵。矩阵是一个4 -或16-tuple。

The following example converts an RGB image (linearly calibrated according to ITU-R 709, using the D65 luminant) to the CIE XYZ colour space:

下面的例子将RGB图像转换(线性校准根据ITU-R 709,使用D65发光体)到CIE XYZ颜色空间:

Convert RGB to XYZ

rgb2xyz = (
    0.412453, 0.357580, 0.180423, 0,
    0.212671, 0.715160, 0.072169, 0,
    0.019334, 0.119193, 0.950227, 0 )
out = im.convert("RGB", rgb2xyz)
  • copy

im.copy() ⇒ image

Copies the image. Use this method if you wish to paste things into an image, but still retain the original.

复制图像。使用这种方法如果你希望的东西粘贴到一个图像,但仍保留原来的。

  • crop

im.crop(box) ⇒ image

Returns a copy of a rectangular region from the current image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call the load method on the cropped copy.

返回一个复制当前图像的矩形区域。盒子是4-tuple定义左,上,右,和更低的像素坐标。这是一个懒散的操作。源图像的变化可能是也可能不是反映在裁剪图像。一个单独的副本,在裁剪复制调用加载方法。

  • draft

im.draft(mode, size)

Configures the image file loader so it returns a version of the image that as closely as possible matches the given mode and size. For example, you can use this method to convert a colour JPEG to greyscale while loading it, or to extract a 128x192 version from a PCD file.

Note that this method modifies the Image object in place (to be precise, it reconfigures the file reader). If the image has already been loaded, this method has no effect.

配置图像文件加载器所以它返回一个版本的图像尽可能匹配给定的模式和规模。例如,您可以使用此方法来颜色JPEG转换为灰度图时加载它,或从PCD提取128 x192版本文件。注意,这个方法修改图像对象(准确地说,它重新配置文件阅读器)。如果已经加载的图片,这个方法没有效果。

  • filter

im.filter(filter) ⇒ image

Returns a copy of an image filtered by the given filter. For a list of available filters, see the ImageFilter module.

返回一个副本给定图像过滤的过滤器。有关可用过滤的列表,请参见ImageFilter模块。

  • fromstring

im.fromstring(data)

im.fromstring(data, decoder, parameters)

Same as the fromstring function, but loads data into the current image.

fromstring一样的功能,但将数据加载到当前图像。

  • getbands

im.getbands() ⇒ tuple of strings

Returns a tuple containing the name of each band. For example, getbands on an RGB image returns (“R”, “G”, “B”).

包含每个乐队的名称返回一个元组。例如,getbands RGB图像返回(“R”、“G”、“B”)。

  • getbbox

im.getbbox() ⇒ 4-tuple or None

Calculates the bounding box of the non-zero regions in the image. The bounding box is returned as a 4-tuple defining the left, upper, right, and lower pixel coordinate. If the image is completely empty, this method returns None.

计算图像中零的区域的边界框。边界框返回4-tuple定义左,上,右,和更低的像素坐标。如果图像完全是空的,这个方法返回None。

  • getcolors

im.getcolors() ⇒ a list of (count, color) tuples or None

im.getcolors(maxcolors) ⇒ a list of (count, color) tuples or None

(New in 1.1.5) Returns an unsorted list of (count, color) tuples, where 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. The default maxcolors value is 256. To make sure you get all colors in an image, you can pass in size[0]*size[1] (but make sure you have lots of memory before you do that on huge images).

1.1.5(新)返回一个未排序的列表(数、颜色)元组,在计数的次数对应的颜色出现在图像。如果超出maxcolors值,停止计算方法并返回None。默认maxcolors值是256。确保你得到所有颜色的图片,你可以通过大小大小[0]*1。

  • getdata

im.getdata() ⇒ sequence

Returns the contents of an image as a sequence object containing pixel values. The sequence object is flattened, so that values for line one follow directly after the values of line zero, and so on.

返回一个图像作为一个序列对象的内容包含像素值。序列对象被夷为平地,这值线后直接按照线的值为零,等等。

Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations, including iteration and basic sequence access. To convert it to an ordinary sequence (e.g. for printing), use list(im.getdata()).

注意,这个方法返回的序列对象内部库是一种数据类型,它只支持特定的顺序操作,包括迭代和基本顺序访问。将它转换成一个普通的序列(如打印),使用列表(im.getdata ())。

  • getextrema

im.getextrema() ⇒ 2-tuple

Returns a 2-tuple containing the minimum and maximum values of the image. In the current version of PIL, this only works for single-band images.

返回一个二元数组包含图像的最小和最大值。在当前版本的库,这只适用于单波段图像。

  • getpixel

im.getpixel(xy) ⇒ value or tuple

Returns the pixel at the given position. If the image is a multi-layer image, this method returns a tuple.

Note that this method is rather slow; if you need to process larger parts of an image from Python, you can either use pixel access objects (see load), or thegetdata method.

返回给定的像素位置。如果图像是一个多层图像,该方法返回一个元组。注意,这种方法相当缓慢;如果你需要从Python处理图像的放大部分,可以使用像素访问对象(参见load),或* * getdata * *的方法。

  • histogram

im.histogram() ⇒ list

Returns a histogram for the image. The histogram is returned as a list of pixel counts, one for each pixel value in the source image. If the image has more than one band, the histograms for all bands are concatenated (for example, the histogram for an “RGB” image contains 768 values).

返回图像的直方图。直方图作为一个像素统计列表,返回一个源图像的每个像素值。如果图像有多个乐队,乐队的直方图是连接(例如,“RGB图像的直方图包含768个值)。

A bilevel image (mode “1”) is treated as a greyscale (“L”) image by this method.

(上下两层的图像模式“1”)都被视为一个灰度图像(“L”)的方法。

im.histogram(mask) ⇒ list

Returns a histogram for those parts of the image where the mask image is non-zero. The mask image must have the same size as the image, and be either a bi-level image (mode “1”) or a greyscale image (“L”).

返回一个直方图的那部分图像掩模图像是零。掩模图像形象,必须有相同的大小,是一个双层的图像(“1”模式)或灰度图像(“L”)。

  • load

im.load()

Allocates storage for the image and loads it from the file (or from the source, for lazy operations). In normal cases, you don’t need to call this method, since the Image class automatically loads an opened image when it is accessed for the first time.

分配存储图像并将其加载的文件(或从源懒操作)。在正常情况下,您不需要调用这个方法,因为图像类自动加载一个打开图像时的首次访问。

(New in 1.1.6) In 1.1.6 and later, load returns a pixel access object that can be used to read and modify pixels. The access object behaves like a 2-dimensional array, so you can do:

1.1.6 1.1.6(新)和后,负载返回一个像素访问对象,可用于读取和修改像素。访问对象的行为像一个二维数组,所以你能做什么:

pix = im.load()
print pix[x, y]
pix[x, y] = value

Access via this object is a lot faster than getpixel and putpixel.

通过这个对象访问比获取像素和putpixel快很多。

  • offset

im.offset(xoffset, yoffset) ⇒ image

(Deprecated) Returns a copy of the image where the data has been offset by the given distances. Data wraps around the edges. If yoffset is omitted, it is assumed to be equal to xoffset.

This method is deprecated, and has been removed in PIL 1.2. New code should use the offset function in the ImageChops module.

(弃用)返回图像的一个副本的数据已经抵消了给定的距离。数据封装边缘。如果省略yoffset,它被认为是等于xoffset。不建议使用这个方法,在库1.2 \删除。新代码应该使用(* offset )(http://effbot.org/tag/PIL.ImageChops.offset)功能( * ImageChops * *) (http://effbot.org/imagingbook/imagechops.htm)模块。

  • paste

im.paste(image, box)

Pastes another image into this image. The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted image must match the size of the region.

贴另一个图像到这张照片。箱参数是一个二元数组给左上角,4-tuple定义左边,上,右,和更低的像素坐标,或没有((0,0)一样)。如果一个4-tuple,粘贴图像的大小必须匹配区域的大小。

If the modes don’t match, the pasted image is converted to the mode of this image (see the convert method for details).

如果模式不匹配,粘贴图像转换为图像的模式(有关详细信息,请参阅转换方法)。

im.paste(colour, box)

Same as above, but fills the region with a single colour. The colour is given as a single numerical value for single-band images, and a tuple for multi-band images.

同上,但是该地区充满了一种单一的颜色。给出的颜色作为一个单一的数值为单波段图像,和多波段图像的元组。

im.paste(image, box, mask)

Same as above, but updates only the regions indicated by the mask. You can use either “1”, “L” or “RGBA” images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate values can be used for transparency effects.

同上,但只更新区域显示的面具。您可以使用“1”、“L”或“RGBA”图像(在后一种情况下,使用α带面具)。掩码是255,给定的图像复制。面具是0,保存当前值。中间值可用于透明效果。

Note that if you paste an “RGBA” image, the alpha band is ignored. You can work around this by using the same image as both source image and mask.

注意,如果您粘贴一个“RGBA”形象,阿尔法乐队将被忽略。您可以通过使用相同的解决这个形象源图像和面具。

im.paste(colour, box, mask)

Same as above, but fills the region indicated by the mask with a single colour.

同上,但该地区充满暗示的面具只有一个颜色。

  • point

im.point(table) ⇒ image

im.point(function) ⇒ image

Returns a copy of the image where each pixel has been mapped through the given lookup table. The table should contains 256 values per band in the image. If a function is used instead, it should take a single argument. The function is called once for each possible pixel value, and the resulting table is applied to all bands of the image.

返回图像的一个副本,每个像素被映射到给定的查找表。表应该包含256值/乐队的形象。如果使用一个函数相反,它应该采取一个参数。函数被调用一次对于每一个可能的像素值,以及由此产生的表适用于所有乐队的形象。

If the image has mode “I” (integer) or “F” (floating point), you must use a function, and the function must have the following format:

如果图像模式“我”(整数)或“F”(浮点),您必须使用一个函数,函数必须有以下格式:

argument * scale + offset

Example:

out = im.point(lambda i: i * 1.2 + 10)

You can leave out either the scale or the offset.

im.point(table, mode) ⇒ image

im.point(function, mode) ⇒ image

Same as above, but specifies a new mode for the output image. This can be used to convert “L” and “P” images to “1” in one step, e.g. to threshold an image.

同上,但指定了输出图像的新模式。这可以用于“L”和“P”的图像转换成“1”在一个步骤中,如阈值图像。

(New in 1.1.5) This form can also be used to convert “L” images to “I” or “F”, and to convert “I” images with 16-bit data to “L”. In the second case, you must use a 65536-item lookup table.

1.1.5(新)这种形式还可以用于“L”图像转换为“我”或“F”,和“我”具有16位的图像数据转换为“L”。在第二种情况下,您必须使用一个65536项目查找表。

  • putalpha

im.putalpha(band)

Copies the given band to the alpha layer of the current image.

The image must be an “RGBA” image, and the band must be either “L” or “1”.

将给定频带复制到当前图像的α层。图像必须是“RGBA”的形象,和乐队必须“L”或“1”。

(New in PIL 1.1.5) You can use putalpha on other modes as well; the image is converted in place, to a mode that matches the current mode but has an alpha layer (this usually means “LA” or “RGBA”). Also, the band argument can be either an image, or a colour value (an integer).

在库1.1.5(新)可以使用putalpha其他模式;图像的转换,模式匹配当前的模式,但有一个α层(这通常意味着“拉”或“RGBA”)。同时,乐队参数可以是一个图像,或者颜色值(整数)。

  • putdata

im.putdata(data)

im.putdata(data, scale, offset)

Copy pixel values from a sequence object into the image, starting at the upper left corner (0, 0). The scale and offset values are used to adjust the sequence values:

将像素值从一个序列对象复制到图像,从左上角开始(0,0)。规模和偏移量的值是用来调整序列值:

pixel = value * scale + offset

If the scale is omitted, it defaults to 1.0. If the offset is omitted, it defaults to 0.0.

如果省略的规模,它默认为1.0。如果省略偏移量,它默认为0.0。

  • putpalette

im.putpalette(sequence)

Attach a palette to a “P” or “L” image. For an “L” image, the mode is changed to “P”. The palette sequence should contain 768 integer values, where each group of three values represent the red, green, and blue values for the corresponding pixel index. Instead of an integer sequence, you can use a 768-byte string.

附加一个面板“P”或“L”形象。对于一个“L”的形象,“P”的模式改变了。面板序列应该包含768整数值,每组三个值代表了红、绿、蓝值对应的像素指数。而不是一个整数序列,你可以使用一个768字节的字符串。

  • putpixel

im.putpixel(xy, colour)

Modifies the pixel at the given position. The colour is given as a single numerical value for single-band images, and a tuple for multi-band images.

修改像素在给定的位置。给出的颜色作为一个单一的数值为单波段图像,和多波段图像的元组。

Note that this method is relatively slow. If you’re using 1.1.6, pixel access objects (see load) provide a faster way to modify the image. If you want to generate an entire image, it can be more efficient to create a Python list and use putdata to copy it to the image. For more extensive changes, use paste or the ImageDraw module instead.

注意,这种方法相对较慢。如果你使用1.1.6,像素访问对象(参见负载)提供一种更快的方式来修改图像。如果你想生成整个图像,它可以更有效的创建Python列表并使用putdata复制图像。对于更广泛的变化,使用粘贴或ImageDraw模块。

You can speed putpixel up a bit by “inlining” the call to the internal putpixel implementation method:

你可以加速putpixel有点“内联”调用内部putpixel实现方法:

im.load()
    putpixel = im.im.putpixel
    for i in range(n):
       ...
       putpixel((x, y), value)

In 1.1.6, the above is better written as:

pix = im.load()
    for i in range(n):
        ...
        pix[x, y] = value
  • quantize

**im.quantize(colors, options) ⇒ image

(Deprecated) Converts an “L” or “RGB” image to a “P” image with the given number of colors, and returns the new image.

(弃用)转换为一个“L”或“RGB图像到一个“P”的形象与给定数量的颜色,并返回新形象。

For new code, use convert with a adaptive palette instead:

对于新代码,使用* convert *与自适应面板:

out = im.convert("P", palette=Image.ADAPTIVE, colors=256)
  • resize

im.resize(size) ⇒ image

im.resize(size, filter) ⇒ image

Returns a resized copy of an image. The size argument gives the requested size in pixels, as a 2-tuple: (width, height).

返回一个缩放图像的副本。尺寸参数给请求的大小(以像素为单位),作为一个二元数组:(宽度、高度)。

The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.

滤波器参数可以是一个最近的(使用近邻),双线性(2 x2环境中的线性插值),双三次的(三次样条插值在4 x4环境),或平滑(高质量downsampling过滤器)。如果省略,或者图像模式“1”或“P”,这是最近的。

Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for large downsampling ratios (e.g. when creating thumbnails). You should use ANTIALIAS unless speed is much more important than quality.

注意,双线性和双三次的过滤器在当前版本的库并不适合大型downsampling比率(例如,当创建缩略图)。您应该使用抗锯齿,除非速度比质量更重要。

  • rotate

im.rotate(angle) ⇒ image

im.rotate(angle, filter=NEAREST, expand=0) ⇒ image

Returns a copy of an image rotated the given number of degrees counter clockwise around its centre.

返回一个给定数量的复制图像的旋转度逆时针方向绕着它的中心。

The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), or BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.

滤波器参数可以是一个最近的(使用近邻),双线性(2 x2环境中的线性插值),或双三次的(三次样条插值在4 x4环境)。如果省略,或者图像模式“1”或“P”,这是最近的。

The expand argument, if true, indicates that the output image should be made large enough to hold the rotated image. If omitted or false, the output image has the same size as the input image.

扩大的论点,如果这是真的,表明输出图像应该足够容纳旋转图像。如果遗漏或错误,输出图像具有相同的大小作为输入图像。

  • save

im.save(outfile, options…)

im.save(outfile, format, options…)

Saves the image under the given filename. If format is omitted, the format is determined from the filename extension, if possible. This method returns None.

节省下的图像文件名。如果省略格式,该格式文件扩展名的确定,如果可能的话。该方法返回None。

Keyword options can be used to provide additional instructions to the writer. If a writer doesn’t recognise an option, it is silently ignored. The available options are described later in this handbook.

关键字选项可用于提供额外的指令的作家。如果一个作家不承认一个选项,它默默地忽略。在本手册描述的可用选项。

You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode.

您可以使用一个文件对象,而不是一个文件名。在这种情况下,您必须指定格式。告诉,文件对象必须实现寻求和写作方法,是在二进制模式下打开。

If the save fails, for some reason, the method will raise an exception (usually an IOError exception). If this happens, the method may have created the file, and may have written data to it. It’s up to your application to remove incomplete files, if necessary.

如果保存失败,由于某种原因,该方法将引发一个异常(通常一个IOError异常那么)。如果发生这种情况,该方法可能会创建这个文件,并有可能写数据。由您的应用程序将不完整的文件,如果必要的。

  • seek

im.seek(frame)

Seeks to the given frame in a sequence file. If you seek beyond the end of the sequence, the method raises an EOFError exception. When a sequence file is opened, the library automatically seeks to frame 0.

寻求给定的帧序列文件中。如果你寻求超越的序列,提出了一个方法EOFError例外。当一个序列文件打开,图书馆自动寻求框架0。

Note that in the current version of the library, most sequence formats only allows you to seek to the next frame.

注意,在当前版本的库,大多数序列格式只允许你寻求下一帧。

  • show

im.show()

Displays an image. This method is mainly intended for debugging purposes.

显示一个图像。这种方法主要用于调试目的。

On Unix platforms, this method saves the image to a temporary PPM file, and calls the xv utility.

在Unix平台上,这种方法保存图像到一个临时PPM文件,并调用十五的效用。

On Windows, it saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.

在Windows上,它保存图像到一个临时BMP文件,并使用标准的显示BMP效用。

This method returns None.

该方法返回None。

作者标识没有Mac很绝望。。/(ㄒoㄒ)/~~

  • split

im.split() ⇒ sequence

Returns a tuple of individual image bands from an image. For example, splitting an “RGB” image creates three new images each containing a copy of one of the original bands (red, green, blue).

返回一个元组的个人形象乐队的形象。例如,将一个“RGB图像创建三个新的图像每个包含一份最初的乐队之一(红、绿、蓝)。

  • tell

im.tell() ⇒ integer

Returns the current frame number.

返回当前帧数。

  • thumbnail

im.thumbnail(size)

im.thumbnail(size, filter)

Modifies the image to contain a thumbnail version of itself, no larger than the given size. This method calculates an appropriate thumbnail size to preserve the aspect of the image, calls the draft method to configure the file reader (where applicable), and finally resizes the image.

修改图像包含的缩略版本身,不超过给定的大小。这种方法计算出一个合适的图像的缩略图大小保存方面,草案方法的调用配置文件阅读器(如适用),最后调整图像的大小。

The filter argument can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS (best quality). If omitted, it defaults to NEAREST.

滤波器参数可以最近的之一,双线性、双三次的,或平滑(质量)。如果省略,默认为最近的。

Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for thumbnail generation. You should use ANTIALIAS unless speed is much more important than quality.

注意,双线性和双三次的过滤器在当前版本的库并不适合缩略图生成。您应该使用抗锯齿,除非速度比质量更重要。

Also note that this function modifies the Image object in place. If you need to use the full resolution image as well, apply this method to a copy of the original image. This method returns None.

还要注意,这个函数修改图像对象。如果你需要使用完整的分辨率图像,这种方法适用于原始图像的一个副本。该方法返回None。

  • tobitmap

im.tobitmap() ⇒ string

Returns the image converted to an X11 bitmap.

返回图像转换为一个X11位图。

  • tostring

im.tostring() ⇒ string

Returns a string containing pixel data, using the standard “raw” encoder.

返回一个字符串,其中包含像素数据,使用标准的“原始”编码器。

im.tostring(encoder, parameters) ⇒ string

Returns a string containing pixel data, using the given data encoding.

返回一个字符串,其中包含像素数据,使用给定的数据编码。

Note: The tostring method only fetches the raw pixel data. To save the image to a string in a standard file format, pass a StringIO object (or equivalent) to the save method.

注意:tostring方法只获取原始像素数据。将图像保存到一个字符串在标准文件格式,通过StringIO对象(或同等)来保存方法。

  • transform

im.transform(size, method, data) ⇒ image

im.transform(size, method, data, filter) ⇒ image

Creates a new image with the given size, and the same mode as the original, and copies data to the new image using the given transform.

用给定的大小,创建一个新的形象和与原来相同的模式,将数据复制到新的图像使用给定的变换。

In the current version of PIL, the method argument can be EXTENT (cut out a rectangular subregion), AFFINE (affine transform), QUAD (map a quadrilateral to a rectangle), MESH (map a number of source quadrilaterals in one operation), or PERSPECTIVE. The various methods are described below.

在当前版本的库,可以将方法参数范围(剪出一个矩形区),仿射(仿射变换),四(四边形映射到一个矩形),网格(映射源四边形的操作),或观点。下面描述的各种方法。

The filter argument defines how to filter pixels from the source image. In the current version, it can be NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), or BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.

滤波器参数定义了如何从源图像像素过滤。在当前版本中,可以是最近的(用近邻),双线性(2 x2环境中的线性插值),或双三次的(三次样条插值在4 x4环境)。如果省略,或者图像模式“1”或“P”,这是最近的。

im.transform(size, EXTENT, data) ⇒ image

im.transform(size, EXTENT, data, filter) ⇒ image

Extracts a subregion from the image.

从图像中提取一个亚区。

Data is a 4-tuple (x0, y0, x1, y1) which specifies two points in the input image’s coordinate system. The resulting image will contain data sampled from between these two points, such that (x0, y0) in the input image will end up at (0,0) in the output image, and (x1, y1) at size.

数据是一个4-tuple (x0, y0 (x1, y1)指定输入图像中两个点的坐标系统。由此产生的图像将从这两点之间包含数据采样,这样(x0, y0)在输入图像最终将在输出图像(0,0),和(x1, y1)的大小。

This method can be used to crop, stretch, shrink, or mirror an arbitrary rectangle in the current image. It is slightly slower than crop, but about as fast as a corresponding resize operation.

这种方法可用于作物,伸展、收缩或镜任意矩形在当前图像。是略低于作物,而是尽快相应的调整操作。

im.transform(size, AFFINE, data) ⇒ image

im.transform(size, AFFINE, data, filter) ⇒ image

Applies an affine transform to the image, and places the result in a new image with the given size.

适用于图像的仿射变换,将导致一个新形象与给定的大小。

Data is a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f) in the input image, rounded to nearest pixel.

数据是一个6-tuple (a, b, c, d, e, f)包含前两行从一个仿射变换矩阵。对于每个像素(x, y)在输出图像,新价值从一个位置(x + b + c, d e x + y + f)在输入图像,圆形的最近的像素。

This function can be used to scale, translate, rotate, and shear the original image.

这个函数可以用于规模、翻译、旋转和剪切原始图像。

im.transform(size, QUAD, data) ⇒ image

im.transform(size, QUAD, data, filter) ⇒ image

Maps a quadrilateral (a region defined by four corners) from the image to a rectangle with the given size.

地图一个四边形(区域定义为四个角)的形象与给定的一个矩形的大小。

Data is an 8-tuple (x0, y0, x1, y1, x2, y2, y3, y3) which contain the upper left, lower left, lower right, and upper right corner of the source quadrilateral.

数据是一个8-tuple (x0, y0 (x1, y1, x2, y2, y3, y3)含有左上角,左下角,右上角右下方,四边形的来源。

im.transform(size, MESH, data) image ⇒ image

im.transform(size, MESH, data, filter) image ⇒ image

Similar to QUAD, but data is a list of target rectangles and corresponding source quadrilaterals.

类似于四,但数据列表的相应目标矩形和源的四边形。

im.transform(size, PERSPECTIVE, data) image ⇒ image

im.transform(size, PERSPECTIVE, data, filter) image ⇒ image

Applies a perspective transform to the image, and places the result in a new image with the given size.

透视变换适用于形象,将导致一个新形象与给定的大小。

Data is a 8-tuple (a, b, c, d, e, f, g, h) which contains the coefficients for a perspective transform. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c)/(g x + h y + 1), (d x + e y + f)/(g x + h y + 1) in the input image, rounded to nearest pixel.

数据是一个8-tuple (a, b, c, d, e, f, g, h)包含透视变换的系数。对于每个像素(x, y)在输出图像,新价值从一个位置(x + b + c) / (g h x + y + 1) (d e x + y + f) / (g h x + y + 1)在输入图像,圆形的最近的像素。

This function can be used to change the 2D perspective of the original image.

这个函数可以用来改变原始图像的二维角度。

  • transpose

im.transpose(method) ⇒ image

Returns a flipped or rotated copy of an image.

返回一个翻转或旋转图像的副本。

Method can be one of the following: FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, or ROTATE_270.

方法可以是下列之一:。。。

  • verify

im.verify()

Attempts to determine if the file is broken, without actually decoding the image data. If this method finds any problems, it raises suitable exceptions. This method only works on a newly opened image; if the image has already been loaded, the result is undefined. Also, if you need to load the image after using this method, you must reopen the image file.

尝试确定文件坏了,没有实际解码图像数据。如果这种方法发现任何问题,它提出了合适的异常。这种方法只适用于新开的形象;如果已经加载的图片,结果是未定义的。同样的,如果你需要加载图像使用这种方法后,你必须重新打开图像文件。

Note that this method doesn’t catch all possible errors; to catch decoding errors, you may have to load the entire image as well.

注意,这个方法没有抓住所有可能的错误;赶上解码错误,你可能需要加载整个图像。

Attributes

Instances of the Image class have the following attributes:

图像类的实例有以下属性:

  • format

im.format ⇒ string or None

The file format of the source file. For images created by the library itself (via a factory function, or by running a method on an existing image), this attribute is set to None.

源文件的文件格式。对图像库本身创建的(通过一个工厂函数,或通过运行方法现有图像),这个属性设置为None。

  • mode

im.mode ⇒ string

Image mode. This is a string specifying the pixel format used by the image. Typical values are “1”, “L”, “RGB”, or “CMYK.” See Concepts for a full list.

图像模式。这是一个字符串指定像素格式所使用的图像。典型值“1”,“L”、“RGB”,或“CMYK。

  • size

im.size ⇒ (width, height)

Image size, in pixels. The size is given as a 2-tuple (width, height).

图像大小,以像素为单位。是作为二元数组大小(宽度、高度)。

  • palette

im.palette ⇒ palette or None

Colour palette table, if any. If mode is “P”, this should be an instance of the ImagePalette class. Otherwise, it should be set to None.

颜色表,如果任何。如果“P”模式,这应该是的一个实例ImagePalette类。否则,它应该设置为None。

  • info

im.info ⇒ dictionary

A dictionary holding data associated with the image. This dictionary is used by file handlers to pass on various non-image information read from the file. See documentation for the various file handlers for details.

字典数据与图像。这本字典文件处理程序通过使用各种非信息读取该文件。有关详细信息,请参阅文档等文件处理程序。

Most methods ignore the dictionary when returning new images; since the keys are not standardized, it’s not possible for a method to know if the operation affects the dictionary. If you need the information later on, keep a reference to the info dictionary returned from the open method.

大多数方法忽略了字典当返回新的图像;自钥匙不是标准化的,这是不可能的知道操作方法影响了字典。如果你需要的信息之后,请引用open的方法。

下一篇:053-python库PIL(三)

你可能感兴趣的:(052-python库PIL(二))