Skip to content

Instantly share code, notes, and snippets.

@jeepkd
Created March 9, 2017 07:33
Show Gist options
  • Save jeepkd/85eaafd1b9157229810a9c3556d6bced to your computer and use it in GitHub Desktop.
Save jeepkd/85eaafd1b9157229810a9c3556d6bced to your computer and use it in GitHub Desktop.
#%%
import glob
import numpy as np
import pandas as pd
import exifread
import imagehash
import IPython.display
from PIL import Image
from datetime import datetime
from shutil import copyfile
#%%
photo_names = glob.glob('merged_photos/*.jpg')
target_dir = 'renamed_photos'
def photo_datetime(filename):
with open(filename, 'rb') as photo:
taken = exifread.process_file(photo)['EXIF DateTimeOriginal'].values
return datetime.strptime(taken, "%Y:%m:%d %H:%M:%S")
def photo_hash(filename):
with Image.open(filename) as photo:
return imagehash.dhash(photo)
#%%
df = pd.DataFrame(photo_names, columns=["name"])
df['datetime'] = [photo_datetime(filename) for filename in photo_names]
df.sort(['datetime','name'], inplace=True)
df.reset_index(inplace=True, drop=True)
df
#%%
df['dhash'] = [photo_hash(filename) for filename in photo_names]
df
#%%
df['new_name'] = f"{target_dir}/fb_photo_" \
+ df['datetime'].map(lambda x: x.strftime('%Y-%m-%d-%H-%M-%S')) \
+ "_dhash_" + df['dhash'].map(str) \
+ '.jpg'
df
# for p in df['name'][1200:]:
# IPython.display.display(IPython.display.Image(p))
for index, row in df.iterrows():
print(row['name'], row['new_name'])
copyfile(row['name'], row['new_name'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment