-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-ImageMetaData.ps1
More file actions
114 lines (108 loc) · 4.29 KB
/
Get-ImageMetaData.ps1
File metadata and controls
114 lines (108 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
## Get-ImageMetaData -- pull EXIF, XMP, and other data from images using the BitmapMetaData
## Usage: ls *.jpg | Get-ImageMetaData | ft Length, LastWriteTime, Name, "36867"
## Note that '36867' is the decimal value of (0x9003) the EXIF tag for DateTimeOriginal
## For more information see: http://owl.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
## http://poshcode.org/617
#####################################################################################################
## History:
## - v1.0 - First release, retrieves all the data and stacks it somehow onto a FileInfo object
#####################################################################################################
# filter Get-ImageMetadata {
PARAM($file)
BEGIN {
Try
{
$null = [Reflection.Assembly]::LoadWithPartialName("PresentationCore");
function Get-ImageMetadata
{
PARAM([System.Windows.Media.Imaging.BitmapFrame]$bitmapFrame, [string]$path)
PROCESS
{
Try
{
#Write-Log $path
if($path -is [string])
{
## To read metadata, you use GetQuery. To write metadata, you use SetQuery
## To WRITE metadata, you need a writer,
## but first you have to open the file ReadWrite, instead of Read only
# $writer = $bitmapFrame.CreateInPlaceBitmapMetadataWriter();
# if ($writer.TrySave()){
# $writer.SetQuery("/tEXt/{str=Description}", "Have a nice day.");
# } else {
# Write-Host "Couldn't save data" -Fore Red
# }
$next=$bitmapFrame.MetaData.GetQuery($path);
if($next.Location)
{
$next | ForEach-Object { Get-ImageMetadata $bitmapFrame "$($next.Location)$_" }
}
else
{
if($path.Split("/")[-1] -match "{ushort=(?<code>\d+)}")
{
# $path = "0x{0:X}" -f [int]$matches["code"]
$path = [int]$matches["code"]
}
Add-Member -in ($Global:ImageMetaData) -Type NoteProperty -Name $path -value $next -Force
# @{$path=$next}
}
}
else
{
$bitmapFrame.Metadata | ForEach-Object { Get-ImageMetadata $bitmapFrame $_ }
}
}
Catch
{
Write-Log "ERROR: $($_.Exception.Message)"
Write-Log "ERROR: $($_.InvocationInfo.PositionMessage.Split('+')[0])"
#Write-Log $path
}
}
}
}
Catch
{
Write-Log "ERROR: $($_.Exception.Message)"
Write-Log "ERROR: $($_.InvocationInfo.PositionMessage.Split('+')[0])"
}
}
PROCESS
{
Try
{
if($_) { $file = $_ }
if($file -is [IO.FileInfo])
{
$file = [string]$file.FullName;
}
elseif($file -is [String])
{
$file = [string](Resolve-Path $file)
}
$Global:ImageMetaData = New-Object IO.FileInfo $file
$stream = new-object IO.FileStream $file, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read);
& {
$decoder = [System.Windows.Media.Imaging.BitmapDecoder]::Create( $stream, "None", "Default" )
$bitmapFrame = $decoder.Frames[0];
$bitmapFrame.Metadata | ForEach-Object {
#Write-Log $_
Get-ImageMetadata $bitmapFrame $_
}
}
trap
{
Write-Error "WARNING: $_"
continue;
}
$stream.Close()
$stream.Dispose()
Write-Output $Global:ImageMetaData
}
Catch
{
Write-Log "ERROR: $($_.Exception.Message)"
Write-Log "ERROR: $($_.InvocationInfo.PositionMessage.Split('+')[0])"
}
}