Oct-21-2022, 12:24 AM
Perhaps this will help you to see what's going on step by step.
Copy and paste each line one by one into your Python IDE, to see what it is doing.
Of course, the if ... else: has to be pasted in one go.
Then, when you understand what's happening, put lines 25 to 35 in a function and return newname.
I replaced the gaps in the new file names with underscore, because, if I had hundreds or thousands of photos to work on, I would do this in bash, and bash doesn't like the spaces!
Probably a good idea to standardize all the names to .jpg or whatever first.
Copy and paste each line one by one into your Python IDE, to see what it is doing.
Of course, the if ... else: has to be pasted in one go.
Then, when you understand what's happening, put lines 25 to 35 in a function and return newname.
I replaced the gaps in the new file names with underscore, because, if I had hundreds or thousands of photos to work on, I would do this in bash, and bash doesn't like the spaces!
Probably a good idea to standardize all the names to .jpg or whatever first.
from PIL import Image from PIL.ExifTags import TAGS from pathlib import Path from datetime import datetime path2img = '/home/pedro/Downloads/' file1 = path2img + '1666308164309.jpg' # from my phone file2 = path2img + '1646613137056.jpg' # from my phone file3 = path2img + 'Athena.jpg' # no exif data im1 = Image.open(file1) exif1 = im1._getexif() # seems like the key you want is 36867 for key in exif1.keys(): print(key, exif[key]) newname = exif2[36867].replace(' ', '_') print(newname) # try again with file2 im2 = Image.open(file2) exif2 = im2._getexif() newname = exif2[36867].replace(' ', '_') print(newname) # try again with file3, no exif data im3 = Image.open(file3) # returns None when no exif data exif3 = im3._getexif() # if no exif data give the time now as name if exif3 == None: print('Problem: no exif data') now = datetime.now() newname = now.strftime("%A %d-%b-%Y %H:%M:%S").replace(' ', '_') else: newname = exif3[36867].replace(' ', '_') print(newname)