标题: [其他] 批处理裁剪图片四边空白部分 [打印本页]
作者: 505250350 时间: 2019-8-24 20:43 标题: 批处理裁剪图片四边空白部分
怎么用批处理裁剪掉png格式的图片四边空白像素,如下图效果。
试过用nconvert.exe,但是图片白色时不行,不知怎么回事。- nconvert -quiet -overwrite -autocrop 0 255 255 255 *.png
复制代码
作者: flashercs 时间: 2019-8-25 08:34
本帖最后由 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
复制代码
作者: 505250350 时间: 2019-8-25 18:37
回复 2# flashercs
谢谢了,我自己又研究了一天,用ImageMagick实现了。- set pic=%MyDocuments%\images\*.png
- ::裁剪掉四边空白像素
- for /f "delims=" %%a in ('dir /a-d/s/b %pic%') do ( tools\convert.exe %%a -quiet -trim %%a )
复制代码
作者: luckcsz 时间: 2019-12-6 00:16
回复 3# 505250350
能否发下全部代码~!
作者: Batcher 时间: 2019-12-6 12:26
回复 4# luckcsz
3楼应该就是完整代码,你需要先安装 ImageMagick
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |