图片比较

import os

from PIL import Image, ImageChops


def compare_images(path_one, path_two):
image_one = Image.open(path_one)
image_two = Image.open(path_two)
try:
diff = ImageChops.difference(image_one, image_two)
except Exception as e:
print(image_one, image_two, e)
return

if diff.getbbox() is None:
# 图片间没有任何不同则直接退出
return path_one, path_two
else:
pass


def list_dir(remote_dir):
all_files = []
files = os.listdir(remote_dir)
for x in files:
filename = remote_dir + '/' + x
if os.path.isdir(filename):
all_files.extend(list_dir(filename))
else:
if not x.endswith('.jpg'):
continue
if os.path.isfile(filename):
all_files.append(filename)
return all_files


if __name__ == '__main__':
base_dir = "xxx"
file_list = list_dir(base_dir)
ret_list = []
for item in file_list:
a_path = os.path.basename(item)
for sec in file_list:
b_path = os.path.basename(sec)
if sec != item:
src_list = compare_images(item, sec)
if src_list:
ret_list.append(src_list)
print(ret_list)
posted @ 2017-11-21 15:26  hyu9999  Views(171)  Comments(0)    收藏  举报