How to Change image captured date in python?

前端 未结 4 1408
清酒与你
清酒与你 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:19

    PNG does not support EXIF so I made this to fix the creation/modification time instead, based on Wilbur Dsouza's answer:

    import datetime
    import os
    import re
    import sys
    import time
    
    import piexif
    
    
    def fix(directory):
        print(directory)
        for dirpath, _, filenames in os.walk(directory):
            for f in filenames:
                fullPath = os.path.abspath(os.path.join(dirpath, f))
                # Format: Screenshot_20170204-200801.png
                if re.match(r"^Screenshot_\d\d\d\d\d\d\d\d-\d\d\d\d\d\d.*", f):
                    match = re.search("^Screenshot_(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d).*", f)
                    year = int(match.group(1))
                    month = int(match.group(2))
                    day = int(match.group(3))
                    hour = int(match.group(4))
                    minute = int(match.group(5))
                    second = int(match.group(6))
    
                    date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
                    modTime = time.mktime(date.timetuple())
    
                    print(f, date)
    
                    os.utime(fullPath, (modTime, modTime))
    
    
    if __name__ == "__main__":
        fix(sys.argv[1]) 
    

提交回复
热议问题