上一篇:052-python库PIL(二)
一、The ImageChops Module(图像印章)
chop 美 [tʃɑp]
n. 砍;排骨;商标;削球
vt. 剁碎;砍
ImageChops可以说是图像排,图像砍。图像印章是比较好听的一个叫法。
The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more.
ImageChops模块包含许多算术图像操作,称为通道操作(“公章”)。这些可以用于各种目的,包括特殊效果,图像组成,算法画等等。
For more pre-made operations, see ImageOps.
预先做好的业务,看到ImageOps。
At this time, most channel operations are only implemented for 8-bit images (e.g. “L” and “RGB”).
在这个时候,很多渠道操作的实现只有8位图像(例如“L”和“RGB”)。
Functions
Most channel operations take one or two image arguments and returns a new image. Unless otherwise noted, the result of a channel operation is always clipped to the range 0 to MAX (which is 255 for all modes supported by the operations in this module).
大多数通道操作一个或两个图像参数并返回一个新形象。除非特别指出,一个通道操作的结果总是剪0到最大范围(这是255年所有模式支持的操作在本模块)。
- constant (常数)
ImageChops.constant(image, value) ⇒ image
Return a layer with the same size as the given image, but filled with the given pixel value.
返回一个层具有相同大小给定的图像,但充满了给定的像素值。
- duplicate(重复的)
ImageChops.duplicate(image) ⇒ image
Return a copy of the given image.
返回一个给定图像的副本。
- invert(反转)
ImageChops.invert(image) ⇒ image
Inverts an image.
反转一个图像。
out = MAX - image
- lighter(更轻的)
ImageChops.lighter(image1, image2) ⇒ image
Compares the two images, pixel by pixel, and returns a new image containing the lighter values.
比较两个图像,像素到像素,并返回一个包含轻价值的新形象。
out = max(image1, image2)
- darker(深)
ImageChops.darker(image1, image2) ⇒ image
Compares the two images, pixel by pixel, and returns a new image containing the darker values.
比较两个图像,像素到像素,并返回一个包含深值的新形象。
out = min(image1, image2)
- difference(区别)
ImageChops.difference(image1, image2) ⇒ image
Returns the absolute value of the difference between the two images.
返回两个图像之间的差异的绝对值。
out = abs(image1 - image2)
- multiply(乘)
ImageChops.multiply(image1, image2) ⇒ image
Superimposes two images on top of each other. If you multiply an image with a solid black image, the result is black. If you multiply with a solid white image, the image is unaffected.
添加两个图像的顶部。如果你用一个图像与一个坚实的黑人形象,结果是黑色的。如果你用白色固体图像,图像不受影响。
out = image1 * image2 / MAX
- screen(屏幕)
ImageChops.screen(image1, image2) ⇒ image
Superimposes two inverted images on top of each other.
添加两个反向图像的顶部。
out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
- add(添加)
ImageChops.add(image1, image2, scale, offset) ⇒ image
Adds two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0.
添加了两个图像,分割结果通过规模和添加偏移量。如果省略,默认值为1.0,0.0和抵消。
out = (image1 + image2) / scale + offset
- subtract(减去)
ImageChops.subtract(image1, image2, scale, offset) ⇒ image
Subtracts two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0.
减去两个图像,分割结果通过规模和添加偏移量。如果省略,默认值为1.0,0.0和抵消。
out = (image1 - image2) / scale + offset
- blend(混合)
ImageChops.blend(image1, image2, alpha) ⇒ image
Same as the blend function in the Image module.
一样的混合函数图像模块。
- composite(复合)
ImageChops.composite(image1, image2, mask) ⇒ image
Same as the composite function in the Image module.
一样的复合函数在图像模块。
- offset(抵消)
ImageChops.offset(image, xoffset, yoffset) ⇒ image
ImageChops.offset(image, offset) ⇒ image
Returns a copy of the image where 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.
返回一个副本的图像数据已经抵消了给定的距离。数据封装边缘。如果省略yoffset,它被认为是等于xoffset。
二、The ImageColor Module(ImageColor模块)
The ImageColor module contains colour tables and converters from CSS3-style colour specifiers to RGB tuples. This module is used by Image.new and the ImageDraw module, among others.
ImageColor模块包含的颜色表和转换器从CSS3-style色彩说明符RGB元组。这个模块使用图片。新和ImageDraw模块等等。
- Colour Names(颜色名称)
The ImageColor module supports the following string formats:
ImageColor模块支持以下字符串格式:
Hexadecimal color specifiers, given as “#rgb” or “#rrggbb”. For example, “#ff0000” specifies pure red.
十六进制颜色说明符,给出“# rgb”或“# rrggbb”。例如,“# ff0000”指定纯红色。
RGB functions, given as “rgb(red, green, blue)” where the colour values are integers in the range 0 to 255. Alternatively, the color values can be given as three percentages (0% to 100%). For example, “rgb(255,0,0)” and “rgb(100%,0%,0%)” both specify pure red.
RGB函数,给出“RGB(红、绿、蓝)”的颜色值范围在0到255的整数。另外,颜色值可以作为三个百分比(0%对100%)。例如,“rgb(255, 0, 0)”和“rgb(100%、0%、0%)”两个指定纯红色。
Hue-Saturation-Lightness (HSL) functions, given as “hsl(hue, saturation%, lightness%)” where hue is the colour given as an angle between 0 and 360 (red=0, green=120, blue=240), saturation is a value between 0% and 100% (gray=0%, full color=100%), and lightness is a value between 0% and 100% (black=0%, normal=50%, white=100%). For example, “hsl(0,100%,50%)” is pure red.
Hue-Saturation-Lightness(高速逻辑)函数,称为“奥软(色相,饱和度%,轻盈%)”,色调是0到360之间的颜色作为一个角度(红色= 0,绿色= 120,蓝色= 240),饱和是一个值在0%和100%之间(灰色= 0%,全彩色= 100%),和明度值在0%和100%之间(黑= 0%,正常= 50%,白色= 100%)。例如,“奥软(0100%、50%)”是纯粹的红色。
Common HTML colour names. The ImageColor module provides some 140 standard colour names, based on the colors supported by the X Window system and most web browsers. Colour names are case insensitive. For example, “red” and “Red” both specify pure red.
常见的HTML色彩的名字。ImageColor模块提供了一些140标准颜色名称,支持的基于颜色的X窗口系统和大多数web浏览器。颜色名称不区分大小写。例如,“红色”和“红色”指定纯红色。
Functions
- getrgb(获取rgb)
getrgb(color) ⇒ (red, green, blue)
(New in 1.1.4) Convert a colour string to an RGB tuple. If the string cannot be parsed, this function raises a ValueError exception.
1.1.4(新)颜色的字符串转换为RGB元组。如果无法解析字符串,这个函数提出了一个ValueError例外。
- getcolor
getcolor(color, mode) ⇒ (red, green, blue) or integer
(New in 1.1.4) Same as getrgb, but converts the RGB value to a greyscale value if the mode is not color or a palette image. If the string cannot be parsed, this function raises a ValueError exception.
1.1.4(新)getrgb一样,但是将RGB值转换为灰度值如果没有颜色模式或调色板图像。如果无法解析字符串,这个函数提出了一个ValueError例外。
三、The ImageDraw Module(ImageDraw模块)
The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.
ImageDraw模块提供简单的2 d图形图像对象。您可以使用这个模块创建新的图像,注释或修整现有图片,为web使用动态生成图形。
For a more advanced drawing library for PIL, see The aggdraw Module.
为一个更高级的图纸库库,看到aggdraw模块。
Example
Draw a Grey Cross Over an Image
import Image, ImageDraw
im = Image.open("lena.pgm")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw
# write to stdout
im.save(sys.stdout, "PNG")
Concepts(概念)
- Coordinates(坐标)
The graphics interface uses the same coordinate system as PIL itself, with (0, 0) in the upper left corner.
图形界面和库本身使用相同的坐标系统,与(0,0)在左上角。
- Colours(颜色)
To specify colours, you can use numbers or tuples just as you would use with Image.new or Image.putpixel. For “1”, “L”, and “I” images, use integers. For “RGB” images, use a 3-tuple containing integer values. For “F” images, use integer or floating point values.
指定颜色,您可以使用数字或元组就像使用图像。新的或Image.putpixel。为“1”、“L”和“我”的图像,使用整数。对于“RGB图像,使用包含包含整数值。对于“F”的图像,使用整数或浮点值。
For palette images (mode “P”), use integers as colour indexes. In 1.1.4 and later, you can also use RGB 3-tuples or colour names (see below). The drawing layer will automatically assign colour indexes, as long as you don’t draw with more than 256 colours.
调色板图像(“P”模式),使用整数作为颜色索引。1.1.4和后,您还可以使用RGB包含或颜色名称(见下文)。绘图层会自动分配颜色索引,只要你不画有超过256种颜色。
- Colour Names (颜色名称)
In PIL 1.1.4 and later, you can also use string constants when drawing in “RGB” images. PIL supports the following string formats:
在库1.1.4和后,您还可以使用字符串常量在画“RGB图像。库支持以下字符串格式:
Hexadecimal color specifiers, given as “#rgb” or “#rrggbb”. For example, “#ff0000” specifies pure red.
十六进制颜色说明符,给出“# rgb”或“# rrggbb”。例如,“# ff0000”指定纯红色。
RGB functions, given as “rgb(red, green, blue)” where the colour values are integers in the range 0 to 255. Alternatively, the color values can be given as three percentages (0% to 100%). For example, “rgb(255,0,0)” and “rgb(100%,0%,0%)” both specify pure red.
RGB函数,给出“RGB(红、绿、蓝)”的颜色值范围在0到255的整数。另外,颜色值可以作为三个百分比(0%对100%)。例如,“rgb(255, 0, 0)”和“rgb(100%、0%、0%)”两个指定纯红色。
Hue-Saturation-Lightness (HSL) functions, given as “hsl(hue, saturation%, lightness%)” where hue is the colour given as an angle between 0 and 360 (red=0, green=120, blue=240), saturation is a value between 0% and 100% (gray=0%, full color=100%), and lightness is a value between 0% and 100% (black=0%, normal=50%, white=100%). For example, “hsl(0,100%,50%)” is pure red.
Hue-Saturation-Lightness(高速逻辑)函数,称为“奥软(色相,饱和度%,轻盈%)”,色调是0到360之间的颜色作为一个角度(红色= 0,绿色= 120,蓝色= 240),饱和是一个值在0%和100%之间(灰色= 0%,全彩色= 100%),和明度值在0%和100%之间(黑= 0%,正常= 50%,白色= 100%)。例如,“奥软(0100%、50%)”是纯粹的红色。
Common HTML colour names. The ImageDraw provides some 140 standard colour names, based on the colors supported by the X Window system and most web browsers. Colour names are case insensitive, and may contain whitespace. For example, “red” and “Red” both specify pure red.
常见的HTML色彩的名字。ImageDraw提供了一些140标准颜色名称,支持的基于颜色的X窗口系统和大多数web浏览器。颜色名称区分大小写,可以包含空格。例如,“红色”和“红色”指定纯红色。
- Fonts(字体)
PIL can use bitmap fonts or OpenType/TrueType fonts.
库可以使用位图字体或OpenType / TrueType字体。
Bitmap fonts are stored in PIL’s own format, where each font typically consists of a two files, one named .pil and the other usually named .pbm. The former contains font metrics, the latter raster data.
点阵字体存储在库的格式,每个字体通常包含两个文件,一个名叫.pil,另通常.pbm命名。前者包含字体度量,后者栅格数据。
To load a bitmap font, use the load functions in the ImageFont module.
加载位图字体,使用在ImageFont模块加载函数。
To load a OpenType/TrueType font, use the truetype function in the ImageFont module. Note that this function depends on third-party libraries, and may not available in all PIL builds.
负载的OpenType / TrueType字体,使用ImageFont TrueType功能模块。注意,这个函数依赖的第三方库,并可能没有覆盖所有库构建。
(IronPIL) To load a built-in font, use the Font constructor in the ImageFont module.
(IronPIL)加载一个内置的字体,字体使用构造函数在ImageFont模块。
Functions
- Draw(画)
Draw(image) ⇒ Draw instance
Creates an object that can be used to draw in the given image.
创建一个对象,可以用来画在给定的图像。
(IronPIL) Instead of an image, you can use HWND or HDC objects from the ImageWin module. This allows you to draw directly to the screen.
(IronPIL)而不是一个图像,您可以使用HWND或从ImageWin HDC对象模块。这允许您直接绘制到屏幕上。
Note that the image will be modified in place.
注意,图像将被修改。
Methods(方法)
- arc
draw.arc(xy, start, end, options)
Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box.
画一个弧(圆轮廓的一部分)的开始和结束之间的角度,在给定的边界框。
The outline option gives the colour to use for the arc.
大纲选择了颜色使用弧。
- bitmap
draw.bitmap(xy, bitmap, options)
Draws a bitmap (mask) at the given position, using the current fill colour for the non-zero portions. The bitmap should be a valid transparency mask (mode “1”) or matte (mode “L” or “RGBA”).
绘制位图(面具)在给定的位置,使用当前的非零部分填充颜色。位图应该是一个有效的透明蒙版(模式“1”)或哑光(模式“L”或“RGBA”)。
This is equivalent to doing image.paste(xy, color, bitmap).
这相当于做形象。粘贴(xy,颜色,位图)。
To paste pixel data into an image, use the paste method on the image itself.
像素数据粘贴到一个图像,利用图像上的粘贴方法本身。
- chord
draw.chord(xy, start, end, options)
Same as arc, but connects the end points with a straight line.
一样的弧,但连接端点和一条直线。
The outline option gives the colour to use for the chord outline. The fill option gives the colour to use for the chord interior.
大纲选择了色彩使用的和弦大纲。填充选项给和弦室内的颜色使用。
- ellipse
draw.ellipse(xy, options)
Draws an ellipse inside the given bounding box.
画一个椭圆内给定的边界框。
The outline option gives the colour to use for the ellipse outline. The fill option gives the colour to use for the ellipse interior.
大纲选择椭圆轮廓的颜色使用。填充选项给出了颜色对椭圆内部使用。
- line
draw.line(xy, options)
Draws a line between the coordinates in the xy list.
在xy坐标系之间画一条线。
The coordinate list can be any sequence object containing either 2-tuples [ (x, y), … ] or numeric values [ x, y, … ]. It should contain at least two coordinates.
可以任何序列坐标列表对象包含集合[(x, y)…]或数值(x, y,…)。它应该包含至少两个坐标。
The fill option gives the colour to use for the line.
填充选项给线的颜色使用。
(New in 1.1.5) The width option gives the line width, in pixels. Note that line joins are not handled well, so wide polylines will not look good.
1.1.5(新)宽度的选择给出了线宽度,以像素为单位。注意,线连接处理得不是很好,所以宽折线会不好看。
Note: The width option is broken in 1.1.5. The line is drawn with twice the width you specify. This will be fixed in 1.1.6.
注意:1.1.5宽度的选择是破碎的。画的线带您指定宽度的两倍。1.1.6这将是固定的。
- pieslice
draw.pieslice(xy, start, end, options)
Same as arc, but also draws straight lines between the end points and the center of the bounding box.
一样的弧,也吸引了直线端点和边界框的中心。
The outline option gives the colour to use for the pieslice outline. The fill option gives the colour to use for the pieslice interior.
概述选项给pieslice轮廓的颜色使用。填充选项给pieslice室内的颜色使用。
- point
draw.point(xy, options)
Draws points (individual pixels) at the given coordinates.
吸引点(每个像素)在给定的坐标。
The coordinate list can be any sequence object containing either 2-tuples [ (x, y), … ] or numeric values [ x, y, … ].
可以任何序列坐标列表对象包含集合[(x, y)…]或数值(x, y,…)。
The fill option gives the colour to use for the points.
填充选项给点的颜色使用。
- polygon
draw.polygon(xy, options)
Draws a polygon.
画一个多边形。
The polygon outline consists of straight lines between the given coordinates, plus a straight line between the last and the first coordinate.
给定的多边形轮廓由直线之间的坐标,加上去年第一个坐标之间的直线。
The coordinate list can be any sequence object containing either 2-tuples [ (x, y), … ] or numeric values [ x, y, … ]. It should contain at least three coordinates.
可以任何序列坐标列表对象包含集合[(x, y)…]或数值(x, y,…)。它应该包含至少三个坐标。
The outline option gives the colour to use for the polygon outline. The fill option gives the colour to use for the polygon interior.
大纲的选择给出了多边形轮廓的颜色使用。填充选项给多边形内部的颜色使用。
- rectangle
draw.rectangle(box, options)
Draws a rectangle.
绘制一个矩形。
The box can be any sequence object containing either 2-tuples [ (x, y), (x, y) ] or numeric values [ x, y, x, y ]. It should contain two coordinates.
盒子可以任何序列对象包含集合[(x, y) (x, y)]或数值(x, y, x, y)。它应该包含两个坐标。
Note that the second coordinate pair defines a point just outside the rectangle, also when the rectangle is not filled.
注意第二个坐标定义一个点就在矩形,矩形时也没有了。
The outline option gives the colour to use for the rectangle outline. The fill option gives the colour to use for the rectangle interior.
大纲选择了色彩使用的矩形轮廓。填充选项给用于矩形内部的色彩。
- text
draw.text(position, string, options)
Draws the string at the given position. The position gives the upper left corner of the text.
在给定位置的字符串。这个职位给了文本的左上角。
The font option is used to specify which font to use. It should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.
字体选项用于指定要使用的字体。它应该ImageFont类的一个实例,通常从文件加载使用ImageFont模块的加载方法。
The fill option gives the colour to use for the text.
填充选项给使用的文本颜色。
- textsize
draw.textsize(string, options) ⇒ (width, height)
Return the size of the given string, in pixels.
返回给定字符串的大小,像素。
The font option is used to specify which font to use. It should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.
字体选项用于指定要使用的字体。它应该ImageFont类的一个实例,通常从文件加载使用ImageFont模块的加载方法。
Options(选项)
- outline(大纲)
outline integer or tuple
大纲整数或元组
- fill(填满)
fill integer or tuple
填整数或元组
- font(字体)
font ImageFont instance
字体
Compatibility(兼容性)
The Draw class contains a constructor and a number of methods which are provided for backwards compatibility only. For this to work properly, you should either use options on the drawing primitives, or these methods. Do not mix the old and new calling conventions.
画类包含一个构造函数和方法只提供向后兼容性。正常工作,您应该使用期权画原语,或这些方法。不要将新旧调用约定。
(IronPIL) The compatibility methods are not supported by IronPIL.
(IronPIL)兼容性IronPIL不支持的方法。
- ImageDraw
ImageDraw(image) ⇒ Draw instance
(Deprecated). Same as Draw. Don’t use this name in new code.
(弃用)。画一样。不要在新代码中使用这个名字。
- setink
draw.setink(ink)
(Deprecated). Sets the colour to use for subsequent draw and fill operations.
(弃用)。将颜色设置为用于随后的绘制和填充操作。
- setfill
draw.setfill(mode)
(Deprecated). Sets the fill mode.
(弃用)。设置填充模式。
If the mode is 0, subsequently drawn shapes (like polygons and rectangles) are outlined. If the mode is 1, they are filled.
如果该模式为0,随后形状(如多边形和矩形)了。如果模式是1,填满。
- setfont
draw.setfont(font)
(Deprecated). Sets the default font to use for the text method.
(弃用)。设置默认字体用于文本的方法。
The font argument should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.
字体参数应该是ImageFont类的实例,通常从文件加载使用ImageFont模块的加载方法。
四、The ImageEnhance Module(ImageEnhance模块)(图像增强模块)
The ImageEnhance module contains a number of classes that can be used for image enhancement.
ImageEnhance模块包含许多类,可用于图像增强。
Example
Vary the Sharpness of an Image
改变图像的清晰度
import ImageEnhance
enhancer = ImageEnhance.Sharpness(image)
for i in range(8):
factor = i / 4.0
enhancer.enhance(factor).show("Sharpness %f" % factor)
Also see the enhancer.py demo program in the Scripts directory.
也看到增强剂。py脚本目录中的演示程序。
Interface
All enhancement classes implement a common interface, containing a single method:
所有增强类实现一个共同的接口,包含一个方法:
enhancer.enhance(factor) ⇒ image
Returns an enhanced image. The factor is a floating point value controlling the enhancement. Factor 1.0 always returns a copy of the original image, lower factors mean less color (brightness, contrast, etc), and higher values more. There are no restrictions on this value.
返回一个增强的图像。因素是一个浮点值控制增强。因素1.0总是返回原始图像的一个副本,较低的因素意味着更少的颜色(亮度、对比度等),和更高的价值。没有任何限制这个值。
- The Color Class (颜色类)
The color enhancement class is used to adjust the color balance of an image, in a manner similar to the controls on a color TV set. This class implements the enhancement interface as described above.
颜色增强类是用于调整图像的色彩平衡,在某种程度上类似于彩色电视机的控制。这个类实现增强界面如上所述。
ImageEnhance.Color(image) ⇒ Color enhancer instance
Creates an enhancement object for adjusting color in an image. A factor of 0.0 gives a black and white image, a factor of 1.0 gives the original image.
创建一个图像增强对象调整颜色。0.0倍给黑白图像,1.0倍给原始图像。
- The Brightness Class(亮度类)
The brightness enhancement class is used to control the brightness of an image.
亮度增强类是用于控制图像的亮度。
ImageEnhance.Brightness(image) ⇒ Brightness enhancer instance
Creates an enhancement object for adjusting brightness in an image. A factor of 0.0 gives a black image, factor 1.0 gives the original image.
创建一个增强对象调整亮度的图像。0.0倍给黑人形象,系数1.0给出了原始图像。
- The Contrast Class(对比类)
The contrast enhancement class is used to control the contrast of an image, similar to the contrast control on a TV set.
对比度增强类是用来控制的对比图片,类似于对比控制电视机。
ImageEnhance.Contrast(image) ⇒ Contrast enhancer instance
Creates an enhancement object for adjusting contrast in an image. A factor of 0.0 gives an solid grey image, factor 1.0 gives the original image.
创建一个增强对象调整对比度的图像。0.0倍了坚实的灰色图像,系数1.0给出了原始图像。
- The Sharpness Class(锐度类)
The sharpness enhancement class is used to control the sharpness of an image.
锐度增强类是用于控制图像的清晰度。
ImageEnhance.Sharpness(image) ⇒ Sharpness enhancer instance
Creates an enhancement object for adjusting the sharpness of an image. The factor 0.0 gives a blurred image, 1.0 gives the original image, and a factor of 2.0 gives a sharpened image.
创建一个增强对象调整图像的清晰度。因素0.0给出了一个模糊的形象,1.0给出了原始图像,2.0倍给磨的形象。
五、The ImageFile Module(ImageFile模块)
The ImageFile module provides support functions for the image open and save functions.
ImageFile模块提供支持功能图像打开和保存功能。
In addition, it provides a Parser class which can be used to decode an image piece by piece (e.g. while receiving it over a network connection). This class implements the same consumer interface as the standard sgmllib and xmllib modules.
此外,它提供了一个解析器类可用于解码图像一块一块的(例如,通过网络连接而接受它)。这个类实现相同的消费者接口标准sgmllib和xmllib模块。
Example
Parse An Image
解析一个图像
import ImageFile
fp = open("lena.pgm", "rb")
p = ImageFile.Parser()
while 1:
s = fp.read(1024)
if not s:
break
p.feed(s)
im = p.close()
im.save("copy.jpg")
Functions
- Parser
ImageFile.Parser() ⇒ Parser instance
Creates a parser object. Parsers cannot be reused.
创建一个解析器对象。解析器不能重用。
Methods
- feed(饲料)
parser.feed(data)
Feed a string of data to the parser. This method may raise an IOError exception.
养活一个字符串的数据解析器。这个方法可以提高一个IOError例外。
- close
parser.close() ⇒ image or None
Tells the parser to finish decoding. If the parser managed to decode an image, it returns an Image object. Otherwise, this method raises an IOError exception.
告诉解析器完成解码。如果解析器成功解码图像,它返回一个图像对象。否则,该方法提出了一个IOError例外。
Note: If the file cannot be identified, the parser will raise an IOError exception in the close method. If the file can be identified, but not decoded (for example, if the data is damaged, or if it uses an unsupported compression method), the parser will raise an IOError exception as soon as possible, either in feed or close.
注意:如果文件不能被识别,解析器将提高的IOError异常那么亲密的方法。如果文件可以被识别,但不是解码(例如,如果数据损坏,或如果它使用一个不支持的压缩方法),解析器将提高一个IOError异常尽快,那么在饲料或关闭。
六、The ImageFileIO Module(ImageFileIO模块)
The ImageFileIO module can be used to read an image from a socket, or any other stream device.
-
- ImageFileIO * *模块可以用来从套接字读取图像,或任何其他设备。
This module is deprecated. New code should use the Parser class in the ImageFile module instead.
这个模块是弃用。新代码应该使用解析器ImageFile模块。
Functions
ImageFileIO.ImageFileIO(stream)
Adds buffering to a stream file object, in order to provide seek and tell methods required by the Image.open method. The stream object must implement read and close methods.
增加了缓冲流文件对象,为了提供寻求并告诉方法所需的图像。开放的方法。流对象必须实现阅读并关闭方法。
七、The ImageFilter Module(ImageFilter模块)
The ImageFilter module contains definitions for a pre-defined set of filters, which can be be used with the filter method of the Image class.
ImageFilter模块包含定义为一组预定义的过滤器,它可以被用于图像过滤方法的类。
Example
Filter an Image
过滤器一个图像
import ImageFilter
im1 = im.filter(ImageFilter.BLUR)
im2 = im.filter(ImageFilter.MinFilter(3))
im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)
- Filters
The current version of the library provides the following set of predefined image enhancement filters:
当前版本的库提供了以下的一组预定义的图像增强过滤器:
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, and SHARPEN.
- Kernel(内核)
Kernel(size, kernel, scale=None, offset=0)
(New in 1.1.5) Creates a convolution kernel of the given size. In the current version, size must be either (3, 3) or (5, 5), and the kernel argument must be a sequence containing 9 or 25 integer or floating point weights.
1.1.5(新)创建一个卷积给定大小的内核。在当前版本中,大小必须(3、3)或(5,5),和内核参数必须是一个序列包含9个或25整数或浮点权重。
If scale is given, the result of applying the kernel to each pixel is divided by the scale value. The default is the sum of the kernel weights.
如果规模,将内核应用于每个像素的结果除以刻度值。默认的是内核权重的总和。
If offset is given, this value is added to the result, after it has been divided by scale.
如果偏移量,这个值被添加到结果,后除以规模。
- RankFilter
RankFilter(size, rank)
(New in 1.1.5) Creates a rank filter of the given size. For each pixel in the input image, the rank filter sorts all pixels in a (size, size) environment by pixel value, and copies the rank‘th value to the output image.
1.1.5(新)创建一个给定大小的过滤器。输入图像中的每个像素,排名滤波器类型(大小,大小)环境中的所有像素的像素值,并将等级对应的值复制到输出图像。
- MinFilter
MinFilter(size=3)
(New in 1.1.5) Creates a min filter of the given size. For each pixel in the input image, this filter copies the smallest pixel value from a (size, size) environment to the output image.
1.1.5(新)创建一个给定大小的最小值滤波器。输入图像的每个像素,这个过滤器复制像素最小值(尺寸、大小)环境到输出图像。
- MedianFilter
MedianFilter(size=3)
(New in 1.1.5) Creates a median filter of the given size. For each pixel in the input image, this filter copies the median pixel value from a (size, size) environment to the output image.
1.1.5(新)创建一个给定大小的中值滤波。输入图像的每个像素,这个过滤器复制像素值中值(大小、尺寸)环境到输出图像。
- MaxFilter
MaxFilter(size=3)
(New in 1.1.5) Creates a max filter of the given size. For each pixel in the input image, this filter copies the largest pixel value from a (size, size) environment to the output image.
1.1.5(新)创建一个马克斯给定大小的过滤器。输入图像的每个像素,这个过滤器复制最大像素值(尺寸、大小)环境到输出图像。
- ModeFilter
ModeFilter(size=3)
(New in 1.1.5) Creates a mode filter of the given size. For each pixel in the input image, this filter copies the most common pixel value in a (size, size) environment to the output image. If no pixel value occurs more than once, the original pixel value is used.
1.1.5(新)给定大小的过滤器创建一个模式。输入图像的每个像素,这个过滤器副本中最常见的像素值(大小、尺寸)环境到输出图像。如果没有像素值出现不止一次,使用原始的像素值。
八、The ImageFont Module(ImageFont模块)
The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the text method of the ImageDrawclass.
-
- ImageFont * *模块定义了一个具有相同名称的类。这个类的实例存储位图字体、文本和使用ImageDraw类。
PIL uses it’s own font file format to store bitmap fonts. You can use the pilfontutility to convert BDF and PCF font descriptors (X window font formats) to this format.
库使用它自己的字体文件格式存储位图字体。您可以使用[* * pilfont * *] (http://effbot.org/imagingbook/pilfont.htm)实用程序将快速公车提供和PCF字体描述符(X窗口字体格式)这种格式。
Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support is only available as part of the imToolkit package.
从版本1.1.4,库可以配置为支持TrueType和OpenType字体(以及其他字体格式支持的FreeType的库)。早期版本,TrueType支持只能作为imToolkit包的一部分。
Examples
Here’s a simple example:
import ImageFont, ImageDraw
draw = ImageDraw.Draw(image)
# use a bitmap font
font = ImageFont.load("arial.pil")
draw.text((10, 10), "hello", font=font)
# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)
draw.text((10, 25), "world", font=font)
Functions
- load
ImageFont.load(file) ⇒ Font instance
Loads a font from the given file, and returns the corresponding font object. If this function fails, it raises an IOError exception.
从给定的文件加载字体,并返回相应的字体对象。如果这个函数失败,它提出了一个IOError例外。
- load_path
ImageFont.load_path(file) ⇒ Font instance
Same as load, but searches for the file along sys.path if it’s not found in the current directory.
相同载荷,但搜索文件系统。路径如果它不是在当前目录中找到。
- truetype
ImageFont.truetype(file, size) ⇒ Font instance
Load a TrueType or OpenType font file, and create a font object. This function loads a font object from the given file, and creates a font object for a font of the given size.
加载一个TrueType或OpenType字体文件,并创建一个字体对象。这个函数加载字体对象从给定的文件,并创建一个为一个给定的大小的字体字体对象。
On Windows, if the given file name does not exist, the loader also looks in Windows fonts directory.
在Windows上,如果给定的文件名不存在,加载程序看起来也在Windows字体目录。
This function requires the _imagingft service.
这个函数需要_imagingft服务。
ImageFont.truetype(file, size, encoding=value) ⇒ Font instance
(New in 1.1.5) Load a TrueType or OpenType font file, and create a font object using the given encoding. Common encodings are “unic” (Unicode), “symb” (Microsoft Symbol), “ADOB” (Adobe Standard), “ADBE” (Adobe Expert), and “armn” (Apple Roman).
1.1.5(新)负载TrueType或OpenType字体文件,并创建一个使用给定的编码字体对象。常见的编码是“尤尼克公司”(Unicode),“symb”(微软符号),“ADOB”(Adobe标准),“用”(Adobe专家),和“armn”(苹果罗马)。
The following example draws a character using the Microsoft Symbol font, which uses the “symb” encoding and characters in the range 0xF000 to 0xF0FF:
下面的例子将一个角色使用微软字体,象征它使用“symb”编码和字符0 xf000到0 xf0ff范围:
font = ImageFont.truetype("symbol.ttf", 16, encoding="symb")
draw.text((0, 0), unichr(0xF000 + 0xAA))
- load_default
ImageFont.load_default() ⇒ Font instance
(New in 1.1.4) Load a “better than nothing” default font.
1.1.4(新)加载一个“聊胜于无”默认字体。
Methods
Font objects must implement the following methods, which are used by the ImageDraw layer.
字体对象必须实现以下方法,使用的ImageDraw层。
- getsize
font.getsize(text) ⇒ (width, height)
Returns the width and height of the given text, as a 2-tuple.
返回给定文本的宽度和高度,作为二元数组。
- getmask
font.getmask(text, mode=”“) ⇒ Image object
Returns a bitmap for the text. The bitmap should be an internal PIL storage memory instance (as defined by the Image.core interface module).
返回一个位图的文本。位图应该是一个内部库存储内存实例(如定义的图像。核心接口模块)。
If the font uses antialiasing, the bitmap should have mode “L” and use a maximum value of 255. Otherwise, it should have mode “1”.
如果字体使用反锯齿,位图模式“L”,应该使用最大值为255。否则,它应该模式“1”。
(New in 1.1.5) The optional mode argument is used by some graphics drivers to indicate what mode the driver prefers; if empty, the renderer may return either mode. Note that the mode is always a string, to simplify C-level implementations.
1.1.5(新)一些图形驱动程序所使用的参数是可选的模式来表示模式司机喜欢什么;如果空,渲染器会返回模式。注意,模式总是一个字符串,以简化c级实现。
九、The ImageGrab Module(ImageGrab模块)
The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory.
ImageGrab模块可以使用屏幕或剪贴板的内容复制到一个库的形象记忆。
The current version works on Windows only.
当前版本只适用于Windows。(难过!)
Functions
- grab
ImageGrab.grab() ⇒ image
ImageGrab.grab(bbox) ⇒ image
(New in 1.1.3) Take a snapshot of the screen, and return an “RGB” image. The bounding box argument can be used to copy only a part of the screen.
1.1.3(新)屏幕快照,并返回一个“RGB”的形象。边界框参数只能用于复制屏幕的一部分。
- grabclipboard
ImageGrab.grabclipboard() ⇒ image or list of strings or None
(New in 1.1.4) Take a snapshot of the clipboard contents, and return an image object or a list of file names. If the clipboard doesn’t contain image data, this function returns None.
1.1.4(新)剪贴板内容的快照,并返回一个图像对象或一个文件名列表。如果剪贴板不包含图像数据,这个函数返回None。
You can use isinstance to check if the function returned a valid image object, or something else:
您可以使用isinstance检查如果函数返回一个有效的图像对象,或其他东西:
im = ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
... got an image ...
elif im:
for filename in im:
try:
im = Image.open(filename)
except IOError:
pass # ignore this file
else:
... got an image ...
else:
... clipboard empty ...
十、The ImageMath Module(ImageMath模块)
(New in 1.1.6) The ImageMath module can be used to evaluate “image expressions”. The module provides a single eval function, which takes an expression string and one or more images.
1.1.6(新)ImageMath模块可以用来评估”的形象表达”。模块提供一个eval函数,它接受一个表达式字符串和一个或多个图像。
Examples
Using the ImageMath module(使用ImageMath模块)
import Image, ImageMath
im1 = Image.open("image1.jpg")
im2 = Image.open("image2.jpg")
out = ImageMath.eval("convert(min(a, b), 'L')", a=im1, b=im2)
out.save("result.png")
Functions
- eval
eval(expression, environment) ⇒ image or value
Evaluate expression in the given environment.
在给定的环境中评估表达式。
The expression argument is a string which uses the standard Python expression syntax. In addition to the standard operators, you can also use the functions described below.
表达式参数是一个字符串,使用标准的Python表达式语法。除了标准的运营商,您还可以使用下面描述的功能。
The environment argument is a dictionary that maps image names to Image instances. You can use one or more keyword arguments instead of a dictionary, as shown in the above example. Note that the names must be valid Python identifiers.
环境参数是一个字典,图片名称映射到图像实例。您可以使用一个或多个关键字参数而不是一本词典,在上面的示例中所示。注意,Python标识符名称必须是有效的。
In the current version, ImageMath only supports single-layer images. To process multi-band images, use the split and merge functions.
在当前版本中,ImageMath只支持单层图像。处理多波段图像,使用分割和合并功能。
This function returns an image, an integer value, a floating point value, or a pixel tuple, depending on the expression.
这个函数返回一个形象,一个整数值,一个浮点值,或一个像素元组,根据表达式。
Expression Syntax (表达式语法)
Expressions are standard Python expressions, but they’re evaluated in a non-standard environment. You can use PIL methods as usual, plus the following set of operators and functions:
表达式是标准的Python表情,但他们评估在一个非标准的环境中。像往常一样,您可以使用库方法加上以下的运营商和功能:
- Standard Operators(标准操作)
You can use standard arithmetical operators for addition (+), subtraction (-), multiplication (*), and division (/).
您可以使用标准的算术操作符为加(+)、减(-)、乘(*)、除(/)。
The module also supports unary minus (-), modulo (%), and power (**) operators.
该模块还支持一元负(-),模(%),和权力(* *)操作符。
Note that all operations are done with 32-bit integers or 32-bit floating point values, as necessary. For example, if you add two 8-bit images, the result will be a 32-bit integer image. If you add a floating point constant to an 8-bit image, the result will be a 32-bit floating point image.
请注意,所有操作完成32位整数或32位浮点值,作为必要的。例如,如果你添加两个8位图像,其结果将是一个32位整数的形象。如果您添加一个浮点常数一个8位图像,其结果将是一个32位浮点形象。
You can force conversion using the convert, float, and int functions described below.
你可以强制转换使用转换、浮点数、和int函数描述如下。
- Bitwise Operators(按位运算符)
The module also provides operations that operate on individual bits. This includes and (&), or (|), and exclusive or (^). You can also invert (~) all pixel bits.
该模块还提供了操作,操作单个位。这包括与(&)、或(|),异或(^)。你也可以反转(~)所有像素的位。
Note that the operands are converted to 32-bit signed integers before the bitwise operation is applied. This means that you’ll get negative values if you invert an ordinary greyscale image. You can use the and (&) operator to mask off unwanted bits.
注意,之前的操作数转换为32位有符号整数逐位运算。这意味着你会得到负反转一个普通的灰度图像。您可以使用和(&)运营商屏蔽不需要的部分。
Bitwise operators don’t work on floating point images.
按位运算符不浮点图像。
- Logical Operators(逻辑运算符)
Logical operators like and, or, and not work on entire images, rather than individual pixels.
逻辑运算符,或者,而不是在整个图像,而不是单独的像素。
An empty image (all pixels zero) is treated as false. All other images are treated as true.
一个空的形象(零)所有像素视为是错误的。所有其他的图像被当作真实的。
Note that and and or return the last evaluated operand, while not always returns a boolean value.
注意,和或返回最后评估操作数,而不总是返回一个布尔值。
- Built-in Functions(内置函数)
These functions are applied to each individual pixel.
这些函数应用于每个像素。
abs(image) Absolute value.
abs(图片)绝对值。
convert(image, mode) Convert image to the given mode. The mode must be given as a string constant.
(图像、模式)将图像转换为给定的模式。模式必须作为一个字符串常量。
float(image) Convert image to 32-bit floating point. This is equivalent to convert(image, “F”).
浮动(图像)将图像转换为32位浮点。这相当于转换(形象,“F”)。
int(image) Convert image to 32-bit integer. This is equivalent to convert(image, “I”).
将图像转换为32位的整数。这相当于转换(形象,“我”)。
Note that 1-bit and 8-bit images are automatically converted to 32-bit integers if necessary to get a correct result.
注意,比特和8位图像自动转换为32位的整数,如果需要得到正确的结果。
max(image1, image2) Maximum value.
min(image1, image2) Minimum value.
十一、The ImageOps Module(ImageOps模块)
(New in 1.1.3) The ImageOps module contains a number of ‘ready-made’ image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images.
1.1.3(新)ImageOps模块包含许多“现成的”图像处理操作。这个模块比较实验,大多数运营商只有L和RGB图像。
Functions
- autocontrast
ImageOps.autocontrast(image, cutoff=0) ⇒ image
Maximize (normalize) image contrast. This function calculates a histogram of the input image, removes cutoff percent of the lightest and darkest pixels from the histogram, and remaps the image so that the darkest remaining pixel becomes black (0), and the lightest becomes white (255).
(规范化)图像对比度最大化。这个函数计算输入图像的直方图,删除截止的最轻和最暗像素的直方图,和重新映射图像,以便最黑暗的剩余像素变成黑色(0),和最轻的变成白色(255)。
- colorize
ImageOps.colorize(image, black, white) ⇒ image
Colorize grayscale image. The black and white arguments should be RGB tuples or color names; this function calculates a colour wedge mapping all black pixels in the source image to the first colour, and all white pixels to the second colour.
彩色化灰度图像。黑色和白色的参数应该是RGB元组或颜色名称;这个函数计算颜色楔源图像中的所有黑色像素映射到第一个颜色,和所有的白色像素的第二个颜色。
- crop
ImageOps.crop(image, border=0) ⇒ image
Remove border pixels from all four edges. This function works on all image modes.
除去所有四个边界像素边缘。该函数适用于所有图像模式。
- deform(变形)
ImageOps.deform(image, deformer, filter=Image.BILINEAR) ⇒ image
Deform the image using the given deformer object. The deformer should provide a getmesh method, which returns a MESH list suitable for the Image transform method. See the transform method for details.
变形图像使用给定变形器对象。变形器应该提供一个getmesh方法,它返回一个网格列表适合图像变换方法。有关详细信息,请参阅变换方法。
- equalize(平衡)
ImageOps.equalize(image) ⇒ image
Equalize the image histogram. This function applies a non-linear mapping to the input image, in order to create a uniform distribution of grayscale values in the output image.
平衡图像直方图。这个函数非线性映射适用于输入图像,为了创建一个输出图像中的灰度值均匀分布。
- expand(扩大)
ImageOps.expand(image, border=0, fill=0) ⇒ image
Add border pixels of border to the image, at all four edges.
添加边界像素边界的图像,在所有四个边。
- fit(适合)
ImageOps.fit(image, size, method, bleed, centering) ⇒ image
Returns a sized and cropped version of the image, cropped to the requested aspect ratio and size. The size argument is the requested output size in pixels, given as a (width, height) tuple.
返回一个图像的大小和裁剪版本,裁剪所请求的比例和规模。像素的大小参数是请求的输出大小,作为元组(宽度、高度)。
The method argument is what resampling method to use. The default is Image.NEAREST (nearest neighbour).
使用方法参数是重采样方法。缺省值是图像。最近的(最近的邻居)。
The bleed argument allows you to remove a border around the outside the image (from all four edges). The value is a decimal percentage (use 0.01 for one percent). The default value is 0 (no border).
流血参数允许您删除一个边框外的图像(从所有四个边)。百分比值是一个小数(用0.01百分之一)。默认值为0(无边框)。
The centering argument is used to control the cropping position. (0.5, 0.5) is center cropping (i.e. if cropping the width, take 50% off of the left side (and therefore 50% off the right side), and same with top/bottom).
定心参数用于控制种植位置。(0.5,0.5)中心种植(即如果裁剪宽度,50%的左边(右边),因此50%,和顶部/底部也一样)。
(0.0, 0.0) will crop from the top left corner (i.e. if cropping the width, take all of the crop off of the right side, and if cropping the height, take all of it off the bottom).
(0.0,0.0)将作物从左上角(即如果裁剪宽度,把所有的作物的右边,如果种植的高度,把所有的底部)。
(1.0, 0.0) will crop from the bottom left corner, etc. (i.e. if cropping the width, take all of the crop off the left side, and if cropping the height take none from the top (and therefore all off the bottom)).
(1.0,0.0)将作物从左下角,等等。(即如果裁剪宽度,脱掉所有的作物左边,如果种植高度没有从顶部(因此所有底部))。
The fit function was contributed by Kevin Cazabon.
适应函数是由凯文Cazabon贡献的。
- flip(翻转)
ImageOps.flip(image) ⇒ image
Flip the image vertically (top to bottom).
垂直翻转图像(从上到下)。
- grayscale(灰度)
ImageOps.grayscale(image) ⇒ image
Convert the image to grayscale.
将图像转换为灰度。
- invert(反)
ImageOps.invert(image) ⇒ image
Invert (negate) the image.
反转(否定)的图像。
- mirror(镜像)
ImageOps.mirror(image) ⇒ image
Flip image horizontally (left to right).
水平翻转图像(左到右)。
- posterize(多色调分色印)
ImageOps.posterize(image, bits) ⇒ image
Reduce the number of bits for each colour channel.
减少碎片的数量为每个颜色通道。
- solarize(曝光过度)
ImageOps.solarize(image, threshold=128) ⇒ image
Invert all pixel values above the given threshold.
反转所有像素值高于给定阈值。
下一篇:054-python库Pillow