@echo off & chcp 936
setlocal enabledelayedexpansion
set "IM=D:\桌面\ImageMagick-7.0.3-0-portable-Q16-x86"
for %%N in (identify convert) do for %%a in ("%IM%\%%N.exe") do set "%%N=%%~sa"
set "imgTypes=png bmp jpg"
set /a "cntPic=sumPic=0"
for %%x in (%imgTypes%) do for /f "delims=" %%i in ('2^>nul dir /a-d /b /s "%~dp0*.%%x" ^| find /v /c ""') do ^
set /a "sumPic += %%i"
for %%x in (%imgTypes%) do for /f "delims=" %%i in ('2^>nul dir /a-d /b /s "%~dp0*.%%x"') do (
for /f "tokens=1-2" %%b in ('%identify% -format "%%[fx:w] %%[fx:h]" "%%~i"') do (
set /a "cntPic+=1, 原宽=%%b,原高=%%c"
title 已完成 !cntPic!/!sumPic! %%~i %~nx0 & rem 在标题栏上显示处理进度
REM echo;%%i
REM echo;原尺寸:!原宽!,!原高!
REM 适应边缘空白有噪点的方式, -blur 参数控制噪点大小过滤, -fuzz 控制色模糊过滤
for /f delims^= %%a in ('%convert% "%%~i" -virtual-pixel edge -blur 0x4 -fuzz 5%% -trim ^
-format '%%[fx:w+20]x%%[fx:h+20]+%%[fx:page.x-10]+%%[fx:page.y-10]' ^
info:') do (
echo;%%~i: %%a
set "cropParam=%%a"
%convert% "%%~i" -crop !cropParam:'=! +repage "%%~i"
)
REM 以下是对清晰图片 trim 的方式, 空白边缘无噪点
REM 剪上部 和 左部 空白
REM %convert% "%%~i" -gravity South ^
REM -background white -splice 0x1 -background black -splice 0x1 ^
REM -trim +repage -chop 0x1 ^
REM -gravity East ^
REM -background white -splice 1x0 -background black -splice 1x0 ^
REM -trim +repage -chop 1x0 ^
REM "%%~i"
REM 一次性裁剪掉四边空白
REM %convert% "%%~i" -gravity SouthEast ^
REM -background white -splice 0x0 -background black -splice 0x0 ^
REM -fuzz 5%% -trim +repage -chop 0x0 "%%~i"
REM 剪下部和右部空白
REM %convert% "%%~i" -gravity North ^
REM -background white -splice 0x1 -background black -splice 0x1 ^
REM -trim +repage -chop 0x1 ^
REM -gravity West ^
REM -background white -splice 1x0 -background black -splice 1x0 ^
REM -trim +repage -chop 1x0 ^
REM "%%~i"
)
)
echo;!cntPic!/!sumPic! 已完成处理
pause
exit
REM 一次性裁剪掉四边空白
REM %convert% "d:\test.png" -gravity SouthEast ^
REM -background white -splice 0x0 -background black -splice 0x0 ^
REM -trim +repage -chop 0x0 "d:\test.png"
REM 剪上部空白
%convert% "D:\test.png" -gravity South ^
-background white -splice 0x1 -background black -splice 0x1 ^
-trim +repage -chop 0x1 "d:\trim_South.png"
REM 剪左部空白
%convert% "d:\trim_South.png" -gravity East ^
-background white -splice 1x0 -background black -splice 1x0 ^
-trim +repage -chop 1x0 "d:\trim_SouthEast.png"