@
jaylong src/
handler_photo_by_date.py#!/usr/bin/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
global Count
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 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 TrimTime2(mtime):
ts = time.mktime(time.strptime(mtime,'%Y-%m-%d %H:%M:%S'))
return time.strftime('%Y%m%d-%H%M%S',time.localtime(ts))
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
print filename, pic_date[filename]
return pic_date
def ArchivePic():
pic_date = GetPicExif()
Count = 0
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"%(output_path,TrimTime(mtime))
if not os.path.exists(new_dir):
os.mkdir(new_dir)
new_file_name = '%s-%s%s'%(pic_prefix,TrimTime2(mtime),pic_suffix)
new_file_path = '%s/%s'%(new_dir,new_file_name)
print "processing", pic, "to", 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.move(pic,new_file_path)
#shutil.copy2(pic,new_file_path)
#os.remove(pic)
Count+=1
print Count, std_out
else:
shutil.move(pic,new_file_path)
#shutil.copy2(pic,new_file_path)
#os.remove(pic)
Count+=1
print Count, std_out
except Exception, e:
print e
else:
while True:
new_file_name = '%s-%s_%s'%(pic_prefix,TrimTime2(mtime),pic_suffix)
if new_file_name not in os.listdir(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.move(pic,new_file_path)
#shutil.copy2(pic,new_file_path)
#os.remove(pic)
Count+=1
print Count, std_out
except Exception, e:
print e
else:
os.remove(pic)
return
if __name__ == "__main__":
input_path = r"/home/xxx/Camera"
output_path = r"/home/xxx/New"
fnexp = "*.jpg"
pic_suffix = ".jpg" #如果查找的是 jpg 文件,那文件后缀名也要匹配
pic_prefix = "IMG"
birth_day = '20191018'
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()