python比较两张图片是否一样_python 判断 图片是否相同

import os

from PIL import Image

from PIL import ImageFile

import imagehash

#仅适用于图片内容完全相同,而尺寸不同的比较

def compare_image(img_file1, img_file2):

if img_file1 == img_file2:

return True

fp1 = open(img_file1, 'rb')

fp2 = open(img_file2, 'rb')

img1 = Image.open(fp1)

img2 = Image.open(fp2)

ImageFile.LOAD_TRUNCATED_IMAGES = True

b = img1 == img2

fp1.close()

fp2.close()

return b

#通过图片hash值进行比较

def get_hash_dict(dir):

hash_dict = {}

image_quantity = 0

for _, _, files in os.walk(dir):

for i, fileName in enumerate(files):

with open(dir + fileName, 'rb') as fp:

hash_dict[dir + fileName] = imagehash.average_hash(Image.open(fp))

image_quantity += 1

return hash_dict, image_quantity

def compare_image_with_hash(image_file_name_1, image_file_name_2, max_dif=0):

"""

max_dif: 允许最大hash差值, 越小越精确,最小为0

推荐使用

"""

ImageFile.LOAD_TRUNCATED_IMAGES = True

hash_1 = None

hash_2 = None

with open(image_file_name_1, 'rb') as fp:

hash_1 = imagehash.average_hash(Image.open(fp))

with open(image_file_name_2, 'rb') as fp:

hash_2 = imagehash.average_hash(Image.open(fp))

dif = hash_1 - hash_2

if dif < 0:

dif = -dif

if dif <= max_dif:

return True

else:

return False

def compare_image_dir_with_hash(dir_1, dir_2, max_dif=0):

"""

max_dif: 允许最大hash差值, 越小越精确,最小为0

用于比较两个文件夹内的图片

"""

ImageFile.LOAD_TRUNCATED_IMAGES = True

hash_dict_1, image_quantity_1 = get_hash_dict(dir_1)

hash_dict_2, image_quantity_2 = get_hash_dict(dir_2)

if image_quantity_1 > image_quantity_2:

tmp = image_quantity_1

image_quantity_1 = image_quantity_2

image_quantity_2 = tmp

tmp = hash_dict_1

hash_dict_1 = hash_dict_2

hash_dict_2 = tmp

result_dict = {}

for k in hash_dict_1.keys():

result_dict[k] = None

for dif_i in range(0, max_dif + 1):

have_none = False

for k_1 in result_dict.keys():

if result_dict.get(k_1) is None:

have_none = True

if not have_none:

return result_dict

for k_1, v_1 in hash_dict_1.items():

for k_2, v_2 in hash_dict_2.items():

sub = (v_1 - v_2)

if sub < 0:

sub = -sub

if sub == dif_i and result_dict.get(k_1) is None:

result_dict[k_1] = k_2

break

return result_dict

def main():

print(compare_image('image1\\815.jpg', 'image2\\5.jpg'))

print(compare_image_with_hash('image1\\815.jpg', 'image2\\5.jpg', 6))

r = compare_image_dir_with_hash('image1\\', image2\\', 10)

for k in r.keys():

print(k, r.get(k))

if __name__ == '__main__':

main()

运行结果:

False

True

image2\5.jpg image1\815.jpg

image2\6.jpg image1\819.jpg

image2\7.jpg image1\900.jpg

image2\8.jpg image1\998.jpg

image2\9.jpg image1\1012.jpg

图片例子

815.jpg

1043898-20180110180003941-1601370471.jpg

5.jpg

1043898-20180110180034035-1951768962.jpg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值