## Python图像处理库(PIL)--简介和基本概念

Python Imaging Library为您的python程序添加图像处理能力。这个库提供广泛的文件格式支持、高效的内部表示和相当强大的图像处理能力。

Python Imaging Library的最新版本可以识别并读取多数图像格式。写图像操作则有意地限制为最常用的交流和发布的格式。 convert images between different pixel representations 图像格式转换
• 缩放、裁剪、合成、变换和通道 Scaling, cropping, compositing, transforms, bands (the separate color channels e.g. alpha channel 半透明通道)
The library also supports image resizing, rotation and arbitrary affine transforms.提供图像缩放，旋转和任意仿射变换的支持。
• 图像增强 Image Enhancement
point operations,filtering with a set of built-in convolution kernels, and colour space conversions. 像素操作，内置convolution kernel的滤镜和颜色空间变换
adjust contrast, brightness, colour balance and sharpness using the ImageEnhance module
• 直方图、对比增强和统计分析 histogram,contrast enhancement,statistical analysis
There's a histogram method allowing you to pull some statistics out of an image. This can be used for automatic contrast enhancement, and for global statistical analysis.提供histogram方法使您可以输出对某个图像的一些统计信息。这可以用于自动对比增强和全局统计分析。
• 绘制文字和基本图形 Drawing text and basic shapes
• 图像序列(动画) Image Sequences

Supported sequence formats include FLI/FLC, GIF, and a few experimental formats. TIFF files can also contain more than one frame.

• 图像显示 Image Display
The current release includes Tk PhotoImage and BitmapImage interfaces, as well as a Windows DIB interface that can be used with PythonWin. For X and Mac displays, you can use Jack Jansen's img library.最新发布版本包含Tk PhotoImage and BitmapImage 接口，Windows DIB 接口也可以随着PythonWin一起使用。而对了X 和Mac系统的显示，您可以使用Jack Jansen的图像库。
For debugging, there's also a showmethod in the Unix version which calls xv to display the image. Debug版本中，于Unix也提供show方法调用xv来显示图像
• 图像打印 Postscript Printing
print images, text and graphics on Postscript printers

• 图像大小及格式转换 Image resizing or format conversions
• 图表 Chart generation (also see PIDDLE piddle.sf.net)
• 生成缩略图 Thumbnail generation
• 水印和图例 Watermarking or adding legend

## 基本概念 PIL API - Concepts

The size attribute is a 2-tuple containing width and height (in pixels).

The graphics interface uses the same coordinate system as PIL itself, with (0, 0) in the upper left corner.

Pil采取左上角为(0,0)的坐标系统

Angles are given in degrees. Zero degrees is in the +x (east) direction, and the angle increases

counter-clockwise,in the usual Cartesian convention. For example, angle 45 points northeast.

A bounding box or bbox is a rectangle(矩形) in the image. It is defined by a 4-tuple, (x0, y0, x1, y1) where (x0,y0) is the top left (northwest) corner of the rectangle, and (x1, y1) is the bottom right (southeast) corner.
Generally, the area described by a bounding box will include point (x0, y0), but it will not include point (x1, y1) or the row and column of pixels containing point (x1, y1).
For example, drawing an ellipse inside the bounding box (0,0,5,10) will produce an ellipse 5 pixels
wide and 10 pixels high. The resulting ellipse will include pixel column 4 but not column 5, and will
also include pixel row 9 but not row 10.

An image band is a set of values, one per image pixel.

Monochrome(黑白) or grayscale images have one band;color images in the RGB system have three bands, CMYK images have four, and so on.

Photoshop users will recognize bands as similar to Photoshop channels.

pil库中，图片之间的模式(mode)可以转化。

>>> bands = im.split ()

>>> rIm = bands [0]

>>> aIm = bands [3]

>>> remadeImage = Image.merge (“RGBA”, (rIm, gIm, bIm, aIm))


