Python Forum
|SOLVED] Glob JPGs, read EXIF, update file timestamp?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
|SOLVED] Glob JPGs, read EXIF, update file timestamp?
#1
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")
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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))
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
:-), OK, a funny case of misscommunication/misunderstanding.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error When Trying To Read CSV File kaega2 3 151 Dec-03-2021, 09:10 PM
Last Post: kaega2
  Updating a config file [solved] ebolisa 8 439 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  [SOLVED] Read text file from some point till EOF? Winfried 1 283 Oct-10-2021, 10:29 PM
Last Post: Winfried
  [SOLVED] Input parameter: Single file or glob? Winfried 0 332 Sep-10-2021, 11:54 AM
Last Post: Winfried
  q re glob.iglob iterator and close jimr 2 467 Aug-23-2021, 10:14 PM
Last Post: perfringo
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 714 Aug-13-2021, 08:21 PM
Last Post: Winfried
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 517 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Read csv file through PyCharm kimx0961 3 914 Aug-01-2021, 07:05 PM
Last Post: perfringo
  [Solved] Plotting data from txt file Laplace12 1 553 Jul-06-2021, 07:14 AM
Last Post: Laplace12
  Why it does not print(file.read()) Rejaul84 1 661 Jul-01-2021, 10:37 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020