国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用python實現對比兩張圖片的不同

2020-02-22 23:07:57
字體:
來源:轉載
供稿:網友

from PIL import Imagefrom PIL import ImageChops def compare_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)    if diff.getbbox() is None:    # 圖片間沒有任何不同則直接退出      print("【+】We are the same!")    else:      diff.save(diff_save_location)  except ValueError 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')

執行結果:

第二種方法:

from PIL import Imageimport mathimport operatorfrom functools import reducedef image_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(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )  return resultif __name__ == '__main__':  img1 = "./1.png" # 指定圖片路徑  img2 = "./2.png"  result = image_contrast(img1,img2)  print(result)

如果兩張圖片完全相等,則返回結果為浮點類型“0.0”,如果不相同則返回結果值越大。

同樣用上面兩張圖片,執行結果為38,還是比較小的:

這樣就可以在自動化測試用例中調用該方法來斷言執行結果。

關于Pillow庫的詳細文檔:

http://pillow.readthedocs.org/en/latest/index.html

總結

以上所述是小編給大家介紹的用python實現對比兩張圖片的不同,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林站長站網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梅河口市| 平阴县| 松潘县| 江安县| 芦山县| 民和| 永济市| 黄大仙区| 清丰县| 绥中县| 固始县| 大姚县| 永仁县| 平南县| 桃江县| 谷城县| 清远市| 平舆县| 偏关县| 巴南区| 义乌市| 铜川市| 明溪县| 搜索| 瓮安县| 雷波县| 和龙市| 德保县| 富顺县| 县级市| 长宁县| 兴宁市| 洞口县| 松滋市| 交城县| 铜梁县| 铜梁县| 伊川县| 天峨县| 黔西| 金平|