No one could ever accuse me of being a shutterbug. I’m more of a casual photographer and much most of that was from mobile devices. But that’s not the be-all-end-all. I’ve also got scores of scans of old photographs, screenshots, and a bunch of other graphics that have accumulated over the years. Thankfully modern mobile devices tag photos with a plethora of information. Now it’s just a matter of getting to that information. These tags can help you organize your data so you don’t run into my problems.
Defining the Problem
I used to sync these files to my computer manually years ago – and that was fine, but then I found myself in this place:
That’s 13,316 JPG files.
Since over the years I’ve used all manner of synchronization processes (manual mobile device sync, copy/paste from other computers, cloud sync) everything was strewn all over 300 different folders.
This had to end. I needed a better way to handle the pictures. I elected to sort them by the date. Now if they all came from the same source, I could have probably used the Creation Time field to do the grouping. Unfortunately, that wasn’t an option – the creation time was all over the map due to synchronizations, migrations, and copies of copies. Thankfully, most of them had metadata with the information needed.
Now to access that information. Using PowerShell it’s easy to get details about a file – just pipe it to Format-List and get all the member information, right?
Well now there’s a problem. The “date taken” information is missing.
Turn out it’s actually encoded within the file. Well, this just got harder.
A Solution is Found!
I did some digging into how to access the metadata and that led me to the taglib-sharp project. It looked fairly straight-forward. It’s basically a DLL that when loaded allows you to access the metadata tags (both for read and for write). The problem is that the DLL isn’t shipped as part of the project – so I needed to compile it. First I downloaded the most recent copy of the project and extracted the content into a folder.
Now I need to actually compile the DLL. For that I downloaded and installed Visual Studio Community Edition and the .NET Core 2.1 SDK. Then I opened the Solution (SLN) file in Visual Studio. I elected to use the Dark Theme because dark_theme_is_life.
That’s all the prerequisites I need. Then I just click Build and Build Solution.
Now I’ve got a new DLL that was created in the Debug\bin\Debug\net40 folder.
I could leave that DLL there and just reference if, but I’d prefer to keep it with my other PowerShell scripts (in OneDrive). So I copied it over to my “_includes” folder.
A Solution is Found (Part 2)
Now a little bit about using this DLL and referencing it within PowerShell.
You’ll need to load the DLL into the active PowerShell memory space. There are a couple of ways to do this, but I elected to load it via an assembly.
$TagLibrary = "$env:UserProfile\OneDrive\Scripts\_includes\taglib-sharp.dll"
[System.Reflection.Assembly]::LoadFile($TagLibrary) | Out-Null
If you run this with the proper path for the tag library and get no errors, then it’s loaded.
Now let’s do a test. I don’t remember where I learned about this syntax, but it’s as follows:
#
# [Taglib.file]::Create( <# Path to File #> )
#
[Taglib.file]::Create( $Photo.FullName )
Returns:
Properties : TagLib.Properties
Tag : TagLib.Image.CombinedImageTag
ImageTag : TagLib.Image.CombinedImageTag
TagTypesOnDisk : TiffIFD
TagTypes : TiffIFD
Name : [Full Path of file]\IMG_20180414_091035.jpg
MimeType : taglib/jpg
Tell : 0
Length : 0
InvariantStartPosition : -1
InvariantEndPosition : -1
Mode : Closed
FileAbstraction : TagLib.File+LocalFileAbstraction
Writeable : False
PossiblyCorrupt : True
CorruptionReasons : {Size of entries exceeds possible data size}
Saving that to a variable ($Media) allows me to access the information stored in the tags easier.
$Media = [Taglib.file]::Create( $Photo.FullName )
$Media | Get-Member
$Media.Tag
$Media.ImageTag.DateTime
Nice! There’s the timestamp of the picture when it was taken. Also I get the GPS coordinates, the make/model of the camera, and some other interesting information.
That’s enough for day one. Tomorrow, we’ll look at how I took that information and worked with it.
Did you see that you got “Size of entries exceeds possible data size”
I have this problem that I can’t write to all my jpg files. I know that I need to set the tag before using the taglib and that solves a few of my problems.
Still after this I have a bunch of files that it can’t write to. And I run as Administrator.
I didn’t notice it at first glance. I’m not sure it has any bearing on the type of information I’m trying to detail. The full script is in the second part of this series.