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
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 543 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Recommended way to read/create PDF file? Winfried 3 2,783 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,308 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,048 Jul-06-2023, 01:52 AM
Last Post: Tupa
  Timestamp of file changes if a share mapped to a server…. tester_V 34 3,631 Jul-04-2023, 05:19 AM
Last Post: tester_V
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,010 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,961 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Read csv file with inconsistent delimiter gracenz 2 1,140 Mar-27-2023, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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