python"># -*- 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,"skipping due to ",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 = "\033[1;33mAdd new picture \033[1;31m%s\033[0m\033[1;33m to dest path:%s\033[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,TrimTime(mtime),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,TrimTime(mtime),GenRandomStr(),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()
标签:python