Created
July 22, 2019 00:22
-
-
Save kennethreitz/df7c4365c7502bcb51c6b687cf600848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import os | |
import json | |
from tempfile import mkstemp | |
# import exifread | |
import PIL | |
import PIL.Image | |
import PIL.ExifTags | |
import exifread | |
import piexif | |
from requests.structures import CaseInsensitiveDict | |
import clint | |
import clint.eng | |
from google.cloud import vision | |
from google.cloud.vision import types | |
def resize(img_path): | |
size = 1024, 1536 | |
t = mkstemp()[1] | |
try: | |
im = PIL.Image.open(img_path) | |
im.thumbnail(size, PIL.Image.ANTIALIAS) | |
im.save(t, "JPEG") | |
return t | |
except IOError: | |
return img_path | |
def get_exif(img_path): | |
with open(img_path, 'rb') as f: | |
d = exifread.process_file(f) | |
data = {} | |
try: | |
for k, v in d.items(): | |
k = k.replace('EXIF ', '') | |
if len(v.values) == 1: | |
data[k] = str(v.values[0]).strip() | |
elif isinstance(v.values, str): | |
data[k] = str(v.values).strip() | |
else: | |
data[k] = tuple([str(v).strip() for v in v.values]) | |
return CaseInsensitiveDict(data) | |
except Exception: | |
pass | |
return data | |
def get_vision(img_path): | |
# Instantiates a client | |
client = vision.ImageAnnotatorClient() | |
with io.open(img_path, 'rb') as img: | |
content = img.read() | |
image = types.Image(content=content) | |
del content | |
# Performs label detection on the image file | |
response = client.label_detection(image=image) | |
labels = [label.description for label in response.label_annotations] | |
del response | |
return labels | |
def get_title(img_path): | |
labels = get_vision(resize(img_path)) | |
exif = get_exif(img_path) | |
camera = exif.get('Image Model', '') | |
lens = exif.get('LensModel', '') | |
title = f"{clint.eng.join(labels)}" | |
if lens: | |
return f"{lens} & {camera}: {title}." | |
elif camera: | |
return f"{camera}: {title}." | |
else: | |
return f"{title}." | |
def write_exif_title(img_path, title): | |
try: | |
exif = piexif.load(img_path) | |
exif['0th'][piexif.ImageIFD.Artist] = 'Kenneth Reitz' | |
exif['0th'][piexif.ImageIFD.Copyright] = '2019, Kenneth Reitz.' | |
exif['0th'][piexif.ImageIFD.ImageDescription] = title | |
piexif.insert(piexif.dump(exif), img_path) | |
except Exception: | |
pass | |
def iter_photos(path='./photos'): | |
for dirpath, dirnames, filenames in os.walk(path): | |
for filename in filenames: | |
yield os.path.join(dirpath, filename) | |
for photo in clint.textui.progress.bar([p for p in iter_photos()]): | |
title = get_title(photo) | |
print(title) | |
write_exif_title(photo, title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
was debugging a memory leak in scrapy, hence the 'del's