Hi folks
Sometimes we need to extract some exif data from images. We could do this with PowerShell.
Here is the most efficient way to do this
function Get-ExifProperty { param ( [string] $ImagePath, [int] $ExifTagCode ) $fullPath = (Resolve-Path $ImagePath).Path PSUsing ($fs = [System.IO.File]::OpenRead($fullPath)) ` { PSUsing ($image = [System.Drawing.Image]::FromStream($fs, $false, $false)) ` { if (-not $image.PropertyIdList.Contains($ExifTagCode)) { return $null } $propertyItem = $image.GetPropertyItem($ExifTagCode) $valueBytes = $propertyItem.Value $value = [System.Text.Encoding]::ASCII.GetString($valueBytes) -replace "`0$" return $value } } }
PSUsing was described in the previous blogpost
Here is the list of tags: http://www.digitalpreservation.gov/formats/content/tiff_tags.shtml
For example, date taken tag is http://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif/datetimeoriginal.html
Date in the following format: “yyyy:MM:dd HH:mm:ss”
$ExifTagCode_DateTimeOriginal = 0x9003 function Get-DateTaken { param ( [string] $ImagePath ) $str = Get-ExifProperty -ImagePath $ImagePath -ExifTagCode $ExifTagCode_DateTimeOriginal if ($str -eq $null) { return $null } $dateTime = [DateTime]::MinValue if ([DateTime]::TryParseExact($str, "yyyy:MM:dd HH:mm:ss", $null, [System.Globalization.DateTimeStyles]::None, [ref] $dateTime)) { return $dateTime } return $null }
Stay tuned
PS C:\Users\Jacques> Get-ExifProperty -ImagePath “\\Actarus\Download\fotocol\11001-AARTSELAAR 1 AFD\11001ADRIAAN SANDERSLEI____________________0000000002.JPG” -ExifTagCode $ExifTagCode_DateTimeOriginal
Exception lors de l’appel de « OpenRead » avec « 1 » argument(s) : « Le format du chemin d’accès donné n’est pas pris en charge. »
Au caractère Ligne:11 : 14
+ PSUsing ($fs = [System.IO.File]::OpenRead($fullPath)) `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
Nice content.