1. 程式人生 > >Python圖像處理庫(PIL)

Python圖像處理庫(PIL)

positions 坐標系統 透明 follow set origin 三種 cells rectangle

Python Imaging Library為您的python程序添加圖像處理能力。這個庫提供廣泛的文件格式支持、高效的內部表示和相當強大的圖像處理能力。

核心圖像庫是為快速訪問幾種基本像素格式圖像設計的。它能為一般的圖像處理工具提供一個可靠的基礎。

這個Pythonic library可以

  • 裝載和保存多種格式文件 Loading and Saving images (diverse formats)
    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
  • 添加保存/裝載功能到程序 Adding save/load support into your application
  • 圖表 Chart generation (also see PIDDLE piddle.sf.net)
  • 生成縮略圖 Thumbnail generation
  • 水印和圖例 Watermarking or adding legend

基本概念 PIL API - Concepts


尺寸 Sizes

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

通過圖片對象的size屬性可以得到圖片的尺寸,結果這是一個二元組,包含水平和垂直方向上的像素數。

坐標 Coordinates

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

Pil采取左上角為(0,0)的坐標系統

角度 Angles
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.

邊界框 Bboxes(Bounding boxes)

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.

由四元組(x0, y0, x1, y1)定義的一個矩形區域。區域包括左上角(x0,y0),但不包括右下角(x1, y1)或者(x1, y1)所在行列的像素點。

通道 Bands – the separate color channels

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允許將這些通道進行疊加.

在RGB模式下,每個圖片由三個通道疊加而成,每個模式下為一個灰度圖,當有一個調色板來調色的時候,這三張灰度圖的疊加即可合成3*8位(每個像素)的一個真彩圖片。

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 
 

模式 Mode

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位浮點型像素

調色板 Palette
mode("P")為每個像素定義具體的顏色值

顏色 Color – 32位數值、元組或字符串 specified as 32bit value, tuple, or string

  • 單通道圖像-像素數值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.

濾鏡 Resampling Filters
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:

在對圖片的幾何操作中可能會將多個輸入像素映射到單個的輸出像素(減少像素量,比如說創建縮略圖),可以使用不同濾鏡來計算新的像素。pil提供4種不同的采樣濾鏡(在目前的版本中,後續的版本可能支持更多):

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.

例如,Image對象有三種使用filter的方法

1 resize(size,filter=None)
返回指定大小的一個新圖像對象,可以使用一個filter參數來指明內插方式,默認采用NEAREST

>>> 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.
有專門的ImageFilter模塊來支持Image enhancement。

字體 Fonts

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.

有專門的ImageFont模塊來支持ImageDraw模塊中.text()方法中的字體選項。

文件格式 Format

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

文件類型碼(Type列)出現在Image.format屬性和Image.save()方法中:

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

技術分享

常用操作


合成 Image.blend(i1,i2,a)/Image.composite(i1,i2,mask)

縮略圖 thumbnail(size,filter=None)
Modifies in-place,Preserves aspect ratio
>>> myImage.thumbnail ((128, 128), Image.ANTIALIAS)
剪切 crop(bbox)

>>> bounds = (100, 100, 400, 400)
>>> cutoutIm = myImage.crop (bounds)
粘貼 paste(i2,where,mask=None)/paste(color,box=None,mask=None)

旋轉 rotate(theta)
rotated around its center

翻轉旋轉 transpose(method)
ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)
>>> fixedIm = myImage.transpose (ROTATE_90)

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)

圖像對象 Image – from file or newly created

所有的圖片操作必須有一個操作對象,例如Pil提供open(filename)進行這個過程,此後,一切關於圖片的操作均基於這個對象。有以下幾種創建image對象的方式:

1 Image.open(f)

>>> import Image
>>>
>>> Im = Image.open("lena.jpg")
>>> print Im.mode,Im.size,Im.format
RGB (256, 256) JPEG
>>> Im.show()

技術分享

如果文件不能打開,會拋出IOError異常。

可以查看image對象的format,mode,size,palette,info幾個屬性。

