Skip to content

Instantly share code, notes, and snippets.

@peaeater
Last active June 22, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peaeater/7709748 to your computer and use it in GitHub Desktop.
Save peaeater/7709748 to your computer and use it in GitHub Desktop.
Produces a JPG per TIF, requires imagemagick => http://www.imagemagick.org/
<#
1. Leaf
Given a text file of TIF/TIFF filenames, create JPGs from TIFs
and create a mirror directory structure for JPG file outputs.
* Handles filenames with entry separators
* Ignores TIF older than its JPG mirror unless -force param is used
* Requires imagemagick
.\tif2jpg.ps1 -in c:\dev\abc\extract\extracted\tifs\abc-tifs-1.txt `
-pathsnip \\abc\archive\bigpictures `
-pathprefix c:\dev\abc\raw\largeimages `
-outdir c:\dev\abc\raw\media `
2. Container
Given a directory path, create JPGs from TIFs recursively
and create a mirror directory structure for JPG file outputs.
* Performs in-place if outdir is not provided
* Ignores TIF older than its JPG mirror unless -force param is used
* Requires imagemagick
.\tif2jpg.ps1 -indir c:\dev\abc\raw\largeimages -outdir c:\dev\abc\raw\media
or, in-place:
.\tif2jpg.ps1 -indir c:\dev\abc\raw\largeimages
#>
param (
[Parameter(Mandatory=$true, ParameterSetName="Leaf")]
[string]$in,
[Parameter(Mandatory=$true, ParameterSetName="Leaf")]
[string]$pathsnip,
[Parameter(Mandatory=$true, ParameterSetName="Leaf")]
[string]$pathprefix,
[Parameter(Mandatory = $true, ParameterSetName = "Container")]
[string]$indir,
[Parameter(Mandatory = $true, ParameterSetName = "Leaf")]
[Parameter(Mandatory = $false, ParameterSetName = "Container")]
[string]$outdir = $indir,
[Parameter(Mandatory = $false, ParameterSetName = "Leaf")]
[Parameter(Mandatory = $false, ParameterSetName = "Container")]
[int]$size = "1000",
[Parameter(Mandatory = $false, ParameterSetName = "Leaf")]
[Parameter(Mandatory = $false, ParameterSetName = "Container")]
[switch]$force,
[Parameter(Mandatory = $false, ParameterSetName = "Leaf")]
[Parameter(Mandatory = $false, ParameterSetName = "Container")]
[string]$magick = "c:\utils\imagemagick\magick.exe"
)
<#
FUNCTIONS
#>
. .\helper.logging.ps1
function getValidPaths([string]$in, [string]$pathprefix, [string]$pathsnip) {
$paths = get-content $in | Where-Object { $_ -ne '' } | ForEach-Object { $_.split('|') }
$i = 0
$e = 0
$validPaths = @()
foreach ($path in $paths) {
$i++
try {
if ($pathsnip) {
$path = $path.Replace($pathsnip, "", [System.StringComparison]::OrdinalIgnoreCase)
}
if ($pathprefix) {
$path = join-path $pathprefix $path
}
$path = $path.trim()
$ok = test-path $path -PathType Leaf -ErrorAction Stop
if ($ok) {
write-host "$path Found"
$validPaths = $validPaths + $path
}
else {
write-host "$path Not Found"
}
# update progress display
write-progress -activity "Validating file paths..." -status "Processing $i of $($paths.Count)" -percentcomplete (($i / $paths.Count) * 100)
}
catch [Exception] {
$e++
$msg = "Error occurred while validating $path. $($_.Exception)"
logError $logsrc $msg
}
}
return $validPaths
}
<#
MAIN
#>
if ($indir) {
$files = get-childitem "$indir\*" -include *.tif,*.tiff -Recurse
$pathprefix = $indir
}
if ($in) {
$files = get-childitem $(getValidPaths $in $pathprefix $pathsnip) -include *.tif,*.tiff
}
$i = 0
$e = 0
$skip = 0
foreach ($file in $files) {
$i++
$update = $true
$status = "Processing $i of $($files.Count)."
write-progress -activity "Converting to JPG..." -status $status -PercentComplete (($i / $files.Count) * 100)
try {
# jpg path => [base out dir] + [tif parent dir w/o qualifier] + .jpg
$jpgdir = join-path $outdir $($file.DirectoryName.Replace($pathprefix, "", [System.StringComparison]::OrdinalIgnoreCase))
$jpgpath = [System.IO.Path]::Combine($jpgdir, $("{0}.jpg" -f $file.BaseName))
# if jpg already exists and timestamp is newer than tif, skip (unless -force is active)
if ((-not $force) -and (test-path $jpgpath)) {
$jpgfile = get-itemproperty -path $jpgpath
if ($jpgfile.LastWriteTime -gt $file.LastWriteTime) {
write-output "$jpgpath Skipped"
$update = $false
}
}
# create jpg from tif
if ($force -or $update) {
if (!(test-path $jpgdir)) {
mkdir $jpgdir | out-null
}
$arguments = "`"$($file.FullName)`" -resize $size `"$jpgpath`""
start-process $magick $arguments -wait -NoNewWindow
}
else {
$skip++
}
}
catch [Exception] {
$e++
$msg = "Error occurred while processing $($file.FullName). $($_.Exception)"
logError $logsrc $msg
}
}
# final report
$msg = "TIF to JPG finished processing $i {0} to $outdir. $skip skipped, $e {1}." -f $(if ($i -eq 1) {"file"} else {"files"}),$(if ($e -eq 1) {"error"} else {"errors"})
logInfo $logsrc $msg
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment