Advertisement
Guest User

Jpeg EXIF tag manipulation

a guest
Aug 3rd, 2010
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Powershell functions to read and modify the "Date Picture Taken" EXIF tag on jpeg images.
  2.  
  3. [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  4.  
  5. # Define function to return "Date Picture Taken" EXIF Tag value
  6. function get-datetaken {
  7. param ($path)
  8.   $bmp = new-object System.Drawing.Bitmap $path
  9.   $prop = $bmp.GetPropertyItem(36867)
  10.   $date = [System.Text.Encoding]::UTF8.GetString($prop.Value)
  11.   [DateTime]::Parse($date.Substring(0,4)+"/"+$date.Substring(5,2)+"/"+$date.Substring(8))
  12.   $bmp.Dispose()
  13. }
  14.  
  15. # Define function to set "Date Picture Taken" EXIF Tag value
  16. function set-datetaken {
  17. param ([string]$inputPath, [string]$outputPath, [DateTime]$date)
  18.   $dateBytes = [System.Text.Encoding]::UTF8.GetBytes($date.ToString("yyyy:MM:dd HH:mm:ss")) + 0
  19.   $bmp = new-object System.Drawing.Bitmap $inputPath
  20.   $prop = $bmp.GetPropertyItem(36867)
  21.   $prop.Value = $dateBytes
  22.   $bmp.SetPropertyItem($prop)
  23.   $bmp.Save($outputPath)
  24.   $bmp.Dispose()
  25. }
  26.  
  27. # Define function to increment "Date Picture Taken" EXIF Tag values by specified TimeSpan
  28. function adjust-datetaken {
  29. param ([string]$inputPattern, [string]$outputDirectory, [TimeSpan]$span)
  30.     gci $inputPattern | foreach { `
  31.         $out = [IO.Path]::Combine($outputDirectory, $_.Name); `
  32.         set-datetaken $_.FullName $out (get-datetaken $_.FullName).Add($span); `
  33.         write-output "$($out): $(get-datetaken $out)" `
  34.     }
  35. }
  36.  
  37. # get-datetaken "c:\temp\exif\test.jpg"
  38. # set-datetaken "c:\temp\exif\test.jpg" "c:\temp\exif\test-output.jpg" ([DateTime]::Parse("2009/08/05 7:06 PM"))
  39.  
  40. # $span = [DateTime]::Parse("2010/7/19 10:51AM") - [DateTime]::Parse("2004/12/29 11:37 PM")
  41. # adjust-datetaken "d:\home\photos\2010 Road Trip\DSC*.jpg" "C:\Temp\EXIF" $span
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement