Python Forum

Full Version: |SOLVED] Glob JPGs, read EXIF, update file timestamp?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

My digital camera has no GPS, so I sometimes take pictures with my smartphone when I need to get geotagged pics.

Problem is, I noticed the time on the camera on my last trip was about 1h ahead than the time on the smartphone which is correct since set from the GSM network.

So, to make it easier to create a slideshow, I need to write a Python script to update the file time on all the pics that were taken with the camera, and substract 1h so they're about the right time.

Pseudo-code:
Glog *.jpg including subdirs
	Read EXIF
	If no GPX //Camera, not smartphone
		Substract one hour
		Update timestamp

Would someone have some code to share?

Thank you.

---
Edit: Almost there:

#pip install pathlib
from pathlib import Path
#pip install exifread
import exifread, sys

for mypath in Path('c:/my/path/').rglob('*.jpg'):
	p = Path(mypath)
	p = p.resolve()
	with open(p, 'rb') as f:
		tags = exifread.process_file(f)
		if "GPS GPSLatitude" in tags:
			print("GPS: ", p)
		else:
			print("NO GPS")
There are several tools to work/edit with EXIF in python, incl. you can do it with Pillow, piexif

However, if this is just one-time exercise and not a script that you need to implement edit EXIF tags feature better have a look at ExifTool by Phil Harvey
Have a look at TimeShift Feature


EDIT: I see you have found exifread, but it just for reading EXIF tags.

EDIT2: I found this very useful when working on project back in the time: https://exiftool.org/TagNames/EXIF.html + the exif standard
Thanks for the infos. I'll check them out.

Done:
#pip install pathlib
from pathlib import Path
#pip install exifread
import exifread, sys, os, time, re
from datetime import datetime, timedelta

for mypath in Path('c:/my/path/').rglob('*.jpg'):
	p = Path(mypath)
	p = p.resolve()
	# Open image file for reading (binary mode)
	with open(p, 'rb') as f:
		tags = exifread.process_file(f)
		if "GPS GPSLatitude" in tags:
			print("GPS: ", p)
		else:
			print("NO GPS: ",p)
			
			#Get ctime in Epoch (seconds)
			orig_mtime = int(os.path.getmtime(p))
			#need access time, even though not needed here; in Windows at least, atime doesn't change even when reading a file
			orig_atime = int(os.path.getatime(p))
			#Substract one hour
			new_mtime = int(orig_mtime - 3600)
			#rewrite timestamp
			os.utime(p, (orig_atime, new_mtime))
Well, I was under impression you want to edit EXIF, You mention the file time, but I overlooked that.

By the way, did you check the DateTimeOriginal EXIF tag value? And it's really interesting that your phone camera has a separate clock from your phone.
Thanks for the info, I didn't notice the DateTimeOriginal and DateTimeDigitized EXIF fields.

I'm not aware of the smartphone's camera having a different clock than the phone itself. I just meant that the compact digital camera that I use to take most pictures has no GPS, which I why I sometimes use the smartphone when I need geotagged pictures. I use the compact camera because it's easy to use with just one hand while on bike trips, but it also means the time is set manually, and was off by about an hour to real time.
:-), OK, a funny case of misscommunication/misunderstanding.