-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif_extract.py
65 lines (49 loc) · 1.73 KB
/
exif_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import piexif
import json
from urllib import parse
import requests
import uuid
import os
UPLOAD_DIR = "uploads"
def url_extract(url):
filename = parse.urlparse(url).path.split('/')[-1]
file_ext = filename.split('.')[-1]
image_uuid = str(uuid.uuid4())
out_path = os.path.join(os.path.abspath(os.path.curdir), UPLOAD_DIR, image_uuid + "." + file_ext)
response = requests.get(url)
with open(out_path, 'wb') as fd:
for chunk in response.iter_content(4096):
fd.write(chunk)
props = extract(out_path)
return props
def extract(path):
print(path)
exif_dict = piexif.load(path)
props = {}
for ifd in ("0th", "Exif", "GPS", "1st"):
for tag in exif_dict[ifd]:
props[piexif.TAGS[ifd][tag]["name"]] = exif_dict[ifd][tag]
return normalize_exif(filter(props))
def print_exif(props):
print(json.dumps(props, indent=4, separators=(',', ': ')))
def clean_upload_dir():
for filename in os.listdir(UPLOAD_DIR):
os.remove(os.path.join(UPLOAD_DIR, filename))
def filter(props):
filters = ["ExposureTime", "FNumber", "FocalLength", "ISOSpeedRatings"]
filtered_dict = {k: v for k, v in props.items() if k in filters}
return filtered_dict
# return props
def normalize_exif(exif_data):
exposure_time = exif_data["ExposureTime"]
f_stop = exif_data["FNumber"]
result = {}
# TODO eval is EVIL!
result["shutter_speed"] = eval(str(exposure_time[0]) + '/' + str(exposure_time[1]))
result["f_stop"] = eval(str(f_stop[0]) + '/' + str(f_stop[1]))
result["focal_length"] = exif_data["FocalLength"][0]
result["iso"] = exif_data["ISOSpeedRatings"]
return result
if __name__ == '__main__':
# clean_img_repo()
clean_upload_dir()