按日期对照片进行分类整理
发布时间:2020-12-17 17:09:50 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # -*- coding:utf-8 -*-__author__ = 'Barry'from PIL import Imagefrom PIL.ExifTags import TAGSfrom datetime import datetimefrom hashlib import
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 # -*- coding:utf-8 -*- __author__ = 'Barry' from PIL import Image from PIL.ExifTags import TAGS from datetime import datetime from hashlib import md5 import time import os import shutil import fnmatch import random def IterFindFiles(path,fnexp): for root,dirs,files in os.walk(path): for filename in fnmatch.filter(files,fnexp): yield os.path.join(root,filename) def GenRandomStr(): str = '' chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' for num in range(10): str += random.choice(chars) return str def GetPicPath(): pic_full_path = [] if os.path.isdir(input_path): for i in os.listdir(input_path): sub_dir = input_path + '/' + i if os.path.isdir(sub_dir): for n in os.listdir(sub_dir): pic_full_path.append(sub_dir + '/' + n) return pic_full_path def print_all_know_exif_tags(): for k in sorted(TAGS): print k,TAGS[k] def FormatTime(date): try: ts = time.mktime(time.strptime(date,'%Y:%m:%d %H:%M:%S')) return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(ts)) except Exception,e: return False def TrimTime(mtime): ts = time.mktime(time.strptime(mtime,'%Y-%m-%d %H:%M:%S')) return time.strftime('%Y%m%d',time.localtime(ts)) def CalcDays(mtime,birth_day): time_fmt = '%Y%m%d' days_born = (datetime.strptime(mtime,time_fmt) - datetime.strptime(birth_day,time_fmt)).days return str(days_born + 1) def GenMd5(filename): file_tmp = open(filename,'rb') md5_value = md5(file_tmp.read()) file_tmp.close() return md5_value.hexdigest() def GetPicExif(): pic_date = {} #pic_path = GetPicPath() for filename in IterFindFiles(input_path,fnexp): mtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(filename).st_mtime)) try: img = Image.open(filename) except Exception,e: print filename,"skipping due to ",e try: exif_data = img._getexif() except Exception,e: print filename,e finally: if exif_data: if exif_data.has_key(36867): pic_date[filename] = FormatTime(exif_data[36867]) and FormatTime(exif_data[36867]) or mtime elif exif_data.has_key(36868): pic_date[filename] = FormatTime(exif_data[36868]) and FormatTime(exif_data[36868]) or mtime elif exif_data.has_key(306): pic_date[filename] = FormatTime(exif_data[306]) and FormatTime(exif_data[306]) or mtime else: pic_date[filename] = mtime else: pic_date[filename] = mtime return pic_date def ArchivePic(): pic_date = GetPicExif() new_dir_file_md5_list = [] for pic,mtime in pic_date.items(): std_out = " 33[1;33mAdd new picture 33[1;31m%s 33[0m 33[1;33m to dest path:%s 33[0m"%(os.path.split(pic)[1],output_path) new_dir = "%s/%s-(%sdays)"%(output_path,TrimTime(mtime),CalcDays(TrimTime(mtime),birth_day)) if not os.path.exists(new_dir): os.mkdir(new_dir) new_file_name = '%s-%s-%s%s'%(pic_prefix,GenRandomStr(),pic_suffix) new_file_path = '%s/%s'%(new_dir,new_file_name) if not os.path.exists(new_file_path): try: if os.listdir(new_dir): for i in os.listdir(new_dir): hashvalue = GenMd5(os.path.join(new_dir,i)) new_dir_file_md5_list.append(hashvalue) if GenMd5(pic) not in new_dir_file_md5_list: shutil.copy2(pic,new_file_path) print std_out else: shutil.copy2(pic,new_file_path) print std_out except Exception,e: print e else: while True: new_file_name = '%s-%s-%s%s'%(pic_prefix,pic_suffix) if new_file_name not in os.lisdir(new_dir): break else: continue new_file_path = '%s/%s'%(new_dir,new_file_name) for i in os.listdir(new_dir): hashvalue = GenMd5(os.path.join(new_dir,i)) new_dir_file_md5_list.append(hashvalue) if GenMd5(pic) not in new_dir_file_md5_list: try: shutil.copy2(pic,new_file_path) print std_out except Exception,e: print e return if __name__ == "__main__": #input_path = r"D:baby/" input_path = r"C:usersxxr12onedrivepictures" #input_path = r"E:MemArchPic" output_path = r"E:Mybaby" fnexp = "*.jpg" pic_suffix = ".jpg" #如果查找的是jpg文件,那文件后缀名也要匹配 pic_prefix = "MYBABY" birth_day = '20150621' if not os.path.exists(output_path): os.mkdir(output_path) if not os.path.exists(input_path): print "some of path not found!" else: ArchivePic() #print_all_know_exif_tags() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |