Skip to content

Instantly share code, notes, and snippets.

@kylehowells
Created February 14, 2020 21:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kylehowells/5ef3803d7acd3775ba611a6c5a6cee83 to your computer and use it in GitHub Desktop.
Save kylehowells/5ef3803d7acd3775ba611a6c5a6cee83 to your computer and use it in GitHub Desktop.
Automatically Corrects Insta360 Studio Snapshot Dates according to the filename.
import os
import re
import datetime
import piexif
filepath = os.path.abspath(".")
# Find files with `screenshot` in the name.
files = [f for f in os.listdir(filepath) if "screenshot" in f and f.endswith(".jpg")]
print(files)
for filename in files:
print(filename)
# Extract datetime
date_rex = r"(20\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)"
date_ranges = re.search(date_rex, filename)
# Date
year = date_ranges.group(1)
month = date_ranges.group(2)
day = date_ranges.group(3)
# Time
hour = date_ranges.group(4)
minute = date_ranges.group(5)
second = date_ranges.group(6)
# date text
dt_text = f"{year}:{month}:{day} {hour}:{month}:{second}"
print(dt_text)
# Image file path
image_filepath = os.path.join(filepath, filename)
print(image_filepath)
exif_dict = piexif.load(image_filepath)
print(exif_dict) # Before
exif_dict["0th"][piexif.ImageIFD.DateTime] = dt_text
exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = dt_text
print(exif_dict) # After
# Save new metadata
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, image_filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment