1. 把当前目录的矩形图片变圆形图片,"矩形变圆形.ps1.bat"- <#*,:
- @echo off&cd /d "%~dp0"&set "batchfile=%~f0"
- powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- # convert rectangle to ellipse
- Add-Type -AssemblyName System.Drawing
-
- Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object {
- try {
- $_.Name
- $bytes = [IO.File]::ReadAllBytes($_.FullName)
- $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes)
- $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream)
- $width = $bmp.Width
- $height = $bmp.Height
- $a = $width / 2
- $b = $height / 2
- $centerX = $width / 2
- $centerY = $height / 2
- foreach ($x in 0..($width - 1)) {
- foreach ($y in 0..($height - 1)) {
- # // 椭圆公式: (x - centerX)^2 / a^2 + (y - centerY)^2 / b^2 > 1
- $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2)
- if ($equation -gt 1) {
- $color = $bmp.GetPixel($x, $y)
- # NOTE:Both System.Drawing and imagemagick will set RGB channel to 0 when alpha channel is 0.
- $transparent_color = [System.Drawing.Color]::FromArgb(1, $color.R, $color.G, $color.B)
- $bmp.SetPixel($x, $y, $transparent_color)
- }
- }
- }
- $bmp.Save($_.FullName)
- } finally {
- if ($memstream) { $memstream.Close() }
- if ($bmp) { $bmp.Dispose() }
- }
- }
复制代码 2.把当前目录的圆形图片变矩形图片,"圆形变矩形.ps1.bat"- <#*,:
- @echo off&cd /d "%~dp0"&set "batchfile=%~f0"
- powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- # convert ellipse to rectangle
- Add-Type -AssemblyName System.Drawing
-
- Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object {
- try {
- $_.Name
- $bytes = [IO.File]::ReadAllBytes($_.FullName)
- $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes)
- $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream)
- $width = $bmp.Width
- $height = $bmp.Height
- $a = $width / 2
- $b = $height / 2
- $centerX = $width / 2
- $centerY = $height / 2
- foreach ($x in 0..($width - 1)) {
- foreach ($y in 0..($height - 1)) {
- $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2)
- if ($equation -gt 1) {
- $color = $bmp.GetPixel($x, $y)
- # NOTE:Both System.Drawing and imagemagick will set RGB channel to 0 when alpha channel is 0.
- $transparent_color = [System.Drawing.Color]::FromArgb(255, $color.R, $color.G, $color.B)
- $bmp.SetPixel($x, $y, $transparent_color)
- }
- }
- }
- $bmp.Save($_.FullName)
- } finally {
- if ($memstream) { $memstream.Close() }
- if ($bmp) { $bmp.Dispose() }
- }
- }
复制代码
|