exv.py : Utilisation de piexif (ajouté à "requirements.txt"). Merci à Christophe pour l'idée

This commit is contained in:
Antoine Van Elstraete 2019-05-23 22:43:01 +02:00 committed by Antoine Van Elstraete
parent 00270fdc18
commit 9fa8c6e412
2 changed files with 51 additions and 86 deletions

136
exv.py
View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3
from subprocess import Popen, PIPE
import piexif
from datetime import datetime
import json
from os.path import isfile, isdir
@ -10,26 +9,11 @@ from os import walk
TYPES = [
"JPEG",
"JPG",
"EXV",
"CR2",
"CRW",
"MRW",
"TIFF",
"TIF",
"WEBP",
"DNG",
"NEF",
"FEF",
"ARW",
"RW2",
"SR2",
"SRW",
"ORF",
"PNG",
"PGF",
"RAF",
"PSD",
"JP2"
]
def extractor(input_files, start, end, recursive, extensions):
@ -39,21 +23,21 @@ def extractor(input_files, start, end, recursive, extensions):
raise ValueError("Input files must be a list.")
exif_dict_list = []
files = []
usefull_exif = [
"Image.Model",
"Photo.LensModel",
"Photo.FocalLength",
"Photo.ApertureValue",
"Photo.ExposureTime",
"Photo.ISOSpeedRatings",
"Photo.PixelXDimension",
"Photo.PixelYDimension",
"Image.ImageWidth",
"Image.ImageLength",
"Photo.DateTimeOriginal",
"Photo.DateTimeDigitized",
"Exif.Image.DateTime"
]
usefull_exif = {
"Model": "",
"LensModel": "",
"FocalLength": 0,
"ApertureValue": 0,
"ExposureTime": 0,
"ISOSpeedRatings": 0,
"PixelXDimension": 0,
"PixelYDimension": 0,
"ImageWidth": 0,
"ImageLength": 0,
"Pixels": None,
"Dimension": 0,
"DateTime": None,
}
for item in input_files:
if isdir(item):
for (dirpath, dirnames, filenames) in walk(item):
@ -74,66 +58,46 @@ def extractor(input_files, start, end, recursive, extensions):
input_files = files
for input_file in input_files:
exif_dict = {}
exif_dict = usefull_exif
if not isfile(input_file):
raise ValueError("{} doesn't exist here.".format(input_file))
exif_dict.update({'file': input_file})
cmd = ["exiv2", "-Ptk", input_file]
with Popen(cmd, stdout=PIPE) as extracted_data:
extracted_data = extracted_data.stdout.read().decode()
for line in extracted_data.splitlines():
for exif in usefull_exif:
if exif in line.split()[0]:
exif_dict.update(
{
line.split()[0].split(".")[-1]:
" ".join(line.split()[1:])
}
)
for exif in usefull_exif:
if exif.split(".")[-1] not in exif_dict:
exif_dict.update({exif.split(".")[-1]: None})
if exif_dict['FocalLength']:
exif_dict['FocalLength'] = float(
exif_dict['FocalLength'].replace(" mm", ""))
if exif_dict['ApertureValue']:
exif_dict['ApertureValue'] = float(
exif_dict['ApertureValue'].replace("F", ""))
if exif_dict['PixelXDimension'] and exif_dict['PixelYDimension']:
exif_dict['PixelXDimension'] = int(exif_dict['PixelXDimension'])
exif_dict['PixelYDimension'] = int(exif_dict['PixelYDimension'])
pixels = exif_dict['PixelXDimension'] * \
exif_dict['PixelYDimension']
elif exif_dict['ImageLength'] and exif_dict['ImageWidth']:
exif_dict['PixelXDimension'] = int(exif_dict['ImageLength'])
exif_dict['PixelYDimension'] = int(exif_dict['ImageWidth'])
pixels = exif_dict['PixelXDimension'] * \
exif_dict['PixelYDimension']
else:
pixels = None
if exif_dict['DateTimeOriginal']:
exif_tags = piexif.load(input_file)
if 0x0110 in exif_tags['0th']:
exif_dict.update({'Model':
exif_tags['0th'][0x0110].decode()})
if 0xa434 in exif_tags['Exif']:
exif_dict.update({'LensModel':
exif_tags['Exif'][0xa434].decode().split("\x00")[0]})
if 0x920a in exif_tags['Exif']:
exif_dict.update({'FocalLength':
float(exif_tags['Exif'][0x920a][0] / exif_tags['Exif'][0x920a][1])})
if 0x829d in exif_tags['Exif']:
exif_dict.update({'ApertureValue':
float(exif_tags['Exif'][0x829d][0]/exif_tags['Exif'][0x829d][1])})
if 0x829a in exif_tags['Exif']:
exif_dict.update({'ExposureTime':
float(exif_tags['Exif'][0x829a][0]/exif_tags['Exif'][0x829a][1])})
if 0x8827 in exif_tags['Exif']:
exif_dict.update({'ISOSpeedRatings':
int(exif_tags['Exif'][0x8827])})
if 0xa002 in exif_tags['Exif'] and 0xa003 in exif_tags['Exif']:
exif_dict.update({'Pixels':
int(exif_tags['Exif'][0xa002]) * int(exif_tags['Exif'][0xa003])})
elif 0x0100 in exif_tags['0th'] and 0x0101 in exif_tags['0th']:
exif_dict.update({'Pixels':
int(exif_tags['0th'][0x0100]) * int(exif_tags['0th'][0x0101])})
if 0x9003 in exif_tags['Exif']:
exif_dict['DateTime'] = datetime.strptime(
exif_dict['DateTimeOriginal'], "%Y:%m:%d %H:%M:%S")
elif exif_dict['DateTimeDigitized']:
exif_tags['Exif'][0x9003].decode(), "%Y:%m:%d %H:%M:%S")
elif 0x9004 in exif_tags['Exif']:
exif_dict['DateTime'] = datetime.strptime(
exif_dict['DateTimeDigitized'], "%Y:%m:%d %H:%M:%S")
elif exif_dict['DateTime']:
exif_tags['Exif'][0x9004].decode(), "%Y:%m:%d %H:%M:%S")
elif 0x0132 in exif_tags['0th']:
exif_dict['DateTime'] = datetime.strptime(
exif_dict['DateTime'], "%Y:%m:%d %H:%M:%S")
else:
exif_dict['DateTime'] = None
if exif_dict['ExposureTime']:
try:
exif_dict['ExposureTime'] = float(
exif_dict['ExposureTime'].split(" ")[0])
except ValueError:
exposure = exif_dict['ExposureTime'].split(" ")[0]
exif_dict['ExposureTime'] = float(
1/(float(exposure.split("/")[1])))
if pixels:
exif_dict.update({'Dimension': round((pixels/10**6), 1)})
else:
exif_dict.update({'Dimension': None})
exif_tags['0th'][0x0132].decode(), "%Y:%m:%d %H:%M:%S")
if exif_dict['Pixels']:
exif_dict.update({'Dimension': round((exif_dict['Pixels']/10**6), 1)})
exif_dict_list.append(exif_dict)
cameras, lenses, focals, apertures, exposures = {}, {}, {}, {}, {}
isos, dimensions, cameras_lenses, dates = {}, {}, {}, {}

View File

@ -2,3 +2,4 @@ Flask
numpy
matplotlib
babel
piexif