Skip to content

Instantly share code, notes, and snippets.

@kneeprayer
Last active August 13, 2022 13:42
Show Gist options
  • Save kneeprayer/cf318264636cf1e0c904baa5f4e650cb to your computer and use it in GitHub Desktop.
Save kneeprayer/cf318264636cf1e0c904baa5f4e650cb to your computer and use it in GitHub Desktop.
Invert Color Of An Image Using Powershell
#
# Usage : .\Invert-Image.ps1 .\test*.png
#
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
Get-ChildItem $args[0] | ForEach-Object {
$image = New-Object System.Drawing.Bitmap($_.fullname)
for ($y = 0; $y -lt $image.Height; $y++) {
for ($x = 0; $x -lt $image.Width; $x++) {
$pixelColor = $image.GetPixel($x, $y)
$varR = 255 - $pixelColor.R
$varG = 255 - $pixelColor.G
$varB = 255 - $pixelColor.B
$image.SetPixel($x, $y, [System.Drawing.Color]::FromArgb($varR, $varG, $varB))
}
}
$image.MakeTransparent($image.GetPixel(10, 10))
$newFileName = $_.FullName -replace ".png", "-2.png"
$image.Save($newFileName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment