Skip to content

Commit b4412f6

Browse files
committed
Update mask-rescaler.ps1
1 parent cd133fc commit b4412f6

File tree

1 file changed

+108
-98
lines changed

1 file changed

+108
-98
lines changed

mask-rescaler.ps1

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,135 @@
11
# Mask Rescaler
22
# =============
33
#
4-
# If the PDF image extractor has given you masks that don't match the image dimensions, this script can help. First, make sure to remove ALL files from the
5-
# directory that aren't the mismatched images/masks. Also remove any unwanted images; the images must be in the 'image, mask, image, mask, ...' order that
6-
# the extractor requires.
4+
# If the PDF image extractor has given you masks that don't match the image dimensions, this script can help.
75
#
8-
# Place this script in the same directory as the images/masks. If on Windows, right-click and select 'Run with PowerShell'. If on macOS/Linux, you'll
9-
# probably have to install PowerShell and run this script via command line.
6+
# First, make sure to remove ALL files from the directory that aren't the mismatched images/masks. Also remove
7+
# any unwanted images; the images must be in the 'image, mask, image, mask, ...' order that the extractor
8+
# requires. Place this script in the same directory as the images/masks. Run the script.
109
#
11-
# Rescaled masks have `_resized` appended to their name, and all old masks are moved to a new directory surprisingly called `old_masks`.
10+
# Rescaled masks have `_resized` appended to their name, and all old masks are moved to a new directory
11+
# surprisingly called `old_masks`. Images are unaffected.
1212
#
1313
# If you still don't understand what this script does, I recommend not using it.
1414
#
1515
#
16-
# Spappz 2022
16+
# Spappz 2024
1717
#
1818
# MIT License
1919

2020
Add-Type -Assembly System.Drawing
2121

22-
# Resize-Image function (copied 2022-01-20) by Christopher Walker at https://gist.github.com/someshinyobject/617bf00556bc43af87cd
22+
# Resize-Image function by Christopher Walker at https://gist.github.com/someshinyobject/617bf00556bc43af87cd
23+
# Copied 2022-01-20
2324
Function Resize-Image {
24-
[CmdLetBinding(
25-
SupportsShouldProcess=$true,
26-
PositionalBinding=$false,
27-
ConfirmImpact="Medium",
28-
DefaultParameterSetName="Absolute"
29-
)]
30-
Param (
31-
[Parameter(Mandatory=$True)]
32-
[ValidateScript({
33-
$_ | ForEach-Object {
34-
Test-Path $_
35-
}
36-
})][String[]]$ImagePath,
37-
[Parameter(Mandatory=$False)][Switch]$MaintainRatio,
38-
[Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Height,
39-
[Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Width,
40-
[Parameter(Mandatory=$False, ParameterSetName="Percent")][Double]$Percentage,
41-
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.SmoothingMode]$SmoothingMode = "HighQuality",
42-
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.InterpolationMode]$InterpolationMode = "HighQualityBicubic",
43-
[Parameter(Mandatory=$False)][System.Drawing.Drawing2D.PixelOffsetMode]$PixelOffsetMode = "HighQuality",
44-
[Parameter(Mandatory=$False)][String]$NameModifier = "resized"
45-
)
46-
Begin {
47-
If ($Width -and $Height -and $MaintainRatio) {
48-
Throw "Absolute Width and Height cannot be given with the MaintainRatio parameter."
49-
}
50-
51-
If (($Width -xor $Height) -and (-not $MaintainRatio)) {
52-
Throw "MaintainRatio must be set with incomplete size parameters (Missing height or width without MaintainRatio)"
53-
}
54-
55-
If ($Percentage -and $MaintainRatio) {
56-
Write-Warning "The MaintainRatio flag while using the Percentage parameter does nothing"
57-
}
58-
}
59-
Process {
60-
ForEach ($Image in $ImagePath) {
61-
$Path = (Resolve-Path $Image).Path
62-
$Dot = $Path.LastIndexOf(".")
25+
[CmdLetBinding(
26+
SupportsShouldProcess = $true,
27+
PositionalBinding = $false,
28+
ConfirmImpact = 'Medium',
29+
DefaultParameterSetName = 'Absolute'
30+
)]
31+
Param (
32+
[Parameter(Mandatory = $True)]
33+
[ValidateScript({
34+
$_ | ForEach-Object {
35+
Test-Path $_
36+
}
37+
})][String[]]$ImagePath,
38+
[Parameter(Mandatory = $False)][Switch]$MaintainRatio,
39+
[Parameter(Mandatory = $False, ParameterSetName = 'Absolute')][Int]$Height,
40+
[Parameter(Mandatory = $False, ParameterSetName = 'Absolute')][Int]$Width,
41+
[Parameter(Mandatory = $False, ParameterSetName = 'Percent')][Double]$Percentage,
42+
[Parameter(Mandatory = $False)][System.Drawing.Drawing2D.SmoothingMode]$SmoothingMode = 'HighQuality',
43+
[Parameter(Mandatory = $False)][System.Drawing.Drawing2D.InterpolationMode]$InterpolationMode = 'HighQualityBicubic',
44+
[Parameter(Mandatory = $False)][System.Drawing.Drawing2D.PixelOffsetMode]$PixelOffsetMode = 'HighQuality',
45+
[Parameter(Mandatory = $False)][String]$NameModifier = 'resized'
46+
)
47+
Begin {
48+
If ($Width -and $Height -and $MaintainRatio) {
49+
Throw 'Absolute Width and Height cannot be given with the MaintainRatio parameter.'
50+
}
6351

64-
#Add name modifier (OriginalName_{$NameModifier}.jpg)
65-
$OutputPath = $Path.Substring(0,$Dot) + "_" + $NameModifier + $Path.Substring($Dot,$Path.Length - $Dot)
66-
67-
$OldImage = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Path
68-
# Grab these for use in calculations below.
69-
$OldHeight = $OldImage.Height
70-
$OldWidth = $OldImage.Width
71-
72-
If ($MaintainRatio) {
73-
$OldHeight = $OldImage.Height
74-
$OldWidth = $OldImage.Width
75-
If ($Height) {
76-
$Width = $OldWidth / $OldHeight * $Height
77-
}
78-
If ($Width) {
79-
$Height = $OldHeight / $OldWidth * $Width
80-
}
81-
}
82-
83-
If ($Percentage) {
84-
$Product = ($Percentage / 100)
85-
$Height = $OldHeight * $Product
86-
$Width = $OldWidth * $Product
87-
}
52+
If (($Width -xor $Height) -and (-not $MaintainRatio)) {
53+
Throw 'MaintainRatio must be set with incomplete size parameters (Missing height or width without MaintainRatio)'
54+
}
8855

89-
$Bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Width, $Height
90-
$NewImage = [System.Drawing.Graphics]::FromImage($Bitmap)
91-
92-
#Retrieving the best quality possible
93-
$NewImage.SmoothingMode = $SmoothingMode
94-
$NewImage.InterpolationMode = $InterpolationMode
95-
$NewImage.PixelOffsetMode = $PixelOffsetMode
96-
$NewImage.DrawImage($OldImage, $(New-Object -TypeName System.Drawing.Rectangle -ArgumentList 0, 0, $Width, $Height))
56+
If ($Percentage -and $MaintainRatio) {
57+
Write-Warning 'The MaintainRatio flag while using the Percentage parameter does nothing'
58+
}
59+
}
60+
Process {
61+
ForEach ($Image in $ImagePath) {
62+
$Path = (Resolve-Path $Image).Path
63+
$Dot = $Path.LastIndexOf('.')
9764

98-
If ($PSCmdlet.ShouldProcess("Resized image based on $Path", "save to $OutputPath")) {
99-
$Bitmap.Save($OutputPath)
100-
}
101-
102-
$Bitmap.Dispose()
103-
$NewImage.Dispose()
104-
$OldImage.Dispose()
105-
}
106-
}
65+
#Add name modifier (OriginalName_{$NameModifier}.jpg)
66+
$OutputPath = $Path.Substring(0, $Dot) + '_' + $NameModifier + $Path.Substring($Dot, $Path.Length - $Dot)
67+
68+
$OldImage = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Path
69+
# Grab these for use in calculations below.
70+
$OldHeight = $OldImage.Height
71+
$OldWidth = $OldImage.Width
72+
73+
If ($MaintainRatio) {
74+
$OldHeight = $OldImage.Height
75+
$OldWidth = $OldImage.Width
76+
If ($Height) {
77+
$Width = $OldWidth / $OldHeight * $Height
78+
}
79+
If ($Width) {
80+
$Height = $OldHeight / $OldWidth * $Width
81+
}
82+
}
83+
84+
If ($Percentage) {
85+
$Product = ($Percentage / 100)
86+
$Height = $OldHeight * $Product
87+
$Width = $OldWidth * $Product
88+
}
89+
90+
$Bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Width, $Height
91+
$NewImage = [System.Drawing.Graphics]::FromImage($Bitmap)
92+
93+
#Retrieving the best quality possible
94+
$NewImage.SmoothingMode = $SmoothingMode
95+
$NewImage.InterpolationMode = $InterpolationMode
96+
$NewImage.PixelOffsetMode = $PixelOffsetMode
97+
$NewImage.DrawImage($OldImage, $(New-Object -TypeName System.Drawing.Rectangle -ArgumentList 0, 0, $Width, $Height))
98+
99+
If ($PSCmdlet.ShouldProcess("Resized image based on $Path", "save to $OutputPath")) {
100+
$Bitmap.Save($OutputPath)
101+
}
102+
103+
$Bitmap.Dispose()
104+
$NewImage.Dispose()
105+
$OldImage.Dispose()
106+
}
107+
}
107108
}
108109

109110
# Grab paths to all the files in the directory
110-
$imageList = Get-ChildItem | Where-Object {$_.Name -match '\.png$'} | Select-Object -ExpandProperty Name | Resolve-Path | Select-Object -ExpandProperty Path
111+
$imageList = Get-ChildItem |
112+
Where-Object { $_.Name -match '\.png$' } |
113+
Select-Object -ExpandProperty Name |
114+
Resolve-Path |
115+
Select-Object -ExpandProperty Path
111116

112117
# Assume the order is image, mask, image, mask
118+
$resizedMasks = [System.Collections.Generic.List[string]]::new()
113119
for ($i = 0; $i -lt $imageList.Count; $i = $i + 2) {
114-
# Read image for reference
115-
$image = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $imageList[$i]
116-
# Rescale and save mask based on the image's dimensions
117-
Resize-Image -Width $image.Width -Height $image.Height -ImagePath $imageList[$i + 1]
120+
# Read image for reference
121+
$image = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $imageList[$i]
122+
$mask = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $imageList[$i + 1]
123+
# Rescale and save mask based on the image's dimensions
124+
if ($image.Width -ne $mask.Width -or $image.Height -ne $mask.Height) {
125+
$resizedMasks.Add($imageList[$i + 1].Name)
126+
Resize-Image -Width $image.Width -Height $image.Height -ImagePath $imageList[$i + 1]
127+
}
118128
}
119129

120130
# Move old masks into an `old_masks` directory as clean-up
121-
$imageListNew = Get-ChildItem | Where-Object {$_.Name -match '\.png$'} | Select-Object -ExpandProperty Name
122-
$null = New-Item -ItemType Directory -Name old_masks
123-
for ($i = 1; $i -lt $imageListNew.Count; $i = $i + 3) {
124-
Move-Item -Path $imageListNew[$i] -Destination old_masks
125-
}
131+
$null = New-Item -ItemType Directory -Name ../old_masks
132+
$resizedMasks |
133+
ForEach-Object { $_ -replace '_resized(?=\.png$)' } |
134+
Move-Item -Destination ../old_masks
135+

0 commit comments

Comments
 (0)