SOLUTION: Count colors used in an image [Branched from 2009 topic]
Add-Type -AssemblyName System.Drawing
function Get-UniqueColorCount {
param (
[string]$ImagePath
)
if (-Not (Test-Path $ImagePath)) {
Write-Host "File not found: $ImagePath"
return
}
$bitmap = [System.Drawing.Bitmap]::FromFile($ImagePath)
$colors = @{}
for ($x = 0; $x -lt $bitmap.Width; $x++) {
for ($y = 0; $y -lt $bitmap.Height; $y++) {
$color = $bitmap.GetPixel($x, $y).ToArgb()
$colors[$color] = $true
}
}
$bitmap.Dispose()
Write-Host "Unique colors in '$ImagePath': $($colors.Count)"
}
# Example usage:
Get-UniqueColorCount "C:\path\to\your\image.jpg"
