Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Last active August 31, 2017 15:07
Show Gist options
  • Save jcefoli/4342a11fade9883bbea179cde5aeb5b8 to your computer and use it in GitHub Desktop.
Save jcefoli/4342a11fade9883bbea179cde5aeb5b8 to your computer and use it in GitHub Desktop.
Loop through images in batches of X and increase the EXIF timestamp by one minute per batch
<#
This script will loop through all numbered images in a folder and update the EXIF metadata timestamp by 1 minute for every X photos (see $BatchSize)
Setup:
-Assumes image filenames are numbered and sequential (34.jpg, 35.jpg, 36.jpg)
-Requires exiftool and assumes it's in your system Path or in the directory you execute this scrpt from.
Download it here: https://www.sno.phy.queensu.ca/~phil/exiftool/
Why:
-When I uploaded my wedding photos to Google Photos, I noticed they were out of order.
-My wedding photographer sent us about 1400 pictures. The first 400 had correct metadata.
-Upon further investigation, I noticed that starting at 8PM the remaining 1000 were missing metadata.
-Since my event ended at 10PM, I estimated that every 4 images should get the same timestamp, then increase by 1 minute
-Here's the script I used to automate it
#>
#Set These Variables
$ImagePath = "C:\pix" #Path to images
$imageStartDateTime = Get-Date -Month 05 -Day 28 -Year 2017 -Hour 20 -Minute 00 -Second 00 -Millisecond 00 #Starting DateTime object to increment from
[Int]$BatchSize = 4
#Powershell does not sort numbered filenames the same way as Windows Explorer (ie. 1000.jpg would be before 2.jpg) so the regex below fixes that
#https://stackoverflow.com/questions/5427506/how-to-sort-by-file-name-the-same-way-windows-explorer-does
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
#Get the first picture filename and set an integer variable
Get-ChildItem -file -Path $ImagePath | Sort-Object $ToNatural | Select-Object -First 1 |
Foreach-Object {
[int]$FirstFileNumber = $_.BaseName
}
#Get the last picture filename and set an integer variable
Get-ChildItem -File -Path $ImagePath | Sort-Object $ToNatural | Select-Object -Last 1 |
Foreach-Object {
[int]$LastFileNumber = $_.BaseName
}
#Loop in batches: https://stackoverflow.com/questions/6000030/powershell-looping-through-objects-in-batches-of-3
$picRange = $FirstFileNumber..$LastFileNumber
$batchCounter = 1
for($i = 0; $i -lt $picRange.Length; $i += $BatchSize) {
# end index
$j = $i + ($BatchSize - 1)
if ($j -ge $picRange.Length) {
$j = $picRange.Length - 1
}
# create tmpObj which contains items 1-4, then items 5-8, then 9 etc
$myTmpObj = $picRange[$i..$j]
# show batches
"Batch $batchCounter"
foreach($item in $myTmpObj) {
#Set File Path
$imgpath = "$ImagePath\$item.jpg"
#Create Timestamp String in formate needed by exiftool
$exifstring = $imageStartDateTime.ToString("yyyy:MM:dd HH:mm:ss.FFK")
#Actually rename the file
Start-Process -FilePath "exiftool" -ArgumentList "-xmp:dateTimeOriginal=`"$exifstring`" $imgpath" -Wait -WorkingDirectory $pwd -NoNewWindow
}
#Increment Batch Counter by 1
$batchCounter++
#Increment datetime object by one minute for each batch of 4 images
$imageStartDateTime = $imageStartDateTime.AddMinutes("1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment