使用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


### 回答1: 要比较两张图像的相似度,可以使用Python中的图像处理库,如OpenCV或Pillow。以下是一些常见的方法: 1. 均方误差(MSE):计算两张图像像素值之间的差异平方的平均值。 2. 结构相似性指数(SSIM):计算两张图像结构、亮度和对比度之间的差异。 3. 相关系数:计算两张图像之间的线性相关性。 4. 直方图相似性:比较两张图像的灰度直方图。 5. 特征匹配:使用特征检测算法(如SIFT、SURF或ORB)检测两张图像中的关键点,并匹配它们之间的描述符。 这些方法中的每一种都有其优缺点,具体的选择取决于应用场景和需要达到的效果。 ### 回答2: Python图形相似度是指利用Python语言编写的程序来计算和比较不同图形之间的相似程度。图形相似度主要用于图像处理、计算机视觉和模式识别领域。 在Python中,可以使用各种图像处理库和算法来计算图形相似度。其中一种常用的方法是使用图像特征描述算法,例如局部二值模式(Local Binary Patterns,LBP)、方向梯度直方图(Histogram of Oriented Gradients,HOG)等。这些算法能够提取图像中的特征,并将其表示为数值形式,然后通过比较这些特征向量来计算相似度。 另外,还可以使用深度学习算法来计算图形相似度,例如卷积神经网络(Convolutional Neural Network,CNN)等。通过将图像输入训练好的神经网络中,可以得到一个表示图像特征的向量,然后通过比较这些向量来计算相似度。 除了特征描述算法和深度学习算法,还可以使用其他的图像相似度计算方法,例如结构相似性指数(Structural Similarity Index,SSIM)、峰值信噪比(Peak Signal-to-Noise Ratio,PSNR)等。这些方法对比图像的亮度、对比度和结构等方面进行比较,从而得出相似度的评估。 总之,Python图形相似度是利用Python编写的程序来计算和比较不同图形之间的相似程度。通过使用图像处理库和算法,可以提取图像中的特征,并将其表示为数值形式,然后通过比较这些特征向量来计算相似度。此外,还可以使用深度学习算法和其他相似度计算方法来实现图形相似度的计算。 ### 回答3: Python图形相似度是指通过Python程序来衡量和比较两个图形之间的相似程度。在计算机视觉和图像处理领域,图形相似度是一个重要的概念,可以用来判断两幅图像之间的差异程度,或者用于图像识别和图像匹配等任务。 Python提供了一些强大的库和工具,可以帮助我们计算图形相似度。其中最常用的是OpenCV库,它提供了许多图像处理和计算机视觉的功能,可以进行图像的比较和相似性分析。通过OpenCV,我们可以计算两个图像之间的结构相似性指数(SSIM),该指数可以衡量两个图像在结构和亮度方面的相似程度。 除了OpenCV之外,还有一些基于Python的图像特征提取和相似度计算的库,如scikit-imagePILPython Imaging Library)。这些库可以帮助我们提取图像的特征,比如色彩、纹理和形状等,并计算出两个图像之间的相似度评分。 在实际应用中,图形相似度可以用于许多领域,比如图像搜索、图像去重、人脸识别和目标跟踪等。通过使用Python编程,我们可以轻松地实现这些功能,并对图像数据进行相似度的计算和分析。 总之,Python图形相似度是通过使用Python编程实现的一种用于衡量和比较两个图形之间相似程度的方法。通过使用Python的图像处理和计算机视觉库,我们可以轻松地计算图像的相似度,并应用于各种实际应用中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值