[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 went 于 2020-2-13 16:25 编辑
  1. function cutImg {
  2.     param (
  3.         [parameter(Mandatory=$true)]$src,
  4.         $top=0,$bottom=0,$left=0,$rigth=0
  5.     )
  6.     if(Test-Path -LiteralPath $src){$srcFile=[System.IO.FileInfo]::new($src)}else{Write-Host "路径错误";return}
  7.     if(($top -lt 0) -or ($bottom -lt 0) -or ($left -lt 0) -or ($rigth -lt 0)){Write-Host "参数小于零";return}
  8.     if(($top+$bottom) -ge 100){Write-Host "高为零";return}
  9.     if(($left+$right) -ge 100){Write-Host "款为零";return}
  10.     $srcBmp=[System.Drawing.Bitmap]::FromFile($src) #源图片
  11.     [int]$tagWidth=$srcBmp.Width*(1-$top/100-$bottom/100)
  12.     [int]$tagHeight=$srcBmp.Height*(1-$left/100-$rigth/100)
  13.     $srcRect=[System.Drawing.Rectangle]::new($srcBmp.Width*($left/100),$srcBmp.Height*($top/100),$tagWidth,$tagHeight) #源裁剪矩形
  14.     $tagRect=[System.Drawing.Rectangle]::new(0,0,$tagWidth,$tagHeight) #目标矩形
  15.     $tagBmp=[System.Drawing.Bitmap]::new($tagWidth,$tagHeight) #目标图片
  16.     $g=[System.Drawing.Graphics]::FromImage($tagBmp) #画笔
  17.     $g.DrawImage($srcBmp,$tagRect,$srcRect,[System.Drawing.GraphicsUnit]::Pixel)
  18.     $tagBmp.Save(($srcFile.DirectoryName+"\"+$srcFile.BaseName+"_cut"+$srcFile.Extension),[System.Drawing.Imaging.ImageFormat]::Jpeg)
  19.     $srcBmp.Dispose()
  20.     $tagBmp.Dispose()
  21.     Write-Host ($src+"`t处理完成!")
  22. }
  23. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  24. dir -Filter "*.jpg" -File | foreach {cutImg -src ($_.FullName) -top 10 -bottom 10 -left 10 -rigth 10}
  25. pause
复制代码
保存ps1文件,放到jpg文件目录下右键运行
记得先测试一下

TOP

回复 3# crownking1983


    你电脑上有powershell吗

TOP

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

TOP

回复 7# crownking1983


    编辑,另存为ansi编码,试试

TOP

本帖最后由 went 于 2020-2-14 01:12 编辑

回复 9# crownking1983


    改了点,你运行后截图看看提示什么
可能你powershell版本太低了

TOP

回复 13# crownking1983


    给你改成支持2.0的了,记得用ansi保存

TOP

回复 18# flashercs


    用过其他格式,发现生成文件太大,翻了几倍

TOP

返回列表