用python实现对比两张图片的不同的方法

时间:2018-02-06关注公众号来源:网络

  ?

用python实现对比两张图片的不同的方法

1

2
3
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Stream Vera Sans Mono', 'Courier New', Courier, monospace !important; FLOAT: none !important; BORDER-TOP-WIDTH: 0px !important; BORDER-BOTTOM-WIDTH: 0px !important; HEIGHT: auto !important; COLOR: rgb(0,102,153) !important; CLEAR: both; VERTICAL-ALIGN: baseline !important; OVERFLOW: visible !important; TOP: auto !important; RIGHT: auto !important; FONT-WEIGHT: bold !important; PADDING-TOP: 0px; LEFT: auto !important; border-radius: 0px; background-size: initial; background-origin: initial; background-clip: initial" class="py keyword">fromPIL importImage
fromPIL importImageChops
defcompare_images(path_one, path_two, diff_save_location):
  """
  比较图片,如果有不同则生成展示不同的图片
  @参数一: path_one: 第一张图片的路径
  @参数二: path_two: 第二张图片的路径
  @参数三: diff_save_location: 不同图的保存路径
  """
  image_one =Image.open(path_one)
  image_two =Image.open(path_two)
  try:
    diff =ImageChops.difference(image_one, image_two)
    ifdiff.getbbox() isNone:
    # 图片间没有任何不同则直接退出
      print("【+】We are the same!")
    else:
      diff.save(diff_save_location)
  exceptValueError as e:
    text =("表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image."
        "The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, "
        "right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted "
        "image must match the size of the region.使用2纬的box避免上述问题")
    print("【{0}】{1}".format(e,text))
if__name__ =='__main__':
  compare_images('1.png',
          '2.png',
          '我们不一样.png')

  执行结果:

用python实现对比两张图片的不同的方法

用python实现对比两张图片的不同的方法

用python实现对比两张图片的不同的方法

用python实现对比两张图片的不同的方法

  第二种方法:

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fromPIL importImage
importmath
importoperator
fromfunctools importreduce
defimage_contrast(img1, img2):
  image1 =Image.open(img1)
  image2 =Image.open(img2)
  h1 =image1.histogram()
  h2 =image2.histogram()
  result =math.sqrt(reduce(operator.add, list(map(lambdaa,b: (a-b)**2, h1, h2)))/len(h1) )
  returnresult
if__name__ =='__main__':
  img1 ="./1.png"# 指定图片路径
  img2 ="./2.png"
  result =image_contrast(img1,img2)
  print(result)

  如果两张图片完全相等,则返回结果为浮点类型“0.0”,如果不相同则返回结果值越大。

阅读全文
扫码关注“ 多特资源库
更多更全的软件资源下载
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
玩家热搜

相关攻略

正在加载中
版权
版权说明

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)

电话:13918309914

QQ:1967830372

邮箱:[email protected]

toast