Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Created March 29, 2023 11:36
Show Gist options
  • Save bennuttall/aa6f48c9a31a788d4e5121ea92a0cd80 to your computer and use it in GitHub Desktop.
Save bennuttall/aa6f48c9a31a788d4e5121ea92a0cd80 to your computer and use it in GitHub Desktop.
Scripts for organising photos
from pathlib import Path
from structlog import get_logger
from PIL import Image
logger = get_logger()
dir = Path.cwd()
def get_date_taken(path):
return Image.open(path)._getexif()[36867]
for f in dir.glob('IMG_*.JPG'):
stem = f.stem
dt = get_date_taken(f)
y, m = dt[:4], dt[5:7]
new_dir = Path(y, m)
new_dir.mkdir(parents=True, exist_ok=True)
new_file = new_dir / f.name
if new_file.exists():
logger.warn("New file already exists", old=str(f), new=str(new_file))
continue
logger.debug("Moving file", old=str(f), new=str(new_file))
f.rename(new_file)
from pathlib import Path
from structlog import get_logger
logger = get_logger()
dir = Path('Camera')
for f in dir.iterdir():
stem = f.stem
if not stem.startswith('PXL_'):
logger.warn("Not a PXL image", filename=str(f))
continue
y, m, d = stem[4:8], stem[8:10], stem[10:12]
new_dir = Path(y, m)
new_dir.mkdir(parents=True, exist_ok=True)
new_file = new_dir / f.name
if new_file.exists():
logger.warn("New file already exists", old=str(f), new=str(new_file))
continue
logger.debug("Moving file", old=str(f), new=str(new_file))
f.rename(new_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment