from transpose_dict import transpose_dict import os import io import time from datetime import datetime import json import subprocess # import metadata file objects from PIL import Image from PIL.ExifTags import TAGS import exifread import pyheif EXIFTOOL="exiftool" def get_pict_meta(exifdata): for tag_id in exifdata: # get the tag name, instead of human unreadable tag id tag = TAGS.get(tag_id, tag_id) if tag == "DateTime": data = exifdata.get(tag_id) # decode bytes if isinstance(data, bytes): data = data.decode() return(data) def get_heic_meta(heif_file): for metadata in heif_file.metadata: file_stream = io.BytesIO(metadata['data'][6:]) tags = exifread.process_file(file_stream, details=False) for k,v in tags.items(): #print(f"TAG:{k}->{v}") if k == "Image DateTime": return(str(v)) # this is the main extensions = (".jpg", ".JPG", ".heic", ".HEIC", ".mov", ".MOV") #extensions = (".jpg", ".JPG") fl = {} for file in os.listdir("./test/work"): if file.endswith(extensions): # if file.endswith(('.jpg','.JPG')): # image = Image.open(os.path.join("./test/work", file)) # exifdata = image.getexif() # dt = (get_pict_meta(exifdata)).replace(' ','|') # #print(f"{file}-->{dt}<") # # elif file.endswith(('.heic','.HEIC')): # heif_file = pyheif.read_heif(os.path.join("./test/work", file)) # # dt = (get_heic_meta(heif_file)).replace(' ','|') # #print(f"{file}++>{dt}<") # elif file.endswith(('.mov','.MOV')): # Open image file for reading (must be in binary mode) # f = open(os.path.join("./test/work", file) , 'rb') dt="2023:08:14|12:00:00" # -createdate for .mov provides Zulu time while for other format timezone time # for .mov need to use -creationdate TAG if file.endswith(('.mov','.MOV')): tags = '-creationdate' else: tags = '-createdate' process = subprocess.Popen([EXIFTOOL,os.path.join("./test/work", file), tags ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for out in process.stdout: print(f"{out.decode()}") #if ((out.decode().split(':'))[0]).strip() == "Creation Date": line = ((out.decode().split(':'))[1:]) print(f"{line} -- {os.path.join('./test/work', file)}") dt = f"{line[0]}:{line[1]}:{line[2].split(' ')[0]}|{line[2].split(' ')[1]}:{line[3]}:{(line[4].split('+'))[0]}".strip() print(f"{dt}") fl.update({file : { int(time.mktime(time.strptime(dt, '%Y:%m:%d|%H:%M:%S'))) : { 'date': dt.split('|')[0], 'time': dt.split('|')[1] } } }) # print unsorted dictionary #print(f"{json.dumps(fl, indent=4)}") # transpose disctionary and sort it by date of creation of the file flt = dict(sorted(transpose_dict(fl,1).items())) # print print(f"{json.dumps(flt, indent=4)}")