Last active
June 11, 2020 18:57
-
-
Save amorphiccat/8344ae5020a0ec1052c592b38d90d966 to your computer and use it in GitHub Desktop.
맥용 퍼슈트 사진 리네이머
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
import re | |
import random | |
import string | |
import sys | |
import os | |
import mac_tag | |
from PIL import Image | |
def extfilter(x): | |
if x[1] == '.JPG': | |
return True | |
if x[1] == '.jpg': | |
return True | |
if x[1] == '.jpeg': | |
return True | |
if x[1] == '.gif': | |
return True | |
if x[1] == '.png': | |
return True | |
return False | |
def randstr(): | |
letters = string.ascii_lowercase | |
return ''.join(random.choice(letters) for i in range(16)) | |
def newname(eventname, photographer, index, ext): | |
return eventname + '_' + photographer + '_' + str(index).zfill(3) + '.' + ext | |
# read image files and sort by timestamp | |
# eventname -> [(filename, timestamp)] sorted | |
def readfile(eventname): | |
names = map(lambda x: os.path.splitext(x), os.listdir()) | |
filtered = filter(extfilter, names) | |
pairs = [] | |
for x in list(filtered): | |
filename = x[0]+x[1] | |
timestamp = '2000:00:00 00:00:00' | |
imagefile = Image.open(filename) | |
if hasattr(imagefile, "_getexif"): | |
exif = imagefile._getexif() | |
if not exif is None: | |
timestamp = exif.get(0x9004, '2000:00:00 00:00:00') | |
pairs.append((filename, timestamp)) | |
pairs.sort(key = lambda x: x[1]) | |
result = [] | |
for x in pairs: | |
result.append(x[0]) | |
return result | |
# read tag, extract photographer information | |
# [filename] sorted -> [(filename, photographer, id)] | |
def filtertags(filenames): | |
result = [] | |
counter = {} | |
for filename in filenames: | |
absname = os.path.abspath(filename) | |
tags = list(mac_tag.get(absname).items())[0][1] | |
if 'Important' in tags: tags.remove('Important') | |
if len(tags) == 0: | |
continue | |
photographer = tags[0] | |
index = counter.get(photographer, 0) | |
result.append((filename, photographer, index)) | |
counter[photographer] = index + 1 | |
return result | |
# allocate temporal names for wrongly renamed entries | |
# exclude correctly renamed entries | |
# [(filename, photographer, id)] sorted -> [(filename, photographer, id)] sorted | |
def clearname(pairs, eventname): | |
result = [] | |
for x in pairs: | |
filename, photographer, index = x[0], x[1], x[2] | |
if filename.startswith(eventname): | |
ext = filename.split('.')[1] | |
if filename != newname(eventname, photographer, index, ext): | |
newfilename = randstr()+'.'+ext | |
print("mv " + filename + " " + newfilename) | |
os.rename(filename, newfilename) | |
result.append((newfilename, photographer, index)) | |
else: | |
result.append((filename, photographer, index)) | |
return result | |
# allocate new names | |
# [(filename, photographer, id)] sorted -> eventname -> () | |
def allocate(pairs, eventname): | |
for x in pairs: | |
filename, photographer, index = x[0], x[1], x[2] | |
ext = filename.split('.')[1] | |
newfilename = newname(eventname, photographer, index, ext) | |
cmd = 'mv \'' + filename + '\' \'' + newfilename+ '\'' | |
os.system(cmd) | |
print(cmd) | |
def rename(eventname): | |
files = readfile(eventname) | |
files2 = filtertags(files) | |
files3 = clearname(files2, eventname) | |
allocate(files3, eventname) | |
def reccall(fn, eventname): | |
fn(eventname) | |
folders = [x.path for x in os.scandir('.') if x.is_dir()] | |
for x in folders: | |
os.chdir(x) | |
print("cd " + x) | |
subeventname = re.findall(r"[a-zA-Z]+", x)[0] | |
print("Event: " + subeventname) | |
reccall(fn, subeventname) | |
os.chdir('..') | |
print("cd ..") | |
# recursively call rename() | |
def renameall(eventname): | |
reccall(rename, '') | |
def recover(eventname): | |
regex = r"\.jpg|\.jpeg|\.gif|\.png|\.JPG" | |
names = os.listdir() | |
filtered = filter(lambda x: len(list(re.findall(regex, x))) != 0, names) | |
withext = map(lambda x: (x, re.findall(regex, x)[0]), filtered) | |
for x in list(withext): | |
name, ext = x[0], x[1] | |
splited = name.split(ext) | |
if len(splited) == 1: continue | |
realname = splited[0] | |
cmd = 'mv \'' + name + '\' \'' + realname + ext + '\'' | |
print(cmd) | |
os.system(cmd) | |
def recoverall(eventname): | |
reccall(recover, '') | |
renameall('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment