本帖最后由 went 于 2020-2-14 14:01 编辑
- @echo off
- powershell -version 2.0 -c ("&{Get-Content '%~0' | Select-Object -Skip 3 | Out-String | Invoke-Expression}")
- pause&exit
- function cutImg {
- param (
- [parameter(Mandatory=$true)]$srcFile, #源图片
- $top=0,$bottom=0,$left=0,$rigth=0 #裁剪百分比: 上 下 左 右
- )
- if(($top -lt 0) -or ($bottom -lt 0) -or ($left -lt 0) -or ($rigth -lt 0)){Write-Host "参数小于零";return}
- if(($top+$bottom) -ge 100){Write-Host "高为零";return}
- if(($left+$right) -ge 100){Write-Host "款为零";return}
- $srcBmp=[System.Drawing.Bitmap]::FromFile($srcFile.FullName) #源图片
- [int]$tagWidth=$srcBmp.Width*(1-$left/100-$rigth/100)
- [int]$tagHeight=$srcBmp.Height*(1-$top/100-$bottom/100)
- $srcRect=[System.Drawing.Rectangle]::FromLTRB($srcBmp.Width*($left/100),$srcBmp.Height*($top/100),$tagWidth+$srcBmp.Width*($left/100),$tagHeight+$srcBmp.Height*($top/100)) #源裁剪矩形
- $tagRect=[System.Drawing.Rectangle]::FromLTRB(0,0,$tagWidth,$tagHeight) #目标矩形
- $tagBmp=New-Object "System.Drawing.Bitmap" -ArgumentList @($tagWidth,$tagHeight) #目标图片
- [System.Drawing.Graphics]$g=[System.Drawing.Graphics]::FromImage($tagBmp) #画笔
- $g.DrawImage($srcBmp,$tagRect,$srcRect,[System.Drawing.GraphicsUnit]::Pixel)
- $tagBmp.Save(($srcFile.DirectoryName+"\"+$srcFile.BaseName+"_cut"+$srcFile.Extension),[System.Drawing.Imaging.ImageFormat]::Jpeg)
- $srcBmp.Dispose()
- $tagBmp.Dispose()
- [System.GC]::Collect(0,[System.GCCollectionMode]::Forced) #释放资源
- Write-Host ($srcFile.FullName+"`t处理完成!")
- Start-Sleep -Milliseconds 10
- }
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- dir -Path . -Filter "*.jpg" -Recurse | foreach {cutImg -srcFile $_ -top 10 -bottom 10 -left 10 -rigth 10}
复制代码 改了下,存为bat文件运行,生成的图片在图片文件夹下,名称有个cut |