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

View File

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