Hi,
I'm still very much in the learning phase of creating python programs.
At present, I'm trying to sort Google Photos onto PC but going forwards I'm hoping script can be used to sort a photo dump from mobile phone.
Ideally, the script would unzip photo downloads, however, I'm happy doing this manually until I get first part of script running.
The unzipped files have contained following extensions '.jpg', '.jpeg', '.JPG'.
One script that seen suggests passing through lower() - I'd be happy if I can get script running for '.JPG' for time being.
The files I'm dealing with just now seem to be of format DSC_*.JPG. I'd like to iterate the files in the Downloads directory and rename in 'DateTimeOriginal'.jpg format.
I've read a few examples of programs and created following script:
https://linuxtut.com/en/4b042473f8442da9989a/
Regards,
Stephen
I'm still very much in the learning phase of creating python programs.
At present, I'm trying to sort Google Photos onto PC but going forwards I'm hoping script can be used to sort a photo dump from mobile phone.
Ideally, the script would unzip photo downloads, however, I'm happy doing this manually until I get first part of script running.
The unzipped files have contained following extensions '.jpg', '.jpeg', '.JPG'.
One script that seen suggests passing through lower() - I'd be happy if I can get script running for '.JPG' for time being.
The files I'm dealing with just now seem to be of format DSC_*.JPG. I'd like to iterate the files in the Downloads directory and rename in 'DateTimeOriginal'.jpg format.
I've read a few examples of programs and created following script:
from pathlib import Path
from PIL import Image
from PIL.ExifTags import TAGS
import os
#define function to retrieve exif table data
def get_exif(file_data):
ret = {}
i = Image.open(file_data)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
#iterate over Downloads directory
for filename in Path("/home/stjude1982/Downloads").glob("*.jpg"):
exif = get_exif(filename)
#check if file contains exif data
if "DateTimeOriginal" in exif:
source = filename
destination = Path(filename).with_name(datetime.strptime(exif["DateTimeOriginal"], "%Y:%m:%d %H:%M:%S"))
os.rename(source, destination)
print(destination)
else:
print(f"[WARNING] {filename}: no EXIF header")This program is generating the following error, however, given my lack of experience/knowledge I'm pretty sure there's more wrong with the program than error suggested. Can anyone help, please? I seem to be going round in circles.Error:%Run RenameGooglePhotos.py
Traceback (most recent call last):
File "/home/stjude1982/Python/RenameGooglePhotos.py", line 21, in <module>
destination = Path(filename).with_name(datetime.strptime(exif["DateTimeOriginal"], "%Y:%m:%d %H:%M:%S"))
NameError: name 'datetime' is not definedFYI - I started off reading and copying the following and edited to the above:https://linuxtut.com/en/4b042473f8442da9989a/
Regards,
Stephen
Larz60+ write Oct-20-2022, 11:23 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags rather than attachment on future posts. Thank you
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags rather than attachment on future posts. Thank you
Attached Files
