一、概述(Python Imaging Library Overview)
PIL就是Python Imaging Library的缩写。就是影像库。
此文对应库版本为1.1.6
Introduction
The Python Imaging Library adds image processing capabilities to your Python interpreter.
库会添加图像处理功能到Python解释器。
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:
让我们来看看几个可能的使用这个库:
- Image Archives(图象档案)
The Python Imaging Library is ideal for for image archival and batch processing applications. You can use the library to create thumbnails, convert between file formats, print images, etc.
本库是图像档案和批处理应用程序的理想选择。你可以创建缩略图,文件格式之间的转换,打印图像,等等。
The current version identifies and reads a large number of formats. Write support is intentionally restricted to the most commonly used interchange and presentation formats.
当前版本能识别并支持大量的格式。写支持被故意限制到只支持最常用的格式。
- Image Display(图像展示)
The current release includes Tk PhotoImage and BitmapImage interfaces, as well as a Windows DIB interface that can be used with PythonWin and other Windows-based toolkits. Many other GUI toolkits come with some kind of PIL support.
前版本包括Tk PhotoImage和BitmapImage接口,以及一个Windows DIB接口,可以用于PythonWin和其他基于Windows的工具包。许多其他GUI工具包都靠本PIL库支持。
For debugging, there’s also a show method which saves an image to disk, and calls an external display utility.
对于调试,还有一个显示方法,保存图像到磁盘,并调用一个外部显示工具。
- Image Processing(图像处理)
The library contains basic image processing functionality, including point 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.
有一个直方图方法允许您把一个图像整成统计数据。这可以用于自动对比度增强,全局统计分析。
二、教程(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.
从文件加载一个图像,图像中使用开放的功能模块。
>>> 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.
如果成功的话,这个函数返回一个图像对象。您现在可以使用实例属性检查文件内容。
>>> print im.format, im.size, im.mode
PPM (512, 512) RGB
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.
格式化的属性来源于图像。如果没读到文件,图像为None。属性是一个二元数组包含大小宽度和高度(以像素为单位)。属性定义模式的数量和名字的乐队的形象,以及像素类型和深度。常见的模式是“L”(亮度)灰度图像的RGB真彩色图像,和“CMYK”印前图像。
If the file cannot be opened, an IOError exception is raised.
如果文件不能打开,一个IOError抛出。
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:
>>> 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工具来显示图像不是很有效。如果你没有安装xv,这甚至不工作。然而,当它工作时很方便调试和测试)
The following sections provide an overview of the different functions provided in this library.
以下部分概述PIL提供的不同的功能。
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.
本库支持多种图像文件格式。在“image”模块中使用“open”从磁盘读取图像。你不需要知道打开一个文件的文件格式。本库自动识别文件的内容格式。
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.
保存一个文件,使用sava方法就行。保存文件时,名字很重要。除非你指定的格式,否则使用文件扩展名来确定文件存储格式使用。
- Convert files to JPEG(转换成JPEG格式)
import os, sys
import Image
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".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:
第二个参数可以提供显式地指定一个文件格式的保存方法。如果你使用一个非标准扩展,您必须指定格式:
- Create JPEG Thumbnails(创建JPEG缩略图)
import os, sys
import Image
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for", infile
It is important to note 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:
这意味着打开一个图像文件是一个快速的操作,这是独立的文件大小和压缩类型。这是一个简单的脚本快速识别一组图像文件:
- 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
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.
图像类包含的方法允许您操作区域内的图像。从一个图像,提取小矩形使用crop方法。
- 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个元素的元祖定义,坐标是(左,上,右,下)。库使用坐标系统(0,0)在左上角。还要注意,坐标参考像素之间的位置,因此该地区在上面的例子中就是300 x300像素。
The region could now be processed in a certain manner and pasted back.
这个区域可以以某种方式处理和粘贴。
- 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).
当粘贴区域,区域必须匹配给定的区域的大小。此外,该地区无法扩展外部形象。然而,原始图像的模式和区域不需要匹配。如果他们不这样做,该区域是自动转换之前粘贴(有关详细信息,请参阅下面的部分颜色变换)。
Here’s an additional 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意味着粘贴图片是完全透明的。中间值表示不同程度的透明度。
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:
库还允许您使用个人乐队的多波段图像,比如一个RGB图像。分割方法创建一组新的图像,每个包含一个乐队从原来的多波段图像。合并函数接受一个模式和一个元组的图像,并结合成一个新的形象。下面的示例互换RGB图像的三种颜色:
- Splitting and merging bands(分裂和合并)
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
Note that for a single-band image, split returns the image itself. To work with individual colour bands, you may want to convert the image to “RGB” first.
注意,对于单波段图像,分割返回图像本身。与个人色彩的乐队,你可以先将图像转换为RGB”。
Geometrical Transforms(几何变换)
The Image class contains methods to resize and rotate an image. The former takes a tuple giving the new size, the latter the angle in degrees counter-clockwise.
图像类包含的方法来调整和旋转图像。前需要一个元组给予新的大小,后者的角度逆时针。
- Simple geometry transforms(简单的几何变换)
out = im.resize((128, 128))
out = im.rotate(45) # degrees counter-clockwise
To rotate the image in 90 degree steps, you can either use the rotate method or the transpose method. The latter can also be used to flip an image around its horizontal or vertical axis.
90度旋转图片的步骤,您可以使用旋转方法或转置方法。后者也可以用来翻转图像在水平或垂直轴。
- Transposing an image(更换一个图像)
out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)
There’s no difference in performance or result between transpose(ROTATE) and corresponding rotate operations.
性能没有差别在(旋转)和相应的旋转操作之后。
A more general form of image transformations can be carried out via the transform method. See the reference section for details.
更一般的形式可以通过变换进行图像转换方法。有关详细信息,请参阅参考资料小节。
Colour Transforms (颜色转换)
The Python Imaging Library allows you to convert images between different pixel representations using the convert function.
库允许您将使用转换函数图像不同像素表示。
- Converting between modes(模式之间的转换)
im = Image.open("lena.ppm").convert("L")
The library supports transformations between each supported mode and the “L” and “RGB” modes. To convert between other modes, you may have to use an intermediate image (typically an “RGB” image).
库支持每种受支持的模式之间的转换和“L”和“RGB”模式。其他模式之间转换,您可能需要使用一个中间图像(通常是一个“RGB”的形象)。
Image Enhancement (图像增强)
The Python Imaging Library provides a number of methods and modules that can be used to enhance images.
库提供了许多方法和模块,可用于增强图像。
Filters (过滤器)
The ImageFilter module contains a number of pre-defined enhancement filters that can be used with the filter method.
ImageFilter模块包含许多预定义的增强过滤器可以用于过滤方法。
- Applying filters(应用过滤器)
import ImageFilter
out = im.filter(ImageFilter.DETAIL)
Point Operations (点操作)
The point method can be used to translate the pixel values of an image (e.g. image contrast manipulation). In most cases, a function object expecting one argument can be passed to the this method. Each pixel is processed according to that function:
点的方法可以用来将一个图像的像素值(例如图像对比操作)。在大多数情况下,一个函数对象期望一个参数可以传递给这个方法。每个像素处理根据这个函数:
- Applying point transforms(应用点转换)
# multiply each pixel by 1.2
out = im.point(lambda i: i * 1.2)
Using the above technique, you can quickly apply any simple expression to an image. You can also combine the point and paste methods to selectively modify an image:
利用上述技术,您可以快速应用任何一个图像的简单的表达式。你也可以把点粘贴方法有选择地修改图像:
- Processing individual bands(处理块)
# split the image into individual bands
source = im.split()
R, G, B = 0, 1, 2
# select regions where red is less than 100
mask = source[R].point(lambda i: i < 100 and 255)
# process the green band
out = source[G].point(lambda i: i * 0.7)
# paste the processed band back, but only where red was < 100
source[G].paste(out, None, mask)
# build a new multiband image
im = Image.merge(im.mode, source)
Note the syntax used to create the mask:
注意语法用于创建修饰:
imout = im.point(lambda i: expression and 255)
Python only evaluates the portion of a logical expression as is necessary to determine the outcome, and returns the last value examined as the result of the expression. So if the expression above is false (0), Python does not look at the second operand, and thus returns 0. Otherwise, it returns 255.
库评估部分的逻辑表达式是必要的,以确定结果,并返回最后一个值检查的结果表达式。如果上面的表达式是错误的(0),Python没有看第二个操作数,从而返回0。否则,它将返回255。
Enhancement (增强)
For more advanced image enhancement, you can use the classes in the ImageEnhance module. Once created from an image, an enhancement object can be used to quickly try out different settings.
对于更高级的图像增强,您可以使用类ImageEnhance模块。一旦创建从一个图像,一个增强对象可以用来快速尝试不同的设置。
You can adjust contrast, brightness, colour balance and sharpness in this way.
你可以调整对比度、亮度、色彩平衡和清晰度。
- Enhancing images(增强图像)
import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")
Image Sequences (图像序列)
The Python Imaging Library contains some basic support for image sequences (also called animation formats). Supported sequence formats include FLI/FLC, GIF, and a few experimental formats. TIFF files can also contain more than one frame.
库包含一些基本的支持图像序列(也称为动画格式)。支持序列格式包括FLI /方法,GIF,和一些实验格式。TIFF文件也可以包含多个框架。
When you open a sequence file, PIL automatically loads the first frame in the sequence. You can use the seek and tell methods to move between different frames:
当您打开一个顺序文件,自动加载序列中的第一帧。您可以使用寻求并告诉方法之间移动不同的框架:
- Reading sequences(阅读序列)
import Image
im = Image.open("animation.gif")
im.seek(1) # skip to the second frame
try:
while 1:
im.seek(im.tell()+1)
# do something to im
except EOFError:
pass # end of sequence
As seen in this example, you’ll get an EOFError exception when the sequence ends.
在这个例子中可以看到,你会得到一个EOFError异常序列结束时。
Note that most drivers in the current version of the library only allow you to seek to the next frame (as in the above example). To rewind the file, you may have to reopen it.
注意,大多数司机在库的当前版本只允许你寻求下一个框架(在上面的示例中)。倒带文件,您可能必须重新打开它。
The following iterator class lets you to use the for-statement to loop over the sequence:
下面的迭代器类允许您使用遍历序列的for语句:
- A sequence iterator class(一个序列迭代器类)
class ImageSequence:
def __init__(self, im):
self.im = im
def __getitem__(self, ix):
try:
if ix:
self.im.seek(ix)
return self.im
except EOFError:
raise IndexError # end of sequence
for frame in ImageSequence(im):
# ...do something to frame...
Postscript Printing(打印)
The Python Imaging Library includes functions to print images, text and graphics on Postscript printers. Here’s a simple example:
库包含函数打印图像,文本和图形在Postscript打印机。这是一个简单的例子:
- Drawing Postscript(画打印)
import Image
import PSDraw
im = Image.open("lena.ppm")
title = "lena"
box = (1*72, 2*72, 7*72, 10*72) # in points
ps = PSDraw.PSDraw() # default is sys.stdout
ps.begin_document(title)
# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box)
# draw centered title
ps.setfont("HelveticaNarrow-Bold", 36)
w, h, b = ps.textsize(title)
ps.text((4*72-w/2, 1*72-h), title)
ps.end_document()
More on Reading Images(更多阅读图片)
As described earlier, the open function of the Image module is used to open an image file. In most cases, you simply pass it the filename as an argument:
如前所述,打开图像的功能模块是用来打开一个图像文件。在大多数情况下,您只需通过文件名作为参数:
im = Image.open("lena.ppm")
If everything goes well, the result is an Image object. Otherwise, an IOError exception is raised.
如果一切顺利,结果是一个图像对象。否则,一个IOError的例外抛出。
You can use a file-like object instead of the filename. The object must implement read, seek and tell methods, and be opened in binary mode.
您可以使用一个类文件对象,而不是文件名。对象必须实现阅读,寻求和告诉的方法,是在二进制模式下打开。
- Reading from an open file(从一个打开的文件阅读)
fp = open("lena.ppm", "rb")
im = Image.open(fp)
To read an image from string data, use the StringIO class:
从字符串读取图像数据,使用StringIO类:
- Reading from a string(从一个字符串阅读)
import StringIO
im = Image.open(StringIO.StringIO(buffer))
Note that the library rewinds the file (using seek(0)) before reading the image header. In addition, seek will also be used when the image data is read (by the load method). If the image file is embedded in a larger file, such as a tar file, you can use the ContainerIO or TarIO modules to access it.
注意,图书馆倒带文件(使用寻求(0))前阅读图像标题。此外,寻求图像读取数据时也会使用(通过负载方法)。如果图像文件嵌入到一个更大的文件,比如tar文件,您可以使用ContainerIO或TarIO模块来访问它。
- Reading from a tar archive(从一个tar存档阅读)
import TarIO
fp = TarIO.TarIO("Imaging.tar", "Imaging/test/lena.ppm")
im = Image.open(fp)
Controlling the Decoder(控制译码器)
Some decoders allow you to manipulate the image while reading it from a file. This can often be used to speed up decoding when creating thumbnails (when speed is usually more important than quality) and printing to a monochrome laser printer (when only a greyscale version of the image is needed).
一些解码器允许你控制图像在阅读从一个文件中。这通常可以用来加快解码在创建缩略图(当速度通常比质量更重要)和印刷单色激光打印机(当只需要一个图像的灰度版本)。
The draft method manipulates an opened but not yet loaded image so it as closely as possible matches the given mode and size. This is done by reconfiguring the image decoder.
draft方法操纵一个打开但尚未加载图片所以尽可能匹配给定的模式和规模。这是通过重新配置图像解码器。
- Reading in draft mode(阅读草稿模式)
im = Image.open(file)
print "original =", im.mode, im.size
im.draft("L", (100, 100))
print "draft =", im.mode, im.size
This prints something like:
original = RGB (512, 512)
draft = L (128, 128)
Note that the resulting image may not exactly match the requested mode and size. To make sure that the image is not larger than the given size, use the thumbnail method instead.
注意,生成图像和大小可能不完全匹配请求的模式。确保不大于给定大小的图片,使用缩略图方法。
三、Concepts(概念)
The Python Imaging Library handles raster images; that is, rectangles of pixel data.
库处理光栅图像;也就是说,矩形像素数据。
Bands(块、乐队、波段)
An image can consist of one or more bands of data. The Python Imaging Library allows you to store several bands in a single image, provided they all have the same dimensions and depth.
一个图像可以包含一个或多个波段的数据。Python成像库允许您存储一些乐队在一个单一的形象,他们都有相同的尺寸和深度。
To get the number and names of bands in an image, use the getbands method.
让乐队的数量和名称在一个图像,使用getbands方法。
- Mode(模式)
The mode of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes:
图像的模式定义的类型和深度图像中的一个像素。当前版本支持以下标准模式:
1 (1-bit pixels, black and white, stored with one pixel per byte)
一个像素存储字节,黑色和白色。
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 x8-bit像素,真正的颜色
RGBA (4x8-bit pixels, true colour with transparency mask)
4 x8-bit像素,真实的颜色和透明蒙版
CMYK (4x8-bit pixels, colour separation)
4 x8-bit像素,分色
YCbCr (3x8-bit pixels, colour video format)
3 x8-bit像素,彩色视频格式
I (32-bit signed integer pixels)
32位带符号整数像素
F (32-bit floating point pixels)
32位浮点像素
PIL also provides limited support for a few special modes, including LA (L with alpha), RGBX (true colour with padding) and RGBa (true colour with premultiplied alpha). However, PIL doesn’t support user-defined modes; if you to handle band combinations that are not listed above, use a sequence of Image objects.
库还提供了有限的支持几个特殊的模式,包括LA(有alpha的L), RGBX(真正的色彩填充)和RGBa(真彩色自左乘α)。然而,库不支持用户定义的模式;如果你处理不上面列出的乐队组合,使用一个图像序列对象。
You can read the mode of an image through the mode attribute. This is a string containing one of the above values.
你可以阅读模式图像的模式属性。这是一个字符串包含一个以上的值。
Size (大小)
You can read the image size through the size attribute. This is a 2-tuple, containing the horizontal and vertical size in pixels.
你可以阅读图像大小通过大小属性。这是一个二元数组,包含水平和垂直大小(以像素为单位)。
Coordinate System (坐标系统)
The Python Imaging Library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner. Note that the coordinates refer to the implied pixel corners; the centre of a pixel addressed as (0, 0) actually lies at (0.5, 0.5).
库使用笛卡尔像素坐标系统,与(0,0)在左上角。注意,坐标是指隐含像素角落;的中心像素处理(0,0)实际上是在(0.5,0.5)。
Coordinates are usually passed to the library as 2-tuples (x, y). Rectangles are represented as 4-tuples, with the upper left corner given first. For example, a rectangle covering all of an 800x600 pixel image is written as (0, 0, 800, 600).
坐标通常是通过到库集合(x, y),矩形表示为4元组给出的左上角。例如,一个矩形覆盖所有800 x600的像素图像写成(0,0,800,600)。
Palette (调色板)
The palette mode (“P”) uses a colour palette to define the actual colour for each pixel.
面板模式(“P”)使用颜色来为每个像素定义实际的颜色。
Info (信息)
You can attach auxiliary information to an image using the info attribute. This is a dictionary object.
您可以将辅助信息附加到一个图像使用info属性。这是一个字典对象。
How such information is handled when loading and saving image files is up to the file format handler (see the chapter on Image File Formats). Most handlers add properties to the info attribute when loading an image, but ignore it when saving images.
如何处理这些信息当加载和保存图像文件的文件格式处理程序(见章节图像文件格式)。大多数处理程序添加属性信息属性时加载一个图像,但忽略它在保存图像。
Filters (过滤器)
For geometry operations that may map multiple input pixels to a single output pixel, the Python Imaging Library provides four different resampling filters.
几何操作,多个输入像素映射到单个输出像素,Python成像库提供了四种不同重采样过滤器。
NEAREST(最近的)
Pick the nearest pixel from the input image. Ignore all other input pixels.
选择最近的像素从输入图像。忽略所有其他输入像素。
BILINEAR(双线性)
Use linear interpolation over a 2x2 environment in the input image. Note that in the current version of PIL, this filter uses a fixed input environment when downsampling.
使用线性插值在输入图像2 x2的环境。注意,在库的当前版本中,这个过滤器将采样时使用一个固定的输入环境。
BICUBIC(双三次的)
Use cubic interpolation over a 4x4 environment in the input image. Note that in the current version of PIL, this filter uses a fixed input environment when downsampling.
使用三次插值4 x4环境输入图像。注意,在库的当前版本中,这个过滤器将采样时使用一个固定的输入环境。
ANTIALIASA(抗锯齿)
(New in PIL 1.1.3). Calculate the output pixel value using a high-quality resampling filter (a truncated sinc) on all pixels that may contribute to the output value. In the current version of PIL, this filter can only be used with the resize and thumbnail methods.
在库1.1.3(新)。使用高质量的重采样滤波器计算输出像素值(截断sinc)对所有像素可能导致输出值。在当前版本的库,这个过滤器只能用于调整和缩略图的方法。
Note that in the current version of PIL, the ANTIALIAS filter is the only filter that behaves properly when downsampling (that is, when converting a large image to a small one). The BILINEAR and BICUBIC filters use a fixed input environment, and are best used for scale-preserving geometric transforms and upsamping.
注意,在当前版本的库,表现正常的平滑滤波器是唯一的滤波器将采样时(也就是说,当将大型图像转换成一个小)。双线性和双三次的过滤器使用一个固定的输入环境,并且是最好的用于scale-preserving几何变换和upsamping。
附:JPEG格式
总结:JPEG就是一种压缩技术,图片后缀一般为“.jpg”或“.jpeg”。过高压缩会导致失真。技术原理是去除多余的和重复的元素达到压缩目的。
JPEG(全称是Joint Photographic Experts Group)是常见的一种图像格式,它由联合照片专家组开发并命名为"ISO 10918-1",JPEG仅仅是一种俗称而已。
JPEG文件的扩展名为其压缩技术十分先进,它用去除冗余的图像和彩色数据,获取得极高的压缩率的同时能展现十分丰富生动的图像,换句话说,就是可以用最少的磁盘空间得到较好的图像质量。
同时JPEG还是一种很灵活的格式,具有调节图像质量的功能,允许你用不同的压缩比例对这种文件压缩,比如我们最高可以把1.37MB的BMP位图文件压缩至20.3KB。当然我们完全可以在图像质量和文件尺寸之间找到平衡点。
JPEG是Joint Photographic Experts Group(联合图像专家组)的缩写,文件后辍名为".jpg"或".jpeg",是最常用的图像文件格式,由一个软件开发联合会组织制定,是一种有损压缩格式,能够将图像压缩在很小的储存空间,图像中重复或不重要的资料会被丢失,因此容易造成图像数据的损伤。尤其是使用过高的压缩比例,将使最终解压缩后恢复的图像质量明显降低,如果追求高品质图像,不宜采用过高压缩比例。
但是JPEG压缩技术十分先进,它用有损压缩方式去除冗余的图像数据,在获得极高的压缩率的同时能展现十分丰富生动的图像,换句话说,就是可以用最少的磁盘空间得到较好的图像品质。而且 JPEG是一种很灵活的格式,具有调节图像质量的功能,允许用不同的压缩比例对文件进行压缩,支持多种压缩级别,压缩比率通常在10:1到40:1之间,压缩比越大,品质就越低;相反地,压缩比越小,品质就越好。
比如可以把1.37Mb的BMP位图文件压缩至20.3KB。当然也可以在图像质量和文件尺寸之间找到平衡点。JPEG格式压缩的主要是高频信息,对色彩的信息保留较好,适合应用于互联网,可减少图像的传输时间,可以支持24bit真彩色,也普遍应用于需要连续色调的图像。
JPEG格式是目前网络上最流行的图像格式,是可以把文件压缩到最小的格式,在 Photoshop软件中以JPEG格式储存时,提供11级压缩级别,以0—10级表示。其中0级压缩比最高,图像品质最差。即使采用细节几乎无损的10 级质量保存时,压缩比也可达 5:1。以BMP格式保存时得到4.28MB图像文件,在采用JPG格式保存时,其文件仅为178KB,压缩比达到24:1。经过多次比较,采用第8级压缩为存储空间与图像质量兼得的最佳比例。PG文件的优点是体积小巧,并且兼容性好。
- 格式应用
由于JPEG优异的品质和杰出的表现,它的应用也非常广泛,特别是在网络和光盘读物上,肯定都能找到它的影子。
目前各类浏览器均支持JPEG这种图像格式,因为JPEG格式的文件尺寸较小,下载速度快,使得Web页有可能以较短的下载时间提供大量美观的图像,JPEG同时也就顺理成章地成为网络上最受欢迎的图像格式。
- 格式类型
JPEG2000作为JPEG的升级版,其压缩率比JPEG高约30%左右,同时支持有损和无损压缩。JPEG2000格式有一个极其重要的特征在于它能实现 [1] 渐进传输,即先传输图像的轮廓,然后逐步传输数据,不断提高图像质量,让图像由朦胧到清晰显示。此外,JPEG2000还支持所谓的"感兴趣区域" 特性,可以任意指定影像上感兴趣区域的压缩质量,还可以选择指定的部分先解压缩。
JPEG2000和JPEG相比优势明显,且向下兼容,因此可取代传统的JPEG格式。JPEG2000即可应用于传统的JPEG市场,如扫描仪、数码相机等,又可应用于新兴领域,如网路传输、无线通讯等等。
- 压缩标准
JPEG是由国际标准组织(ISO)和国际电话电报咨询委员会(CCITT)为静态图像所创建的第一个国际数字图像压缩标准,也是至今一直在使用的、应用最广的图像压缩标准。JPEG由于可以提供有损压缩,因此压缩比可以达到其他传统压缩算法无法比拟的程度。
JPEG的压缩模式有以下几种:
1.顺序式编码(Sequential Encoding)
一次将图像由左到右、由上到下顺序处理。
2.递增式编码(Progressive Encoding)
当图像传输的时间较长时,可将图像分数次处理,以从模糊到清晰的方式来传送图像(效果类似GIF在网络上的传 输)。
3.无有损编码(Lossless Encoding)
4.阶梯式编码(Hierarchical Encoding)
图像以数种分辨率来压缩,其目的是为了让具有高分辨率的图像也可以在较低分辨率的设备上显示。
在Independent JPEG Group所提供的源码上,有jpegtran程序,就提供了优化Huffman [2] ,转成渐进式,镜射,旋转这些无损耗转换。
网友评论