How to Change image captured date in python?

前端 未结 4 1412
清酒与你
清酒与你 2021-02-13 19:43

I have over 500 images (png /jpg) having wrong captured date (Date taken) because of wrong camera date settings. I moved photos to mobile and mobile gallery sorts photos on the

4条回答
  •  抹茶落季
    2021-02-13 20:21

    I might be late to this party, but i wrote a python script for bulk changing taken time field for whatsapp photos based on filename format Eg: IMG-20160117-WA0001.jpg. Also this does not overwrite the existing properties. https://github.com/dsouzawilbur/Scripts/blob/master/Change_Photo_Taken_Time.py

    import os
    import re
    import piexif
    
    def absoluteFilePaths(directory):
        for dirpath,_,filenames in os.walk(directory):
            for f in filenames:
                fullPath = os.path.abspath(os.path.join(dirpath, f))
                if re.match(r"^IMG-\d\d\d\d\d\d\d\d-WA\d\d\d\d.*", f) and not re.match(r"^IMG-\d\d\d\d\d\d\d\d-WA\d\d\d\d-ANIMATION.gif", f):
                    print(f+" Matched")
                    match = re.search("^IMG-(\d\d\d\d)(\d\d)(\d\d)-WA\d\d\d\d.*", f)
                    year = match.group(1)
                    month= match.group(2)
                    day = match.group(3)
                    exif_dict = piexif.load(fullPath)
                    #Update DateTimeOriginal
                    exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                    #Update DateTimeDigitized               
                    exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                    #Update DateTime
                    exif_dict['0th'][piexif.ImageIFD.DateTime] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                    exif_bytes = piexif.dump(exif_dict)
                    piexif.insert(exif_bytes, fullPath)
                    print("############################")
    
    
    absoluteFilePaths("__DIRECTORY_WITH_PHOTOS__")
    

提交回复
热议问题