Fly to the sky & Return

Exfread를 이용한 EXIF data 추출 및 날짜별 폴더 생성해서 사진 옮기기 .....2 본문

프로그래밍/파이썬

Exfread를 이용한 EXIF data 추출 및 날짜별 폴더 생성해서 사진 옮기기 .....2

낼은어떻게 2018. 3. 19. 12:21
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

기존 코드에 에러처리가 없어서인지..   중간에 멈추는 짜증나는 상황이 많이 발생을 하더라구영..

그래서이번에는 에러처리를 좀 한  코드입니다.


코딩 및 실행환경

OS : mac 10.12.6

python : 2.7

IDE : jupyter notebook


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from Tkinter import Tk
import Tkinter, Tkconstants, tkFileDialog
import os, shutil
import exifread
import time
import datetime
 
def get_Exif_Date(path):
    dt_value = None
    f = open(path, 'rb')
    try:
        tags = exifread.process_file(f)
        for tag in tags.keys():
            if tag == 'EXIF DateTimeOriginal':
                dt_value = '%s' % tags[tag]
            
                
        if dt_value:
            exif_time = time.strptime(dt_value + 'UTC''%Y:%m:%d %H:%M:%S%Z')
            
            return exif_time
    finally:
        f.close()
    return None
 
def day_check(exif_day):
    if len(str(exif_day.tm_mday)) == 1:
        day = "0"+ str(exif_day.tm_mday)
    else:
        day = str(exif_day.tm_mday)
        
    if len(str(exif_day.tm_mon)) == 1:
        month = "0"+ str(exif_day.tm_mon)
    else:
        month = str(exif_day.tm_mon)
   
    return month, day
 
root = Tkinter.Tk()
 
root.withdraw()
 
myPath = tkFileDialog.askdirectory()
 
os.makedirs(myPath+'/'+ "non_exif_date"# exif_date data가 없는 파일 갈 디렉토리
 
for f in os.listdir(myPath):
    k=f.lower()
    total_path = myPath +'/'+ k
    try:
    
        exif_time = get_Exif_Date(total_path)
        
        if exif_time != None:
 
            dday = day_check(exif_time)
 
            exif_date = str(exif_time.tm_year) +"_"+ dday[0+"_"+ dday[1]
    
            if not os.path.exists(myPath+'/'+exif_date):
                os.makedirs(myPath+'/'+ exif_date)
                old_file = os.path.join(myPath, k)
                new_file = os.path.join(myPath+'/'+ exif_date, k)
                os.rename(old_file, new_file)
            else:
                old_file = os.path.join(myPath, k)
                new_file = os.path.join(myPath+'/'+ exif_date, k)
                shutil.move(old_file,new_file)
        else:
            old_file = os.path.join(myPath, k)
            new_file = os.path.join(myPath+'/'+ "non_exif_date", k)
            shutil.move(old_file,new_file)
    except Exception as e:
        print(e)
cs