Accessing EXIF Photo Data from JPEGs with PowerShell
Omar and I were chatting today and he wanted to to know how to access EXIF data from PowerShell. Omar wrote a nice little photo library a while back that extracts and interprets EXIF data from images. The .NET Framework 2.0 has some support for EXIF, but it doesn't attempt to interpret it.
Anyway, Omar wants to do some manipulation of his files, doing renaming and such, based on the EXIF data. He wanted to sort a directory by the date the format was actually TAKEN - this is stored inside the photo itself if the time is right on your camera - not the File System Dates.
We could write scripts to manipulate the files his library directly, but wouldn't it be very integrated with the whole PowerShell experience, now would it?
So, here's what we did:
- Put Omar's PhotoLibrary.dll in your MyDocuments/PSConfiguration folder.
- Add this to your Microsoft.PowerShell_profile.ps1 file (or make one if you don't have it):
- $profileTypes = $profile | split-path | join-path -childPath "My.Types.ps1xml"
Update-TypeData $profileTypes
$photoLibrary = $profile | split-path | join-path -childPath "PhotoLibrary.dll"
[System.Reflection.Assembly]::LoadFrom($photoLibrary) - Make (or add to) a My.Types.ps1xml file in your MyDocuments/PSConfiguration folder:
- <Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>DatePhotoTaken</Name>
<GetScriptBlock>
if ($this.Extension -match "jpg|raw")
{
$photo = new-object PhotoLibrary.Photo $this.FullName
$photo.DateTimeOriginal
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
This is one of the most powerful aspect of PowerShell. Actually "spot-welding" new properties on to existing objects. Not object inheritance, mind you, "super-gluing." Like it or hate it, like super-glue, you have to respect that it solves problems.
Now we can do this from our PowerShell:
PS C:\Documents and Settings\shanselm\Desktop>
dir *.jpg | sort -desc DatePhotoTaken | select Name, LastWriteTime, DatePhotoTaken
Name LastWriteTime DatePhotoTaken
---- ------------- --------------
Z Q. at house Jan 1... 1/19/2006 4:26:00 PM 1/18/2006 8:01:00 AM
dadandscott.jpg 9/7/2006 2:37:32 PM 2/3/2002 2:57:03 PM
Since we're using Omar's library, we can do whatever we like with any the other EXIF details he expose:
PS C:\Documents and Settings\shanselm\Desktop>
$photo = new-object PhotoLibrary.Photo ((get-item dadandscott.jpg).Fullname)
PS C:\Documents and Settings\shanselm\Desktop>
$photo | select make, model, Width, Height, DateTimeOriginal | format-list
Make : Eastman Kodak Company
Model : KODAK DC265 ZOOM DIGITAL CAMERA (V01.00)
Width : 1536
Height : 1024
DateTimeOriginal : 2/3/2002 2:57:03 PM
Shiny.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Scott, is it possible to add a shortcut to the context menu that links to a PowerShell script and uses the folder path as a parameter?
Comments are closed.