175 字
1 分鐘
python 檢測圖片是否相同

介紹#

如果使用擷取圖片有兩張圖片,圖 1 及圖 2 可判斷出是否相同或不相同,

使用套件:(在新分頁開啟)https://github.com/JohannesBuchner/imagehash(在新分頁開啟)

  • 平均雜湊(aHash)
  • 感知雜湊(pHash)
  • 差異雜湊(dHash)
  • 小波雜湊(wHash)

ImageHash 主要用於計算圖片 hash 值,值為 16 進位制數,可用於圖片相似度比對。

>>> from PIL import Image
>>> import imagehash
>>> hash = imagehash.average_hash(Image.open('test.png'))
>>> print(hash)
d879f8f89b1bbf
>>> otherhash = imagehash.average_hash(Image.open('other.bmp'))
>>> print(otherhash)
ffff3720200ffff
>>> print(hash == otherhash) #如果兩者圖片相同會是 True
False
>>> print(hash - otherhash) #如果兩者圖片相同會是 0
36

參考文章:(在新分頁開啟)https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/599902/(在新分頁開啟)

python 檢測圖片是否相同
https://aishuafei.com/posts/python-檢測圖片是否相同/
作者
愛耍廢
發佈於
2020-03-01
許可協議
CC BY-NC-SA 4.0