logo资料库

Python Image Library中文版.pdf

第1页 / 共84页
第2页 / 共84页
第3页 / 共84页
第4页 / 共84页
第5页 / 共84页
第6页 / 共84页
第7页 / 共84页
第8页 / 共84页
资料共84页,剩余部分请下载后查看
封面
Python图像库综述
介绍
图像显示
图像处理
教程
使用图像类
读/写图像
剪切,粘贴和合并图像
几何变换
颜色转变
图像序列
图像增强
过滤器
浮点运算
增强
Postscript打印
读取更多图像
控制解码器
概念
Bands
Mode
Size
Coordinate System
Palette
Info
Filters
Image模块
Example
Functions
new
open
Blend
composite
eval
fromstring
merge
Methods
convert
copy
crop
draft
filter
fromstring
getbands
getbbox
getdata
getextrema
getpixel
histogram
load
offset
paste
point
putalpha
putdata
putpalette
putpixel
resize
rotate
save
seek
show
split
tell
thumbnail
tobitmap
tostring
transform
transpose
verify
Attributes
format
mode
size
palette
info
ImageChops模块
Functions
constant
duplicate
invert
lighter
darker
difference
multiply
screen
add
subtract
blend
composite
offset
ImageCrackCode模块
Functions
CrackCode
Methods and attributes
area
bbox
caliper
centroid
edge
links
offset
start
top
hit
topath
getmask
getoutline
ImageDraw模块
Example
Functions
Draw
Methods
arc
bitmap
chord
ellipse
line
pieslice
point
polygon
rectangle
text
textsize
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Python Image Library Author:Fredrik Lundh Matthew Ellis Tanslator:Ivy Law Date:2011-8-20 Email:hyhsdreams9207@163.com
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Python 图像库综述 PIL 1.1.3 | March 12, 2002 | Fredrik Lundh, Matthew Ellis Introduction The Python Imaging Library adds image processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation,and fairly powerful image processing capabilities. The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool. Let's look at a few possible uses of this library: 介绍 像处理能力。 核心图像库是为快速接入储存在一些基本像素格式的数据设计的,它应该为一般的图像 处理工具提供一个坚实的基础。 让我们来看看这个库的一些应用场合: Python 图像库增加了 python 解释器的图像处理功能 Python 图像库提供了广泛文件格式的支持,一个高效的内部表示,和一个相当强大的图 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. For debugging, there's also a show method in the Unix version which calls xv to display the image. 图像显示 当前版本包括 TK PhtotoImage 和 BitmapImage 接口,以及一个能和 PythonWin 一起使用的 Windows DIB 接口。 为排除错误,在 Unix 的版本还有一个调用 xv 来显示图像的显示方法。 Image Processing The library contains some basic image processing functionality, including point
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. operations,filtering with a set of built-in convolution kernels, and colour space conversions. The library also supports image resizing, rotation and arbitrary affine transforms. 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. 图像处理 PIL 库包含了一些基本图像处理功能,包括浮点运算,用一个内置的卷积内核进行过滤, 和一个颜色空间转换。 这个库同样支持图像缩放、旋转和任意仿射变换。 有一个直方图的方法允许拉出图像的一些统计数字,可应用于自动对比度增强,以对全球 的统计分析。 Tutorial 教程 Using the Image Class 使用图像类 The most important class in the Python Imaging Library is the Image class, defined in the module with the same name. You can create instances of this class in several ways; either by loading images from files, processing other images, or creating images from scratch.To load an image from a file, use the open function in the Image module. Python 图像库中最重要的类就是 Image 类,在模块中用 Image 定义的。你能通过几种途径 创建 Image 类的实例:或从文件导入图像,处理其他图像,或从 sratch 创建图像。如要从文 件中导入图像,要使用 Image 模块中的 open 函数。 >>> import Image >>> im = Image.open("lena.ppm") If successful, this function returns an Image object. You can now use instance attributes to examine the file contents. 如果成功的话,将会返回一个 Image 对象。可以使用这个实例属性来检查文件的内容 >>> print im.format, im.size, im.mode PPM (512, 512) RGB
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. The format attribute identifies the source of an image. If the image was not read from a file, it is set to None. The size attribute is a 2-tuple containing width and height (in pixels). The mode attribute defines 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. format 属性标识图像源,如果不是从文件读取图像,format 属性设为 None。size 属性是 一个包括宽度和高度的二元组(以像素为单位)。mode 属性定义图像中条带的数量和名称, 以及图像的像素类型和深度。常见的模式是“L”(亮度模式)模式的灰度图像,“RGB”(色彩 模式)是真彩图像,“CMYK”(印刷色彩模式)是印前图像。 If the file cannot be opened, an IOError exception is raised. Once you have an instance of the Image class, you can use the methods defined by this class to process and manipulate the image. For example, let's display the image we just loaded: 如果文件打开失败,会引发一个 IOError 异常。 一旦你有 Image 类的实例,你就可以使用 Image 类定义的方法来处理和操作图像。例如, 显示刚才加载的图像: >>> im.show() (The standard version of show is not very efficient, since it saves the image to a temporary file and calls the xv utility to display the image. If you don't have xv installed,it won't even work. When it does work though, it is very handy for debugging and tests.) (标准版的显示不是很有效,因为它保存图像到一个临时文件,并调用 xv utility(实用工 具)来显示图像。如果没有安装 xv,它甚至不工作。不过,它的工作非常方便调试和测试) The following sections provide an overview of the different functions provided in this library. 以下各节提供了一个在这个库中提供的不同功能的概述。
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Reading and Writing Images 读/写图像 The Python Imaging Library supports a wide variety of image file formats. To read files from disk, use the open function in the Image module. You don't have to know the file format to open a file. The library automatically determines the format based on the contents of the file. Python 图像库(PIL)支持多种图像文件格式。若从硬盘读取文件,使用 Image 模块的 open 函数。你不需要知道打开文件的格式,PIL 库会根据文件的内容自动判定格式。 To save a file, use the save method of the Image class. When saving files, the name becomes important. Unless you specify the format, the library uses the filename extension to discover which file storage format to use. 要保存文件,使用 Image 类的 save 方法。当保存文件时,名称变得重要了。除非指定格式, PIL 库使用文件名扩展来发现所使用的文件储存格式。 Example: Convert files to JPEG import os, sys import Image for infile in sys.argv[1:]: outfile = os.path.splitext(infile)[0] + ".jpg" if infile != outfile: try: Image.open(infile).save(outfile) except IOError: print "cannot convert", infile A second argument can be supplied to the save method which explicitly specifies a file format. If you use a non-standard extension, you must always specify the format this way: 第二个参数可提供明确指定文件格式的保存方法。如果您使用非标准扩展,你必须始终 指定这样的格式:
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Example: Create JPEG Thumbnails import os, sys import Image for infile in sys.argv[1:]: outfile = os.path.splitext(infile)[0] + ".thumbnail" if infile != outfile: try: im = Image.open(infile) im.thumbnail((128, 128)) im.save(outfile, "JPEG") except IOError: print "cannot create thumbnail for", infile It is important to note is that the library doesn't decode or load the raster data unless it really has to. When you open a file, the file header is read to determine the file format and extract things like mode, size, and other properties required to decode the file, but the rest of the file is not processed until later. 重要的是要注意库不会解码或加载栅格数据,除非它真的有数据。当你打开一个文件,读 取文件头来判定文件格式和提取模式、大小和对文件解码所需的其它属性,但是文件的其余 部分直到后来才处理。 This means that opening an image file is a fast operation, which is independent of the file size and compression type. Here's a simple script to quickly identify a set of image files: 这意味着打开一个图像文件是一个快速操作,与文件大小和压缩类型独立。下面是一个简 单脚本来快速确定一组图像: Example: Identify Image Files import sys import Image for infile in sys.argv[1:]: try: im = Image.open(infile) print infile, im.format, "%dx%d" % im.size, im.mode except IOError: pass
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. Cutting, Pasting and Merging Images 剪切,粘贴和合并图像 The Image class contains methods allowing you to manipulate regions within an image. To extract a sub-rectangle from an image, use the crop method. Image 类包含允许你操纵一个图像领域的方法,使用 crop 方法从一个图像抓取一个子矩 形区域。 Example: Copying a subrectangle from an image box = (100, 100, 400, 400) region = im.crop(box) The region is defined by a 4-tuple, where coordinates are (left, upper, right, lower). The Python Imaging Library uses a coordinate system with (0, 0) in the upper left corner. Also note that coordinates refer to positions between the pixels, so the region in the above example is exactly 300x300 pixels. 该区域由一个 4 元组指定,坐标为(left,upper,right,lower)。Python 图像库使用(0,0) 在左上角。还要注意的是坐标指的是像素之间的位置,所以上面的区域是 300x300 像素。 The region could now be processed in a certain manner and pasted back 该区域能以某种方式处理并粘贴回 Example: Processing a subrectangle, and pasting it back region = region.transpose(Image.ROTATE_180) im.paste(region, box) When pasting regions back, the size of the region must match the given region exactly. In addition, the region cannot extend outside the image. However, the modes of the original image and the region do not need to match. If they don't, the region is automatically converted before being pasted (see the section on Colour Transforms below for details). 当粘贴回时,该区域的大小必须完全匹配给定的区域。此外,该地区无法扩展以外的形象。 然而,原始图像模式和地区并不需要相匹配。如果他们不这样做,该地区将被自动转换,然
Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. 后被粘贴(见彩色部分转化为下面详细说明)。 Here's an additional example: Example: Rolling an image def roll(image, delta): "Roll an image sideways" xsize, ysize = image.size delta = delta % xsize if delta == 0: return image part1 = image.crop((0, 0, delta, ysize)) part2 = image.crop((delta, 0, xsize, ysize)) image.paste(part2, (0, 0, xsize-delta, ysize)) image.paste(part1, (xsize-delta, 0, xsize, ysize)) return image For more advanced tricks, the paste method can also take a transparency mask as an optional argument. In this mask, the value 255 indicates that the pasted image is opaque in that position (that is, the pasted image should be used as is). The value 0 means that the pasted image is completely transparent. Values in-between indicate different levels of transparency. 对于更高级的技巧,粘贴方法也可以作为一个透明度屏蔽可选的参数。在这个掩饰中,值 255 表示粘贴图像是不透明的。值 0 意味着粘贴图像是完全透明的。0-255 的值表示透明程度。 The Python Imaging Library also allows you to work with the individual bands of an multi-band image, such as an RGB image. The split method creates a set of new images, each containing one band from the original multi-band image. The merge function takes a mode and a tuple of images, and combines them into a new image. The following sample swaps the three bands of an RGB image: Python 图像库也允许单个波段的多波段图像,比如 RGB 图像。split 方法创建了一组新图 像,每一个都包含一个波段。merge 函数接收一个模式和一个图像元组,并组成一个新图像。 以下的示例交换 RGB 图像的三个波段: Example: Splitting and merging bands r, g, b = im.split() im = Image.merge("RGB", (b, g, r))
分享到:
收藏