Skip to content

Commit

Permalink
WIP extract Exif using Pillow, refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 18, 2020
1 parent 502efb9 commit c8a1eaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
61 changes: 35 additions & 26 deletions photos_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import pathlib
from .utils import calculate_hash

from PIL import Image
import PIL.ExifTags


@click.group()
@click.version_option()
Expand Down Expand Up @@ -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,
# )
# )
# )
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"],
)

1 comment on commit c8a1eaa

@simonw
Copy link
Contributor Author

@simonw simonw commented on c8a1eaa Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this refs #3

Please sign in to comment.