Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Last active June 23, 2023 02:14
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 Hashbrown777/044707504e724b6a1ffe41932008c599 to your computer and use it in GitHub Desktop.
Save Hashbrown777/044707504e724b6a1ffe41932008c599 to your computer and use it in GitHub Desktop.
Resizes high-resolution images to appropriate sizes for use in websites
Add-Type -AssemblyName System.Drawing
# https://gist.github.com/Hashbrown777/fae023538705a1f3f01cd795a2314e61#file-metadata-ps1
. './metadata.ps1'
Filter PointF {
[System.Drawing.PointF]::new($_[0], $_[1])
}
Function ToArray {
Begin { $output = [System.Collections.ArrayList]::new() }
Process { $output.Add($_) | Out-Null }
End { return ,$output }
}
Function precrop {
Param(
$Image,
[int32]$X = 0,
[int32]$Y = 0,
[int32]$Width,
[int32]$Height,
[int32]$U = 0,
[int32]$V = 0
)
if ($X -lt 0) {
$X += $Image.Width
}
if ($Y -lt 0) {
$Y += $Image.Height
}
if ($Width) {
$U = $X + $Width
}
else {
if ($U -lt 1) {
$U += $Image.Width
}
$Width = $U - $X
}
if ($Height) {
$V = $Y + $Height
}
else {
if ($V -lt 1) {
$V += $Image.Height
}
$Height = $V - $Y
}
[PSCustomObject]@{
width = $Width
height = $Height
x = $X
y = $Y
u = $U
v = $V
}
}
Function Crop {
Param(
[int32]$X = 0,
[int32]$Y = 0,
[int32]$Width,
[int32]$Height,
[int32]$U = 0,
[int32]$V = 0
)
Process {
$data = precrop `
-Image $_ `
-X $X -Y $Y `
-Width $Width -Height $Height `
-U $U -V $V
$new = New-Object System.Drawing.Bitmap($data.width, $data.height)
[System.Drawing.Graphics]::FromImage($new).DrawImage(
$_,
((0,0),($data.width,0),(0,$data.height) | PointF | ToArray),
[System.Drawing.RectangleF]::new($data.x, $data.y, $data.u, $data.v),
[System.Drawing.GraphicsUnit]::Pixel
)
$new
}
}
Function prescale {
Param(
$Image,
$Width,
$Height,
$Scale,
$MinWidth,
$MinHeight
)
Process {
if ($Width) {
$Scale = $Width / $Image.Width
}
elseif ($Height) {
$Scale = $Height / $Image.Height
}
$Width = $Image.Width * $Scale
$Height = $Image.Height * $Scale
if ($Width -lt $MinWidth) {
$Height = $Image.Height * ($Scale = ($Width = $MinWidth) / $Image.Width)
}
if ($Height -lt $MinHeight) {
$Width = $Image.Width * ($Scale = ($Height = $MinHeight) / $Image.Height)
}
[PSCustomObject]@{
width = [int32][Math]::Round($Width)
height = [int32][Math]::Round($Height)
}
}
}
Function Scale {
Param(
$Width,
$Height,
$Scale,
$MinWidth,
$MinHeight
)
Process {
$data = prescale `
-Image $_ `
-Width $Width `
-Height $Height
$new = New-Object System.Drawing.Bitmap($data.width, $data.height)
[System.Drawing.Graphics]::FromImage($new).DrawImage(
$_,
0,
0,
$data.width,
$data.height
)
$new
}
}
Function SaveJPG {
Param(
[long]$Quality = 90L,
$Name,
[switch]$PassThru
)
Begin {
$encoder = `
[System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() `
| ?{ $_.FormatDescription -eq 'JPEG' }
$params = [System.Drawing.Imaging.EncoderParameters]::new(1)
$params.Param[0] = [System.Drawing.Imaging.EncoderParameter]::new(
[System.Drawing.Imaging.Encoder]::Quality,
$Quality
)
}
Process {
$_.Save($Name, $encoder, $params)
if ($PassThru) {
$_
}
}
}
Get-ChildItem -Filter '*-56.jpg' -Recurse `
| %{
$file = $_
#Destination NSW images are suffixed with -56
$name = $file.BaseName -replace '-56',''
$folder = $file.Directory.Parent
"$folder`t$name"
$folder = $folder.FullName
$image = [System.Drawing.Image]::FromFile((Get-Item $file.FullName))
'' + [Math]::Round($file.Length / 1MB, 2) + 'mB'
''
$properties = $file | MetaData '^(Tags|Subject|Title|Dimensions)$'
$properties `
| Get-Member -Type NoteProperty `
| %{
$_.Name,$properties."$($_.Name)" -join "`t"
''
}
#Destination NSW images contain...
$bottom = $image.Height - 1;
for (
$index = 0;
#...a white field with black text (grey pixels)...
$image.GetPixel(200, $bottom).Name -match '^..(..)\1\1$' -and
$image.GetPixel(400, $bottom).Name -match '^..(..)\1\1$' -and
$image.GetPixel(600, $bottom).Name -match '^..(..)\1\1$';
#...starting at 543px in height +50n
#where n is the number of extra lines in the description
$bottom -= @(50,543)[!$index++]
) {}
"Cropping out $($image.Height - $bottom)px footer",''
$image = $image `
| Crop -V $bottom `
| SaveJPG `
-Quality 95L `
-Name "$folder/_original/$name.jpg" `
-PassThru
$scale = prescale -Image $image -Width 1920 -MinHeight 720
"Scaling to 1920x$($scale.height)"
$file = "$folder/$name.jpg"
$image `
| Scale -Width 1920 -MinHeight 720 `
| SaveJPG -Name $file
'' + [Math]::Round((gci $file).Length / 1MB, 2) + 'mB'
''
if ($scale.Height -gt 720) {
$scale = prescale -Image $image -Height 720 -MinWidth 768
"Mobile scaling to $($scale.width)x720"
$file = "$folder/mobile-$name.jpg"
$image `
| Scale -Height 720 -MinWidth 768 `
| SaveJPG -Name $file
'' + [Math]::Round((gci $file).Length / 1MB, 2) + 'mB'
''
}
''
} `
| Tee 'carousel.txt'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment