刪除相同圖片的Python實現代理

jopen 12年前發布 | 16K 次閱讀 Python 圖形/圖像處理
  1. 原理:讀取圖片二進制碼,使用MD5或SHA-1散列,生成圖片唯一編碼,與圖片字典做比對,存在則圖片重復,移除.
  2. Python實現:
    import os
    import re
    import hashlib
    from time import time
    
    rootPath = 'F:/Image/照片'
    backupPath = 'F:/Image/backup'
    picDic = {}
    regular = re.compile(r'^(.*)\.(jpg|jpeg|bmp|gif|png|JPG|JPEG|BMP|GIF|PNG)$')
    
    def RemoverRePic(dirPath):
        quantity = 0
        for childPath in os.listdir(unicode(dirPath)):
            childPath = dirPath + '/'  + childPath
            if os.path.isdir(childPath):
                quantity =+ RemoverRePic(childPath)
            else:
                if regular.match(childPath):
                    pic = open(childPath, 'rb')
                    picMd5 = hashlib.md5(pic.read()).hexdigest()
                    pic.close()
                    if picDic.has_key(picMd5):
                        newPath = backupPath + '/'  + hashlib.md5(childPath)\
                        .hexdigest() + childPath[childPath.find('.'):]
                        os.rename(childPath, newPath)
                        quantity =+ 1
                    else:
                        picDic[picMd5] = childPath
        return quantity
    
    if __name__ == '__main__':
        t = time()
        print 'start:'
        print t
        print RemoverRePic(rootPath)
        print 'end:'
        print time() - t
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!