本帖最后由 flashercs 于 2019-8-25 10:25 编辑
保存为 xxx.bat ,修改文件路径后使用- <#*,:&cls
- @echo off
- pushd "%~dp0"
- Powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- # 图片路径,可包含通配符*?
- $picsPath = "$PWD\[0-9].png"
- function Get-PictureCrop {
- [CmdletBinding(DefaultParameterSetName = "PathSet")]
- param (
- [Parameter(Mandatory = $true,
- Position = 0,
- ParameterSetName = "PathSet",
- ValueFromPipeline = $true,
- ValueFromPipelineByPropertyName = $true,
- HelpMessage = "Path to one or more locations.")]
- [ValidateNotNullOrEmpty()]
- [string[]]
- $Path,
- [Parameter(Mandatory = $false,
- ParameterSetName = "LiteralPathSet",
- ValueFromPipelineByPropertyName = $true,
- HelpMessage = "Literal path to one or more locations.")]
- [ValidateNotNullOrEmpty()]
- [string[]]
- $LiteralPath,
- [switch]$OverWrite
- )
-
- begin {
- if ($PSBoundParameters.ContainsKey("Debug")) {
- $DebugPreference = "Continue"
- }
- Add-Type -AssemblyName System.Drawing
- function Get-TempFileName {
- param (
- [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
- [string]$LiteralPath
- )
- $o = Get-Item -LiteralPath $LiteralPath | Select-Object -Property DirectoryName, BaseName, Extension
- $n = 0
- while (Join-Path -Path $o.DirectoryName -ChildPath "$($o.BaseName)_$n$($o.Extension)" -OutVariable fileName | Test-Path -LiteralPath { $_ }) {
- ++$n
- }
- $fileName[0]
- }
- }
-
- process {
- $pathToProcess = if ($PSCmdlet.ParameterSetName -eq 'PathSet') {
- Convert-Path -Path $Path
- }
- else {
- Convert-Path -LiteralPath $LiteralPath
- }
- # replace bytes from source to dest
- foreach ($filePath in $pathToProcess) {
- if (Test-Path -LiteralPath $filePath -PathType Container) {
- continue
- }
- try {
- $bitmap = New-Object "System.Drawing.Bitmap" -ArgumentList $filePath
- }
- catch {
- $filePath, $Error[0] | Out-String | Write-Host -ForegroundColor Red
- continue
- }
- Write-Host $filePath -ForegroundColor Green
- $height = $bitmap.Height
- $width = $bitmap.Width
- # top
- $top = $null
- :outer
- for ($y = 0; $y -lt $height; $y++) {
- for ($x = 0; $x -lt $width; $x++) {
- $color = $bitmap.GetPixel($x, $y)
- if ($color.GetBrightness() -lt 0.5) {
- $top = $y
- break outer
- }
- }
- }
- if ($null -eq $top) {
- $bitmap.Dispose()
- continue
- }
- # right
- :outer
- for ($x = $width - 1; $x -ge 0; $x--) {
- for ($y = $height - 1; $y -ge 0; $y--) {
- $color = $bitmap.GetPixel($x, $y)
- if ($color.GetBrightness() -lt 0.5) {
- $right = $x
- break outer
- }
- }
- }
- # bottom
- :outer
- for ($y = $height - 1; $y -ge 0; $y--) {
- for ($x = $width - 1; $x -ge 0; $x--) {
- $color = $bitmap.GetPixel($x, $y)
- if ($color.GetBrightness() -lt 0.5) {
- $bottom = $y
- break outer
- }
- }
- }
- # left
- :outer
- for ($x = 0; $x -lt $width; $x++) {
- for ($y = 0; $y -lt $height; $y++) {
- $color = $bitmap.GetPixel($x, $y)
- if ($color.GetBrightness() -lt 0.5) {
- $left = $x
- break outer
- }
- }
- }
- Write-Debug "$left $top $right $bottom"
- if ($left -eq 0 -and $top -eq 0 -and $right -eq ($width - 1) -and $bottom -eq ($height - 1)) {
- $bitmap.Dispose()
- continue
- }
- # new pic
- $bitmap2 = New-Object "System.Drawing.Bitmap" -ArgumentList ($right - $left + 1), ($bottom - $top + 1), $bitmap.PixelFormat
- $bitmap2.SetResolution($bitmap.HorizontalResolution, $bitmap.VerticalResolution)
- $bitmap2 | Format-List * | Out-String | Write-Debug
- for ($x = $left; $x -le $right; $x++) {
- for ($y = $top; $y -le $bottom; $y++) {
- $bitmap2.SetPixel($x - $left, $y - $top, $bitmap.GetPixel($x, $y))
- }
- }
- $imageFormat = $bitmap.RawFormat
- $bitmap.Dispose()
- if ($OverWrite) {
- $bitmap2.Save($filePath, $imageFormat)
- }
- else {
- $bitmap2.Save((Get-TempFileName $filePath), $imageFormat)
- }
- $bitmap2.Dispose()
- }
- }
-
- end {
- }
- }
- # Get-PictureCrop -Path $picsPath
- # 添加参数 OverWrite 为覆盖源文件
- $picsPath | Get-PictureCrop -OverWrite
复制代码
|