diff --git a/photos_to_sqlite/cli.py b/photos_to_sqlite/cli.py index 9aff5bd..d685955 100644 --- a/photos_to_sqlite/cli.py +++ b/photos_to_sqlite/cli.py @@ -5,6 +5,9 @@ import pathlib from .utils import calculate_hash +from PIL import Image +import PIL.ExifTags + @click.group() @click.version_option() @@ -70,36 +73,42 @@ def upload(db_path, directories, auth): for directory in directories: path = pathlib.Path(directory) images = ( - p.resolve() + p for p in path.glob("**/*") if p.suffix in [".jpg", ".jpeg", ".png", ".gif", ".heic"] ) - for filepath in images: + for imagepath in images: + filepath = imagepath.resolve() sha256 = calculate_hash(filepath) + size = filepath.stat().st_size ext = filepath.suffix.lstrip(".") - uploads.upsert({"sha256": sha256, "filepath": str(filepath), "ext": ext}) + uploads.upsert({"sha256": sha256, "filepath": str(filepath), "ext": ext, "size": size}) print(filepath) keyname = "{}.{}".format(sha256, ext) - client.upload_file( - str(filepath), - "dogsheep-photos-simon", - keyname, - ExtraArgs={ - "ContentType": { - "jpg": "image/jpeg", - "jpeg": "image/jpeg", - "png": "image/png", - "gif": "image/gif", - "heic": "image/heic", - }[ext] - }, - ) - print( - " ... uploaded: {}".format( - client.generate_presigned_url( - "get_object", - Params={"Bucket": "dogsheep-photos-simon", "Key": keyname,}, - ExpiresIn=600, - ) - ) - ) + + image = Image.open(filepath) + exif = {PIL.ExifTags.TAGS.get(k, str(k)): v for k, v in image.getexif().items()} + uploads.update(sha256, exif, alter=True) + # client.upload_file( + # str(filepath), + # "dogsheep-photos-simon", + # keyname, + # ExtraArgs={ + # "ContentType": { + # "jpg": "image/jpeg", + # "jpeg": "image/jpeg", + # "png": "image/png", + # "gif": "image/gif", + # "heic": "image/heic", + # }[ext] + # }, + # ) + # print( + # " ... uploaded: {}".format( + # client.generate_presigned_url( + # "get_object", + # Params={"Bucket": "dogsheep-photos-simon", "Key": keyname,}, + # ExpiresIn=600, + # ) + # ) + # ) diff --git a/setup.py b/setup.py index 98b70a3..65afaf4 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ def get_long_description(): [console_scripts] photos-to-sqlite=photos_to_sqlite.cli:cli """, - install_requires=["sqlite-utils>=2.7", "boto3>=1.12.41"], + install_requires=["sqlite-utils>=2.7", "boto3>=1.12.41", "Pillow"], extras_require={"test": ["pytest"]}, tests_require=["photos-to-sqlite[test]"], )