圖片占用工程中比較大的資源歹啼,清理重復(fù)圖片是快速減少工程大小的方式密强。詳細(xì)代碼見(jiàn)下:
from PIL import Image
import math
import operator
from functools import reduce
import shutil
import os
import simplejson
path = ''
#所有圖片的路徑
imgPaths = []
#存儲(chǔ)有相同圖片的key:imgName,value:[imgPath]
samePictureDic = {}
count = 0
# 查找所有文件
def searchDirFile(dir):
listfile = os.listdir(dir)
filepath = dir
for file in listfile: # 把目錄下的文件都賦值給line這個(gè)參數(shù)
if os.path.isdir(filepath + "/" + file):
searchDirFile(filepath + '/' + file)
else:
fileAllPath = os.path.join(filepath,file)
if file.endswith(".png") or file.endswith(".PNG") or file.endswith(".jpg") or file.endswith(".jpeg"):
imgPaths.append(fileAllPath)
def compareImages():
global count
#臨時(shí)存儲(chǔ)
tempImgs = imgPaths[:]
for i in range(0,len(imgPaths) - 1):
if len(imgPaths) < 1:
return
#要比較的圖片
img1Path = imgPaths[i]
print("\n\n\n現(xiàn)在進(jìn)行到:%d *****圖片路徑:%s\n"%(i,img1Path))
#是否有相同圖片
samed = False
#存儲(chǔ)相同圖片路徑
samePaths = []
if img1Path in tempImgs:
# 從臨時(shí)數(shù)組中刪除
tempImgs.remove(img1Path)
if len(tempImgs) > 1:
for j in range(0,len(tempImgs)):
# print(j)
#對(duì)比的圖片
img2Path = tempImgs[j]
if img1Path != img2Path:
if compareImage(img1Path,img2Path):
# print("相同")
#圖片相同
samed = True
#添加相同圖片路徑
samePaths.append(img2Path)
else:
pass
# print("不相同")
if samed:
# print('yes')
#有相同圖片添加圖片1的路徑
print("相同圖片的路徑")
samePaths.append(img1Path)
print(samePaths)
count += 1
print(len(samePaths))
fileName = os.path.basename(img1Path)
names = fileName.split(".")
imgName = '%s%d' % (names[0], count)
for k in range(0,len(samePaths)):
print(k)
path = samePaths[k]
# 目錄源文件
sourceFilePath = '自己存儲(chǔ)路徑/圖片比較/%s_%d.png' % (names[0], k)
# 拷貝到指定目錄
shutil.copy(path, sourceFilePath)
#存儲(chǔ)到j(luò)son中
samePictureDic[imgName] = samePaths
if path in tempImgs:
#從臨時(shí)數(shù)組中刪除
tempImgs.remove(path)
else:
print("沒(méi)有相同圖片")
#遍歷完畢拷窜,把json寫(xiě)入文件
imagesText = open('自己存儲(chǔ)路徑/圖片比較/TheSamePicure.json', 'w')
str = simplejson.dumps(samePictureDic)
imagesText.write(str + '\n')
#比較兩個(gè)圖片是否一樣
def compareImage(img1,img2):
image1 = Image.open(img1)
image2 = Image.open(img2)
print(img2)
# 把圖像對(duì)象轉(zhuǎn)換為直方圖數(shù)據(jù)旷祸,存在list h1叉庐、h2 中
h1 = image1.histogram()
h2 = image2.histogram()
# result的值越大宏多,說(shuō)明兩者的差別越大疯攒;如果result=0,則說(shuō)明兩張圖一模一樣
result = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a - b) ** 2, h1, h2))) / len(h1))
if (result < 0.001):
# print('相同')
return True
else:
return False
if __name__ == '__main__':
searchDirFile(path)
print("共有多少圖片%d" % (len(imgPaths)))
compareImages()