使用Python+PIL查看两张相似图形的差异
[日期:2011-09-13] |
来源:Linux社区
作者:jt6562 |
[字体:大 中 小]
|
PIL(http://www./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库说明文档
# 2009-11-15 fl PIL release 1.1.7
Classes |
|
|
- Image
- ImagePointHandler
- ImageTransformHandler
class Image |
|
|
Methods defined here:
- __getattr__(self, name)
- __init__(self)
- __repr__(self)
- 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
- 从原始图像数据中加载图像,一般用来从剪切板中获取图像
- getbands(self)
- Get band names
- getbbox(self)
- 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)
|
getextrema(self)
- Get min/max value
- getim(self)
- Get PyCObject pointer to internal image memory
- getpalette(self)
- Get palette contents.
- getpixel(self, xy)
- Get pixel value
- 获取每个坐标像素点的RGB值。参数xy格式为(x,y)
- getprojection(self)
- 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(self, method)
- Transpose image (flip or rotate in 90 degree steps)
- verify(self)
- 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
- getmodebandnames(mode)
- getmodebands(mode)
- getmodebase(mode)
- getmodetype(mode)
- init()
- Load all file format drivers.
- isDirectory(f)
- isImageType(t)
- isNumberType(...)
- isNumberType(a) -- Return True if a has a numeric type, False otherwise.
- isSequenceType(...)
- isSequenceType(a) -- Return True if a has a sequence type, False otherwise.
- isStringType(t)
- ##
# (Internal) Checks if an object is a string. If the current # Python version supports Unicode, this checks for both 8-bit # and Unicode strings.
- isTupleType(t)
- 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
- 打开图片文件
- preinit()
- Load standard file format drivers.
- register_extension(id, extension)
- register_mime(id, mimetype)
- register_open(id, factory, accept=None)
- register_save(id, driver)
|
|