需要根据前面的发文,将重复图片放在另外一个文件夹,两个文件夹对比,第二个文件夹是不想要的图片。
import os
import numpy as np
from PIL import Image
#定义比较大小
def con_len(dir_image1, dir_image2):
with open(dir_image1, "rb") as f1:
size1 = len(f1.read())
with open(dir_image2, "rb") as f2:
size2 = len(f2.read())
if (size1 == size2):
result = "大小相同"
else:
result = "大小不同"
return result
#定义比较尺寸
def con_cc(dir_image1, dir_image2):
image1 = Image.open(dir_image1)
image2 = Image.open(dir_image2)
if (image1.size == image2.size):
result = "尺寸相同"
else:
result = "尺寸不同"
return result
#定义比较内容
def con_nr(dir_image1, dir_image2):
image1 = np.array(Image.open(dir_image1))
image2 = np.array(Image.open(dir_image2))
if (np.array_equal(image1, image2)):
result = "内容相同"
else:
result = "内容不同"
return result
#定义比较两张图片是否相同
def con_same(dir_image1, dir_image2):
# 比较两张图片是否相同
# 第一步:比较大小是否相同
# 第二步:比较长和宽是否相同
# 第三步:比较每个像素是否相同
# 如果前一步不相同,则两张图片必不相同
result = "两张图不同"
big = con_len(dir_image1, dir_image2)
if (big == "大小相同"):
cc = con_cc(dir_image1, dir_image2)
if (cc == "尺寸相同"):
nr = con_nr(dir_image1, dir_image2)
if (nr == "内容相同"):
result = "两张图相同"
return result
'''获取带路径的文件名,由于后续循环中需要删除文件,需要重新获取,因此这里定义成函数'''
def get_path_filenames():
# 使用 os.getcwd() 方法获取当前文件夹路径
current_folder = os.getcwd()
# 打印当前文件夹路径
print("当前文件夹路径:",current_folder)
# 定义需要处理和需要比对的文件夹路径
need_pic_path =current_folder+'\\'+ 'image'
re_pic_path = current_folder+'\\'+ 'image2'
#获取文件夹下待处理图片带路径名称
for root, dirs, files in os.walk(need_pic_path):
path_and_names_1=[]
for file_name in files:
path_and_name = os.path.join(root, file_name)
path_and_names_1.append(path_and_name)
print("所有待处理的带路径的文件名称:",path_and_names_1)
#获取文件夹下重复图片带路径名称
for root, dirs, files in os.walk(re_pic_path):
path_and_names_2=[]
for file_name in files:
path_and_name = os.path.join(root, file_name)
path_and_names_2.append(path_and_name)
print("所有待对比的带路径的文件名称:",path_and_names_2)
return(path_and_names_1,path_and_names_2)
path_and_names_1=get_path_filenames()[0]
path_and_names_2=get_path_filenames()[1]
for re_pic in path_and_names_2:
print('开始对比',re_pic)
for pic in path_and_names_1:
result=con_same(re_pic,pic)
if result=='两张图相同':
os.remove(pic)
path_and_names_1 = get_path_filenames()[0]
else:
pass