調用im.show()會在圖片查看工具中顯示當前操作的image對象。

標準版本的show方法的實現不太高效,因為它先把image保存到一個臨時文件,然後調用xy工具來顯示圖像。如果你沒有安裝xy,那麽它就無法工作了。不過如果它可以工作,倒還是非常方便用來debug和測試。

2 Image.new(mode,size,color=None)

color的默認值是黑色,這裏我們新建一個紅色的圖像。

>>> newIm = Image.new (“RGBA”, (640, 480), (255, 0, 0)) #新建一個image對象creating images from scratch

3 Image.blend(i1,i2,a) -- (p1 x (1 - a) + p2 x a)

選一張灰度圖(L)做背景,和雷娜圖(RGB)做blend操作

技術分享

>>> Im2 = Image.open("background.jpg").convert(Im.mode)
>>> Im2 = Im2.resize(Im.size)
>>> Im2.show()
>>>
>>> img = Image.blend(Im,Im2,0.2)
>>> img.show()

技術分享

操作完畢後save(filename)用以保存這個臨時的image對象img到硬盤。

4 Image.composite(i1,i2,mask) --equal-sized images i1 ,i2 and mask("1", "L", or "RGBA") (p1 x (1 - m) + p2 x m)

5 Image.eval(f,i) -- applying a function f to each pixel of image i

6 Image.merge(mode,bandList) --Creates a multi-band image from a sequence of single-band images of equal size

以下是Image對象的全部方法:

save(f,format=None) 保存 如果f是一個file對象,必須指定format(format codes)
convert(mode) 轉換mode
copy()
crop(bbox) 剪切 原圖中bbox區域
filter(name) 濾鏡 the name of predefined image enhancement filters
濾鏡名字需要import ImageFilter
getbands() 通道的字符串序列 如RGB圖返回(‘R‘, ‘G‘, ‘B‘)
getbbox() 包含非零區域的最小bbox
getextrema() 最大最小像素點值 min&max pixel value
單通道圖:返回元組(min,max)
多通道圖:返回各個通道的元組組成的元組
getpixel(xy) 取像素點值 坐標xy處的pixel value or a sequence of pixel values
histogram(mask=None)

統計直方圖

單通道圖:返回列表[c0, c1, ...],ci是值為i的像素數

多通道圖:a single sequence that is the concatenation of the sequences for all bands

mask參數:a same-sized mask image of mode "1" or "L"(include only those pixels correspond to nonzero pixels in the mask argument)

offset(dx,dy=None)

平移

Returns a new image the same size as the original, but with all pixels rotated dx in the +x direction,and dy in the +y direction.

If dy is omitted, it defaults to the same value as dx.

paste(i2,where,mask=None) 粘貼圖片 where參數可以是
1 (x,y)坐標對:i2的像素點(0,0)對齊原圖中的(x,y)粘貼,i2超過原圖邊界的部分被拋棄
2 bbox:i2必須和該bounding box大小一致
3 None:i2必須和原圖大小一致
如果i2的mode和原圖不一致,粘貼前會被轉換。
mask參數:a same-sized mask image of mode "1","L" or “RGBA ”(control which pixels get replaced)
paste(color,box=None,mask=None) 填充顏色 如果box省略,整個圖被填充為color色;mask參數同上
point(function) 改變像素點(函數) Returns a new image with each pixel modified.
point(table) 改變像素點(查表) To translate pixels using a table(a sequence of 256n values, where n is the number of bands in the image) lookup
putalpha(band)

改變alpha通道

The pixels of the band image(same-sized,"L" or "1") replace the alpha band(A) of the original image(RGBA) in place.

putpixel(xy, color) 改變單個像素點顏色 Note that this method is relatively slow. For more extensive changes, use paste or the ImageDraw module instead.
resize(size,filter=None) 調整大小
rotate(theta)

旋轉(圍繞圖片中心)

Any pixels that are not covered by rotation of the original image are set to black.

show()

顯示圖片

