Skip to content

Instantly share code, notes, and snippets.

@maoyeedy
Created April 19, 2025 20:26
Show Gist options
  • Save maoyeedy/769ad8f2f4faf3f5c219b07658bc3880 to your computer and use it in GitHub Desktop.
Save maoyeedy/769ad8f2f4faf3f5c219b07658bc3880 to your computer and use it in GitHub Desktop.
QuadSpriteProcessor-Powershell
<#
.SYNOPSIS
Make Textures Quad-Divisible
.DESCRIPTION
Resize PNG and JPG images so their dimensions are divisible by 4.
Useful for textures in game engines that require quad-divisible dimensions to be compressed with DXT or BC formats.
.NOTES
Requires ImageMagick to be installed and available in the PATH.
#>
function Resize-TextureToDivisibleBy4 {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$FilePath
)
process {
$extension = [System.IO.Path]::GetExtension($FilePath).ToLower()
if ($extension -notin @(".png", ".jpg", ".jpeg")) {
Write-Host "File is not a supported image format: $FilePath"
return
}
try {
$dimensions = $(magick identify -format "%w %h" $FilePath) -split " "
$width = [int]$dimensions[0]
$height = [int]$dimensions[1]
if ($width % 4 -eq 0 -and $height % 4 -eq 0) {
Write-Host "Already divisible by 4: $FilePath ($width×$height)"
return
}
$newWidth = if ($width % 4 -ne 0) { [math]::Ceiling($width / 4) * 4 } else { $width }
$newHeight = if ($height % 4 -ne 0) { [math]::Ceiling($height / 4) * 4 } else { $height }
magick mogrify -filter Box -resize "${newWidth}x${newHeight}!" $FilePath
Write-Host "Resized: $FilePath ($width×$height → $newWidth×$newHeight)"
}
catch {
Write-Error "Failed to process $FilePath $_"
}
}
}
function Find-AndResizeTextures {
[CmdletBinding()]
param (
[Parameter()]
[string]$Path = ".",
[Parameter()]
[switch]$Recursive,
[Parameter()]
[switch]$WhatIf
)
$searchOptions = @{
Path = $Path
Include = @("*.png", "*.jpg", "*.jpeg")
}
if ($Recursive) {
$searchOptions.Add("Recurse", $true)
}
$files = Get-ChildItem @searchOptions
Write-Host "Found $($files.Count) image files to check..."
foreach ($file in $files) {
if ($WhatIf) {
Write-Host "WhatIf: Would process $($file.FullName)"
}
else {
Resize-TextureToDivisibleBy4 -FilePath $file.FullName
}
}
}
# Example usage:
# Process a single file:
# Resize-TextureToDivisibleBy4 -FilePath "path\to\texture.png"
#
# Process all images in current directory:
# Find-AndResizeTextures
#
# Process all images in a directory and subdirectories:
# Find-AndResizeTextures -Path "path\to\textures\" -Recursive
#
# See what would be processed without making changes:
# Find-AndResizeTextures -Path "path\to\textures\" -Recursive -WhatIf
@maoyeedy
Copy link
Author

Requires ImageMagick installed.

@maoyeedy
Copy link
Author

maoyeedy commented May 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment