How to Change image captured date in python?

前端 未结 4 1379
生来不讨喜
生来不讨喜 2021-02-13 19:52

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:20

    This is quite easy to do using the piexif library:

    from datetime import datetime
    import piexif
    
    filename = 'image.jpg'
    exif_dict = piexif.load(filename)
    new_date = datetime(2018, 1, 1, 0, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
    exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
    exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
    exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
    exif_bytes = piexif.dump(exif_dict)
    piexif.insert(exif_bytes, filename)
    

    This script will insert the new date 2018:01:01 00:00:00 into the DateTime, DateTimeOriginal and DateTimeDigitized EXIF fields for image.jpg.

提交回复
热议问题