On Unix systems, this method runs the xv image viewer to display the image.
On Windows boxes,the image is saved in BMP format and can be viewed using Paint.
This can be useful for debugging.

split()

分離通道

返回各個通道的灰度圖組成的元組
Returns a tuple containing each band of the original image as an image of mode "L".
For example, applying this method to an "RGB" image produces a tuple of three images, one each for the red, green, and blue bands.

thumbnail(size,filter=None) 縮略圖 Modifies in-place,Preserves aspect ratio
transform(xs, ys, Image.EXTENT, (x0,y0,x1,y1))

Returns a transformed copy of the image. In the transformed image, the point originally at (x0,y0) will appear at (0,0), and point (x1,y1) will appear at (xs, ys).

transform(xs, ys, Image.AFFINE, (a,b,c,d,e,f)) affine變換

The values a through f are the first two rows of an affine transform matrix.
Each pixel at (x,y) in the resulting image comes from position (ax+by+c,dx+ey+f) in the input
image, rounded to the nearest pixel.

transpose(method) 翻轉旋轉 ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)

The ImageDraw Module


支持2D圖像 The ImageDraw module provide basic 2D graphics support for Image objects.
It can for example be used to

  • create new images,
  • annotate or retouch existing images, and to generate graphics on the fly for web use.

For a more advanced drawing library for PIL, see The aggdraw Module.

創建繪畫對象 ImageDraw module creates drawing surface for image

import Image, ImageDraw
im = Image.open(“vacation.jpeg") 
drawSurface = ImageDraw.Draw(im)

基本繪畫操作 Basic methods of drawing surface

  • 弧/弦/扇形 chord arc pieslice (bbox, strtAng, endAng)
  • 橢圓 ellipse (bbox)
  • 線段/多段線 line (L) draw.line(((60,60),(90,60), (90,90), (60,90), (60,60))) #draw a square
  • 點 point (xy) #單像素點很小看不清,實際中可用實心小圓代替
  • 多邊形 polygon (L) draw.polygon([(60,60), (90,60), (90,90), (60,90)]) #draw a square
  • 矩形 rectangle (bbox) # first coord屬於矩形, second coord不屬於
  • 文字 text(xy,message,font=None) 繪制文字message,文本區域左上角坐標為xy
    drawable.text((10, 10), "Hello", fill=(255,0,0), font=None)
  • 文字大小 textsize(message,font=None) 給定文字message,返回所占像素(width,height)

可選參數 Common optional args for these methods

  • fill=fillColor
  • outline=outlineColor

矢量字體支持 TrueType Font support

import ImageFont
ttFont = ImageFont.truetype (“arial.ttf”, 16)
drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=ttFont)

例子:Draw a Grey Cross Over an Image

技術分享
import Image, ImageDraw im = Image.open("lena.pgm") # Creates an object that can be used to draw in the given image.
draw = ImageDraw.Draw(im) # draw.line(xy, options) => Draws a line between the coordinates in the xy list.

# The coordinate list can be any sequence object containing either 2-tuples [ (x, y), ... ] # or numeric values [ x, y, ... ]. # The fill option gives the color to use for the line.
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")
技術分享

The ImageChops module


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.

At this time, channel operations are only implemented for 8-bit images (e.g. "L" and "RGB").

例子:比較兩幅圖像

Exact Comparison:

The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returns None.

import ImageChops def equal(im1, im2): return ImageChops.difference(im1, im2).getbbox() is None

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the difference function, and then calculates the RMS value from the histogram of the resulting image.

RMS Difference:

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the difference function, and then calculates the RMS value from the histogram of the resulting image.

技術分享
# Example: File: imagediff.py

import ImageChops import math, operator def rmsdiff(im1, im2): "Calculate the root-mean-square difference between two images" h = ImageChops.difference(im1, im2).histogram() # calculate rms
    return math.sqrt(reduce(operator.add, map(lambda h, i: h*(i**2), h, range(256)) ) / (float(im1.size[0]) * im1.size[1]))
技術分享

Python圖像處理庫(PIL)