Python Forum
Rename multiple photos with DateTimeOriginal timestamp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename multiple photos with DateTimeOriginal timestamp
#3
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.

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)
Reply


Messages In This Thread
RE: Rename multiple photos with DateTimeOriginal timestamp - by Pedroski55 - Oct-21-2022, 12:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 1,018 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  error in timestamp Led_Zeppelin 3 4,922 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,437 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Rename part of filename in multiple files atomxkai 7 10,672 Feb-18-2022, 10:03 PM
Last Post: atomxkai
Photo Moving 2 photos from each subfolder to another folder Blacklonewolf 1 2,933 Oct-28-2021, 04:07 PM
Last Post: DeaD_EyE
  Rename Multiple files in directory to remove special characters nyawadasi 9 10,368 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Copy mp3 file multiple times and rename Mar10 4 5,012 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Problems with getting image's DateTimeOriginal Pythenx 3 4,224 May-02-2019, 02:21 PM
Last Post: DeaD_EyE
  Timestamp is undefined ErnestTBass 7 9,973 Feb-16-2019, 08:27 PM
Last Post: snippsat
  Download multiple images and rename them andie31 4 6,420 Sep-13-2018, 10:26 AM
Last Post: andie31

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020