Magick, choco packages + more

This commit is contained in:
Gal Szkolnik 2021-11-15 14:42:46 -05:00
parent 0f84a2f136
commit c8b9f5d2dc
6 changed files with 202 additions and 0 deletions

View file

@ -0,0 +1,87 @@
[CmdletBinding(SupportsShouldProcess,ConfirmImpact="Medium")]param(
[Parameter(ParameterSetName="Normal",ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string[]]$ImageMagickPath,
[Parameter(ParameterSetName="Normal",ValueFromPipelineByPropertyName)]
[string]$PageOutputName,
[Parameter(ParameterSetName="Normal")]
[string]$OutputPath,
[Parameter(ParameterSetName="Normal")]
[string]$OutputImageFormat = "png",
[Parameter(ParameterSetName="Normal")]
[string]$OutputImageExtension,
[Parameter(ParameterSetName="Normal")]
[string[]]$MagickArgs,
[Parameter(ParameterSetName="Normal")]
[switch]$Force,
[Parameter(ParameterSetName="Reset")]
[switch]$ResetConfirm
)
BEGIN{
if( $ResetConfirm ) {
Remove-Variable imagemagick_* -Force
Remove-Variable imagemagick_* -Force
return
}
if( -not $OutputImageExtension ) {
$OutputImageExtension = $OutputImageFormat.ToLower()
}
$OutputImageExtension = $OutputImageExtension -replace '\.*(.+)$','.$1'
if( ! (Test-Path variable:global:imagemagick_noToAll) ) {
$imagemagick_noToAll = $PSBoundParameters.ContainsKey('WhatIf');
$imagemagick_yesToAll = $ConfirmPreference -gt 'Low' -and -not $imagemagick_noToAll
}
}
PROCESS{
if( $ResetConfirm ) { return }
$ImageMagickPath | ForEach-Object {
$local:i = [PSCustomObject]@{
ImageMagickPath = $_
PageOutputName = $(
if( $PageOutputName ) {
"$($PageOutputName)$OutputImageExtension"
} else {
[System.IO.Path]::GetFileName($_) -replace "(\.[A-Z]+)(?:\[(\d+)\])?$",'_$2.' -replace '_?\.$',$OutputImageExtension
}
)
OutputPath = $OutputPath
}
if( -not $i.OutputPath ) {
$i.OutputPath = Join-Path ([System.IO.Path]::GetDirectoryName($i.ImageMagickPath)) $OutputImageFormat
}
if( !$imagemagick_noToAll ) { try {
if( -not (Test-Path -LiteralPath $i.OutputPath) ) {
if( $Force ) {
New-Item -ItemType Directory -Path $i.OutputPath -Force
} else {
throw "Output directory $($i.OutputPath) does not exist!"
}
}
} catch {
Write-Error $_
$imagemagick_noToAll = $true
return
}}
$local:outFilePath = Join-Path $i.OutputPath $i.PageOutputName
$local:magickCmdLine=@("convert",$i.ImageMagickPath)+$MagickArgs+@($outFilePath) | Where-Object {$_}
Write-Verbose "$(if( $imagemagick_noToAll ) { "Skipping" } else {"Running"} ): & magick $magickCmdLine"
if( $PSCmdlet.ShouldContinue( "Convert", "$($i.ImageMagickPath) to $outFilePath", [ref]$imagemagick_yesToAll, [ref]$imagemagick_noToAll ) ) {
& magick $magickCmdLine
Write-Verbose "Error state: $?"
if( $? -and (Test-Path $outFilePath) ) {
Get-Item $outFilePath
}
}
}
}
END{
if( $ResetConfirm ) { return }
$global:imagemagick_yesToAll = $imagemagick_yesToAll
$global:imagemagick_noToAll = $imagemagick_noToAll
}

View file

@ -0,0 +1,33 @@
[CmdletBinding()]param(
[string[]]$SrcPath,
[string]$FilePattern="*.tif",
[int]$PageNumberDigits=3,
[switch]$Force
)
Remove-TypeData -TypeName "ImageMagickPagesz" -ErrorAction Ignore -Confirm:$false
Update-TypeData -TypeName "ImageMagickPagesz" -DefaultDisplayPropertySet 'ImageMagickPath', 'Page' -Force -Confirm:$false
$local:PageNumberFormat="D$PageNumberDigits"
Get-ChildItem $(Join-Path $SrcPath $FilePattern) -Force:$Force |
ForEach-Object {
$local:f = $_;
& magick identify $f.FullName | ForEach-Object {
$local:p = [ordered]@{
PSTypeName = "ImageMagickPagesz"
PSFile = $f
BaseName = $f.BaseName
ImageMagickPath = $_ -ireplace " TIFF \d+x\d+ \d+x\d+\+\d+\+\d .*$",""
Page = 0
PageOutputName = [string]::Empty
}
if( $p.ImageMagickPath -match '.*\[(\d+)\]$' ) {
$p.Page = $matches[1]
}
$p.Page = $(0+$($p.Page)).ToString($PageNumberFormat)
$p.PageOutputName = "$($p.BaseName)_$($p.Page)"
[PSCustomObject]$p
}
}

BIN
ImageMagick/_.package.json Normal file

Binary file not shown.