使用Python+PIL查看两张相似图形的差异

本文介绍使用Python的PIL库进行图像处理的方法,包括图像对比、混合等操作,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

      

 

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

 

 
classImagePointHandler

   

 

 

 

 
classImageTransformHandler

   

 

 

 

 
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)

 

引自:http://www.linuxidc.com/Linux/2011-09/42857.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值