Skip to content

Instantly share code, notes, and snippets.

@Koasing
Last active December 19, 2016 22:55
Show Gist options
  • Save Koasing/1245f5f2c44e277dcb8409055c5861ed to your computer and use it in GitHub Desktop.
Save Koasing/1245f5f2c44e277dcb8409055c5861ed to your computer and use it in GitHub Desktop.
Arrange Dropbox Camera Uploads
import os
import os.path as path
import re
from PIL import Image
IMAGE_EXTENSION = ['.jpg', '.jpeg', '.png', '.gif']
CAPTURE_SIZE = [(2048, 1536), (1136, 640)]
SCREEN_RATIO = [w/h if w > h else h/w for w, h in CAPTURE_SIZE]
def move_file(src, dst):
# src == dst
if src == dst:
print(src, 'not move')
return
d, fn = path.split(dst)
os.makedirs(d, exist_ok=True)
print (src, '->', dst)
os.rename(src, dst)
return
# file list
for fn in os.listdir():
# test isfile
if not path.isfile(fn):
continue
# test file extension
root, ext = path.splitext(fn)
ext = ext.lower()
if ext not in IMAGE_EXTENSION:
print(fn, 'ext skip')
continue
try:
img = Image.open(fn)
width, height = img.size
img.close() # do not forget to release the image
new_fn = fn
# YYYY-MM-DD HH.mm.ss.ext matching
m = re.fullmatch(r'(20\d{2})-([01]\d)-([0-3]\d) \d{2}.\d{2}.\d{2}.*\.\w{3,4}', fn)
if m:
new_fn = '{YYYY}\\{MM}\\{fn}'.format(YYYY=m.group(1), MM=m.group(2), fn=fn)
# screen ratio matching
if ( ext == '.png' ) and ( ( (width, height) in CAPTURE_SIZE ) or ( (height, width) in CAPTURE_SIZE ) ):
new_fn = 'Caputre\\' + new_fn
else:
ratio = width / height if width > height else height / width
if ( ext == '.png' ) and ( ratio in SCREEN_RATIO ):
new_fn = 'Caputre\\' + new_fn
move_file(fn, new_fn)
except:
# this is not an image file. do not touch it.
print(fn, 'not image skip')
print(e)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment