Created
April 13, 2017 06:48
-
-
Save stuartleeks/b76886c97442d6287f12ac432cf3b0e1 to your computer and use it in GitHub Desktop.
photo-utils.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Drawing.dll") | |
function BytesToString($bytes) { | |
return [System.Text.Encoding]::ASCII.GetString($bytes) | |
} | |
function StringToBytes($string) { | |
return [System.Text.Encoding]::ASCII.GetBytes($string) | |
} | |
function GetImageTakenDate($filename) { | |
# https://blogs.technet.microsoft.com/jamesone/2007/07/13/exploring-photographic-exif-data-using-powershell-of-course/ | |
$resolvedFilename = (Resolve-Path $filename).Path | |
try { | |
$image = New-Object -TypeName system.drawing.bitmap -ArgumentList $resolvedFilename | |
$item = $image.GetPropertyItem(36867) | |
} | |
finally { | |
$image.Dispose() | |
} | |
$takenString = BytesToString $item.Value | |
$takenDate = [DateTime]::ParseExact($takenString, "yyyy:MM:dd HH:mm:ss`0", [System.Globalization.CultureInfo]::InvariantCulture) | |
return $takenDate | |
} | |
function SetImageTakenDate($filename, $newDate) { | |
$resolvedFilename = (Resolve-Path $filename).Path | |
$image = New-Object -TypeName system.drawing.bitmap -ArgumentList $resolvedFilename | |
$newDateString = $newDate.ToString("yyyy:MM:dd HH:mm:ss`0") | |
$newValue = StringToBytes $newDateString | |
$item = $image.GetPropertyItem(36867) | |
$item.Value = $newValue | |
$image.SetPropertyItem($item) | |
$image.Save("$resolvedFilename.new") | |
$image.Dispose() | |
Remove-Item -Path $resolvedFilename | |
Rename-Item -Path "$resolvedFilename.new" -NewName $resolvedFilename -Force | |
} | |
function ShiftImageTakenDate($filename, [Timespan] $timespan) { | |
$resolvedFilename = (Resolve-Path $filename).Path | |
$date = GetImageTakenDate $filename | |
$newDate = $date + $timespan | |
SetImageTakenDate $filename $newDate | |
} | |
function SortIntoPathsByDate($SourcePath) { | |
# Perform in-place organisation | |
if (-not $SourcePath.EndsWith("\")) { | |
$SourcePath += "\" | |
} | |
Get-ChildItem $SourcePath -Recurse -File ` | |
| ForEach-Object -Process { | |
$currentFile = $_ | |
try { | |
$PictureTime = GetImageTakenDate $currentFile.FullName | |
} | |
catch { | |
$PictureTime = $currentFile.LastWriteTime | |
} | |
$DestinationPath = $SourcePath + $PictureTime.ToString("yyyy\\yyyy_MM_dd") | |
$DestinationName = $DestinationPath + "\" + $currentFile.Name | |
if ((Test-Path $DestinationName)) { | |
Write-Output "Skipping $DestinationName - already exists" | |
} | |
else { | |
if (!(Test-Path $DestinationPath)) { | |
mkdir $DestinationPath | out-null | |
} | |
else { | |
Write-Output "Moving $($currentFile.FullName) to $DestinationName" | |
Move-Item $currentFile.FullName $DestinationName | |
} | |
} | |
} | |
} | |
function demo() { | |
# adjust date in jpg files | |
$startDate = get-date -Year 2017 -Month 01 -Day 22 | |
$endDate = get-date -Year 2017 -Month 02 -day 24 | |
$diff = $endDate - $startDate | |
Get-Item .\_emmobpics\*20170122*.jpg | ` | |
ForEach-Object { | |
ShiftImageTakenDate $_.FullName $diff | |
} | |
# rename files | |
Get-Item .\_emmobpics\*20170122*.jpg | ` | |
ForEach-Object { | |
Move-Item $_.FullName $($_.FullName.Replace("20170122", "20170224")) | |
} | |
} | |
function demo2() { | |
# adjust date in jpg files | |
$startDate = get-date -Year 2016 -Month 05 -Day 23 -Hour 15 -Minute 46 | |
$endDate = get-date -Year 2016 -Month 12 -day 13 -Hour 09 -Minute 12 | |
$diff = $endDate - $startDate | |
Get-Item .\_emmobpics.2\*20160523*.jpg | ` | |
ForEach-Object { | |
ShiftImageTakenDate $_.FullName $diff | |
} | |
# rename files | |
Get-Item .\_emmobpics.2\*20160523*.jpg | ` | |
ForEach-Object { | |
Move-Item $_.FullName $($_.FullName.Replace("20160523", "20161213")) | |
} | |
} | |
$ErrorActionPreference = "Stop" | |
# remove empty dirs: | |
# get-childitem -Directory | ?{ (Get-ChildItem $_.FullName | Measure-Object).Count -eq 0 } | remove-item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment