1. 程式人生 > >使用Python+PIL檢視兩張相似圖形的差異

使用Python+PIL檢視兩張相似圖形的差異

PIL(http://www.pythonware.com/products/pil/)中包含很多圖形處理庫,主要是Image,Image庫中包含三個類(Image、ImagePointHandler、ImageTransformHandler)及若干方法。

今天實驗了一把,開啟兩個差異很小的圖片,將其中一個反色處理,然後在將兩個影象混合,就能比較清晰的看到差異的地方。

唔~~~~,這要是擴充套件一下,增加截圖功能,可以做成“大家來找茬”之類遊戲的作弊器了!!阿門~~~

程式碼如下:

import Image, ImageChops
im1 = Image.open('c:/1.bmp')
im2 = Image.open('c:/2.bmp')
im3 = ImageChops.invert(im2)
Image.blend(im1,im3,0.5).show()


原始圖片如下:

1.bmp

2.bmp

混合後結果:

附:PIL.Image庫說明文件

 
 
PIL.Image

# 2009-11-15 fl   PIL release 1.1.7

 
Classes
Methods defined here:
convert(self, mode=None, data=None, dither=None, palette=0, colors=256)
Convert to other pixel format
copy(self)
Copy raster data
複製一個新的物件
crop(self, box=None)
Crop region from image
draft(self, mode, size)
Configure image decoder
filter(self, filter)
Apply environment filter to image
fromstring(self, data, decoder_name='raw', *args)
Load data to image from binary string
從原始影象資料中載入影象,一般用來從剪下板中獲取影象
Get band names
Get bounding box of actual data (non-zero pixels) in image
獲取當前影象在實際圖片中的範圍
getcolors(self, maxcolors=256)
Get colors from image, up to given limit
getdata(self, band=None)
Get image data as sequence object.
獲取影象資料列表。如果指定band,就獲取這個顏色通道的值得列表
print im.getdata()[0]
(253,23,34)
Get min/max value
getim(self)
Get PyCObject pointer to internal image memory
Get palette contents.
Get pixel value
獲取每個座標畫素點的RGB值。引數xy格式為(x,y)
Get projection to x and y axes
histogram(self, mask=None, extrema=None)
Take histogram of image
load(self)
Explicitly load pixel data.
offset(self, xoffset, yoffset=None)
(deprecated) Offset image in horizontal and/or vertical direction
paste(self, im, box=None, mask=None)
Paste other image into region
point(self, lut, mode=None)
Map image through lookup table
putalpha(self, alpha)
Set alpha layer
putdata(self, data, scale=1.0, offset=0.0)
Put data from a sequence object into an image.
putpalette(self, data, rawmode='RGB')
Put palette data into an image.
putpixel(self, xy, value)
Set pixel value
設定某座標畫素點值
quantize(self, colors=256, method=0, kmeans=0, palette=None)
resize(self, size, resample=0)
Resize image
重新設定影象大小
rotate(self, angle, resample=0, expand=0)
Rotate image.  Angle given as degrees counter-clockwise.
旋轉影象
save(self, fp, format=None, **params)
Save image to file or stream
儲存影象
seek(self, frame)
Seek to given frame in sequence file
定位到動畫某一幀,一般用於gif格式的檔案圖片
show(self, title=None, command=None)
Display image (for debug purposes only)
現實影象,呼叫預設的影象瀏覽器
split(self)
Split image into bands
將影象按照RGB分別解析,返回三個Image物件
tell(self)
Return current frame number
返回當前幀號
thumbnail(self, size, resample=0)
Create thumbnail representation (modifies image in place)
建立縮圖,主要用來修改影象大小和清晰度,以便生產較小的檔案
tobitmap(self, name='image')
Return image as an XBM bitmap
tostring(self, encoder_name='raw', *args)
Return image as a binary string
transform(self, size, method, data=None, resample=0, fill=1)
Transform image
Transpose image (flip or rotate in 90 degree steps)
Verify file contents.
Data and other attributes defined here:
format = None

mode

format_description = None
 
Functions
blend(im1, im2, alpha)
Interpolate between images.
將兩張圖片混合到一起。引數alpha是im1與im2的混合權重,範圍為[0,1]
composite(image1, image2, mask)
Create composite image by blending images using a transparency mask
eval(image, *args)
Evaluate image expression
fromarray(obj, mode=None)
frombuffer(mode, size, data, decoder_name='raw', *args)
Load image from string or buffer
fromstring(mode, size, data, decoder_name='raw', *args)
Load image from string
init()
Load all file format drivers.
isNumberType(a) -- Return True if a has a numeric type, False otherwise.
isSequenceType(a) -- Return True if a has a sequence type, False otherwise.
##
# (Internal) Checks if an object is a string.  If the current
# Python version supports Unicode, this checks for both 8-bit
# and Unicode strings.
merge(mode, bands)
Merge a set of single band images into a new multiband image.
new(mode, size, color=0)
Create a new image
建立一個Image物件
open(fp, mode='r')
Open an image file, without loading the raster data
開啟圖片檔案
Load standard file format drivers.
register_open(id, factory, accept=None)