本帖最后由 went 于 2020-2-13 16:25 编辑
- function cutImg {
- param (
- [parameter(Mandatory=$true)]$src,
- $top=0,$bottom=0,$left=0,$rigth=0
- )
- if(Test-Path -LiteralPath $src){$srcFile=[System.IO.FileInfo]::new($src)}else{Write-Host "路径错误";return}
- 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($src) #源图片
- [int]$tagWidth=$srcBmp.Width*(1-$top/100-$bottom/100)
- [int]$tagHeight=$srcBmp.Height*(1-$left/100-$rigth/100)
- $srcRect=[System.Drawing.Rectangle]::new($srcBmp.Width*($left/100),$srcBmp.Height*($top/100),$tagWidth,$tagHeight) #源裁剪矩形
- $tagRect=[System.Drawing.Rectangle]::new(0,0,$tagWidth,$tagHeight) #目标矩形
- $tagBmp=[System.Drawing.Bitmap]::new($tagWidth,$tagHeight) #目标图片
- $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()
- Write-Host ($src+"`t处理完成!")
- }
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- dir -Filter "*.jpg" -File | foreach {cutImg -src ($_.FullName) -top 10 -bottom 10 -left 10 -rigth 10}
- pause
复制代码 保存ps1文件,放到jpg文件目录下右键运行
记得先测试一下 |