下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
# -*- Coding:utf-8 -*-__author__ = 'barry'from PIL import Imagefrom PIL.ExifTags import Tagsfrom datetime import datetimefrom hashlib import md5import timeimport osimport shutilimport fnmatchimport randomdef 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 strdef 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_pathdef 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 Falsedef 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_datedef ArchivePic(): pic_date = GetPicExif() new_dir_file_md5_List = [] for pic,mtime in pic_date.items(): std_out = "3[1;33mAdd new picture 3[1;31m%s3[0m3[1;33m to dest path:%s3[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:\users\xxr12\onedrive\pictures" #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()
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的按日期对照片进行分类整理全部内容,希望文章能够帮你解决按日期对照片进行分类整理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)