[Python] Extracting EXIF DateTaken as Str when I need it as Date and Time or an Int

Discussão em 'Python' iniciado por Stack, Dezembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    Trying to rename family pictures, I want all the photos in this folder to be renamed as

    mmdd__00X

    So the 20th image on March 23rd should have a file name of

    0323__020

    A lot of my code comes from other threads on this, I used the code from Python File Creation Date & Rename - Request for Critique for most of it, but found I needed Date Taken (exif data) as opposed to Date Created. Problem is, the EXIF module in Pillow takes the Date Taken tag as a String, but I need it to be an int or a datetime for me to be able to modify. Or can I parse the string to fit my naming format?

    import os
    import datetime
    from PIL import Image

    target = input('Enter full directory path: ')
    os.chdir(target)
    allfiles = os.listdir(target)
    counter = 0

    def get_date_taken(path):
    return Image.open(path)._getexif()[36867]

    for filename in allfiles:
    t = get_date_taken(filename)
    v = datetime.datetime.fromtimestamp(t)
    x = v.strftime('%m%d')
    y = '{:02d}'.format(counter)
    try:
    os.rename(filename, x+"__"+y+".jpg")
    except FileExistsError:
    while True:
    try:
    counter += 1
    y = '{:02d}'.format(counter)
    os.rename(filename, x+"__"+y+".jpg")
    except FileExistsError:
    continue
    counter = 0
    break


    First real program, any help would be appreciated.

    Continue reading...

Compartilhe esta Página