title: How do I add date-time properties to my JPG files using Python, for correct indexing in Google Photos?
body: <p>I'm working with camera kit that produces images with date-times saved as their filenames (e.g. "2019-10-24-10-06-56.jpg"), rather than within their metadata. I'm trying to write a Python script that enables me to extract date-times from their filenames, and add to the date-time metadata properties of each file. This is so I can upload to e.g. Google Photos for correct chronological indexing.</p>
  
  <p>From what I can gather, Google Photos uses the 'Created' date-time property, so I believe this is what I need to change for each JPG file. What's the best way of doing this? I've tried a few things including changing EXIF data (successfully, using piexif) but this doesn't seem to change the image properties as shown in Windows, and it still does not correctly index in Google Photos.</p>
  
  <p>Keen to do this in Python if possible (I'm using Python 2.7) - already written the code to extract date-time from filename. </p>
  
  <p>The following code appears to successfully change the EXIF data:</p>
  
  <pre><code>from datetime import datetime
  import piexif
  import os
  
  jpgFolder = r"C:\Users\srgan\Desktop\Naturewatch Camera Photos\NaturewatchCameraPhotos_24Oct19"
  
  ## count number of photos found
  listOfFiles = os.listdir(jpgFolder)
  fileCount = len(listOfFiles)
  
  ## create datetimeString from JPG filename
  for jpg in os.listdir(jpgFolder):
      filepath = jpgFolder + "\\" + jpg
      year = jpg[0:4]
      month = jpg[5:7]
      day = jpg[8:10]
      hour = jpg[11:13]
      mins = jpg[14:16]
      datetimeStringNew = day + "/" + month + "/" + year + " " + hour + ":" + mins
  
  ## change exif datetimestamp for "Date Taken"
      exif_dict = piexif.load(filepath)
      exif_dict['Exif'] = { piexif.ExifIFD.DateTimeOriginal: datetime(2019, 10, 25, 1, 1).strftime("%d:%m:%Y %H:%M") }
      exif_bytes = piexif.dump(exif_dict)
      piexif.insert(exif_bytes, filepath)
  </code></pre>
  
  <p>correctly returning the following:</p>
  
  <pre><code>{'Exif': {36867: '25:10:2019 01:01'}, '0th': {34665: 26}, 'Interop': {}, '1st': {}, 'thumbnail': None, 'GPS': {}}
  </code></pre>
  
  <p>but this is not reflected in the <a href="https://i.stack.imgur.com/xSk5L.png" rel="nofollow noreferrer">image properties in Windows (1)</a>/<a href="https://i.stack.imgur.com/jk4rn.png" rel="nofollow noreferrer">image properties (2)</a>/dated correctly when uploaded to Google Photos.</p>
  
  <p>Thanks!</p>
  
  <p><em>The Jolly Geographer</em>
  (Not an experienced coder so apologies for any silly questions)</p>