[Python] How to modify the metadata creation date of a file through Python?

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a lot of photos with wrong dates in metadata. When I upload them to a web service like Amazon Photos for example, they are disorganized due to that.

    So I am doing a Python 3 script in Linux to modify the dates of the photos, but not all dates are changing.

    For example, when examining the date of a photo with exiftool I get this:

    File Modification Date/Time : 2023:11:07 17:49:45+01:00
    File Access Date/Time : 2025:05:07 17:30:20+02:00
    File Inode Change Date/Time : 2025:05:07 17:30:20+02:00
    Date/Time Original : 2023:11:06 19:09:35
    Create Date : 2023:11:06 19:09:35
    Modify Date : 2023:11:06 19:09:35
    GPS Date Stamp : 2023:11:06
    Profile Date Time : 2018:06:24 13:22:32
    Date/Time Original : 2023:11:06 19:09:35.0
    GPS Date/Time : 2023:11:06 19:09:35Z


    Then, I execute my script to set 2025:05:05 as the new date, and after that the metadata are these:

    File Modification Date/Time : 2025:05:05 15:39:00+02:00
    File Access Date/Time : 2025:05:07 17:36:57+02:00
    File Inode Change Date/Time : 2025:05:07 17:36:57+02:00
    Modify Date : 2025:05:05 15:39:00
    Date/Time Original : 2025:05:05 15:39:00
    Create Date : 2025:05:05 15:39:00
    GPS Date Stamp : 2023:11:06
    Profile Date Time : 2018:06:24 13:22:32
    Date/Time Original : 2025:05:05 15:39:00.0
    GPS Date/Time : 2023:11:06 19:09:35Z


    As you can see, the following dates were not changed:

    File Access Date/Time : 2025:05:07 17:30:20+02:00
    File Inode Change Date/Time : 2025:05:07 17:30:20+02:00
    GPS Date Stamp : 2023:11:06
    Profile Date Time : 2018:06:24 13:22:32
    GPS Date/Time : 2023:11:06 19:09:35Z


    When I navigate to the photo trough a file explorer like Dolphin, its Created value is not the one I set (2025:05:05), and I guess that is not a good sign. I do not mind the values of Modified or Accesed, which are OK, because they will change with any action so these are not reliable.

    What do you think about this? Which date should I modify to organize photos in web services? And is it OK to do that?

    This is my code, any suggestion will be appreciated:

    #!/usr/bin/env python3
    import subprocess

    file_path = "IMG_20231107_174945.jpg"
    new_date = "2025:05:05 15:39:00"

    subprocess.run([
    "exiftool",
    "-overwrite_original",
    f"-AllDates={new_date}",
    file_path
    ])

    date_touch = new_date.replace(":", "").replace(" ", "")[:12]
    subprocess.run(["touch", "-t", date_touch, file_path])

    Continue reading...

Compartilhe esta Página