>>> grayscaleIm = Image.open (“myAlpha.gif”)

>>> remadeImage = myImage.putalpha (grayscaleIm)  # replace the alpha band


the number and names of the bands in the image, and also the pixel type and depth.

Common modes are "L" (luminance) for greyscale images, "RGB" for true colour images, and "CMYK" for pre-press images.

1 (1-bit pixels, black and white, stored with one pixel per byte)

1位像素，黑和白，存成8位的像素
L (8-bit pixels, black and white)

8位像素，黑白
P (8-bit pixels, mapped to any other mode using a colour palette)

8位像素，使用调色板映射到任何其他模式
RGB (3x8-bit pixels, true colour)

3×8位像素，真彩
RGBA (4x8-bit pixels, true colour with transparency mask)

4×8位像素，真彩+透明通道
CMYK (4x8-bit pixels, colour separation)

4×8位像素，颜色隔离
YCbCr (3x8-bit pixels, colour video format)

3×8位像素，彩色视频格式
I (32-bit integer pixels)

32位整型像素
F (32-bit floating point pixels)

32位浮点型像素

mode("P")为每个像素定义具体的颜色值

• 单通道图像-像素数值32bit value

For single-band images, the color is the pixel value.

For “1”, “L”, and “I” images, use integers.
For “F” images, use integer or floating point values.
For palette images (mode “P”), use integers as colour indexes.

For example, in a mode "1" image, the color is a single integer, 0 for black, 1 for white.
For mode "L", it is an integer in the range [0,255], where 0 means black and 255 means white.

• 多通道图像-像素元组tuple
For multi-band images, supply a tuple with one value per band.
For “RGB” images, use a 3-tuple containing integer values.The tuple (255,0,0) is pure red.

• 字符串常量 In 1.1.4 and later, you can also use RGB 3-tuples or colour names (see below). 颜色对照表

• CSS风格的颜色字符串
Hexadecimal color specifiers, given as “#rgb” or “#rrggbb”. For example, “#ff0000” specifies pure red.

• RGB像素值
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.

• HSL色彩模式
Hue-Saturation-Lightness (HSL) functions, given as “hsl(hue, saturation%, lightness%)”
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%
lightness is a value between 0% and 100% (black=0%, normal=50%, white=100%).
For example, “hsl(0,100%,50%)” is pure red.
通过对色调(H)、饱和度(S)、亮度(L)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色。

• 常见HTML颜色名称
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.

Some operations that reduce the number of pixels, such as creating a thumbnail, can use different filters to compute the new pixel values. These include:

1 NEAREST最近
Uses the value of the nearest pixel.
2 BILINEAR双线性
Uses linear interpolation over a 2x2 set of adjacent pixels.
3 BICUBIC双三次
Uses cubic interpolation over a 4x4 set of pixels.
4 ANTIALIAS平滑
Neighboring pixels are resampled to find the new pixel value.

1 resize(size,filter=None)

>>> smallIm = im.resize ( (128, 128), Image.ANTIALIAS)

2 thumbnail(size,filter=None)

1 改变原图 Modifies in-place
2 保持比例 aspect ratio(height : width)) -- size为(400,150)的im, im.thumbnail((40,40))后变为(40,15)3 filter(name)
Return a copy of the image filtered through a named image enhancement filter.

PIL可以使用bitmap fonts或者OpenType/TrueType fonts.

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.

To load a bitmap font, use the load functions in the ImageFont module.

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.(IronPIL)

To load a built-in font, use the Font constructor in the ImageFont module.

The format attribute identifies the source of an image. If the image was not read from a
file, it is set to None.

>>> im.save (“vacation.png”)  #OR
>>> im.save (“vacation.png”, "PNG”)

posted on 2012-04-19 10:58  小唯THU  阅读(5859)  评论(0编辑  收藏

• 随笔 - 122
• 文章 - 6
• 评论 - 90
• 引用 - 0