Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change image captured date in python?

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 basis of 'Date Taken'. I want all photos to be displayed in order.

So how can I change captured date (Date taken) using python script?

like image 969
Gaurav Vichare Avatar asked Oct 09 '15 06:10

Gaurav Vichare


2 Answers

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.

like image 149
Joachim Avatar answered Oct 17 '22 07:10

Joachim


No real need to write Python, you can do it in one line in the Terminal using jhead. For example, adjust all EXIF times forward by 1 hour

jhead -ta+1:00 *.jpg

Make a COPY of your files and test it out on that first!

Download from here.

like image 43
Mark Setchell Avatar answered Oct 17 '22 07:10

Mark Setchell