Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: coding: cp850
- @echo off & setlocal
- set PreviewCommand=Imagine "%%PreviewFile%%"
- rem USAGE ---------------------------------------
- set "ScriptName=%~nx0"& set Commandline=%*& goto :_AFTER_USAGE
- :USAGE
- echo.
- echo Usage: "%ScriptName%" [-w^|-W] [-r ^<angle^>=0?] [-bg ^<background-color^>=white] [-grid ^<grid-color^>=gray] [-f1 ^<fuzz1^>=60] [-f2 ^<fuzz2^>=30] [-p ^<pads^>=50x50] [-fmt ^<imagemagick-output-format-options^>] input-image output-image [custom-imagemagick-filters]
- echo.
- echo -w overwrite output file ^(default^)
- echo -W don't overwrite
- echo -r rotation angle in degrees; if the angle has "?" at end, the
- echo value will be used to generate and show preview of several
- echo rotations around this value; after viewer's window is closed,
- echo you may specify another angle value, either with "?" at end
- echo for new preview, or witout it for final processing;
- echo default preview command is specified in the beginning of this
- echo script's file as: set PreviewCommand=Imagine "%%PreviewFile%%"
- echo -bg color of pixels, appeared after rotation; aslo sets a default
- echo background color and border color for custom ImageMagick filters
- echo use "?" at end to be able to choose another color after rotation
- echo preview is closed, and for displaying another preview
- echo -grid grid color in angles preview; ignored if there is no rotation;
- echo use "?" at end to be able to choose another one for new preview
- echo -f1 percent value of colors considered to be equal for trimming
- echo edges before rotation
- echo -f2 percent value of colors considered to be equal for trimming
- echo edges after rotation; ignored if no rotation is done
- echo -p pads of original image to keep while trimming ^(e.g. 80x40, 60^)
- echo -f1, -f2 and -p options accepts "?" after actual value, -
- echo in this case a final preview will be shown with further
- echo possibility to enter another value
- echo -fmt ImageMagick's format options, preceding output file, e.g.
- echo "-quality 80 jpg:"
- echo "-quality 50 -define webp:lossless=true"
- echo.
- echo Examples:
- echo %ScriptName% -r 4? -f1 80 -f2 0 scan1.jpg scan1-.jpg
- exit /b 1
- :_AFTER_USAGE
- rem DEFAULTS ------------------------------------
- set Overwrite=
- set Angle=0?
- set Fuzz1=60
- set Fuzz2=30
- set Pads=50
- set Grid=gray
- set Background=white
- set OutfileFormatOptions=
- set CustomImFilters=
- rem set "TempFile=%Temp%\%ScriptName%-%Random%.png"
- set "TempFile=%Temp%\crop-%Random:~0,2%.png"
- rem PARSE ARGUMENTS -----------------------------
- :LOOP_ARGS
- if /i [-w]==[%1] set Overwrite=1& shift & goto :LOOP_ARGS
- if /i [-W]==[%1] set Overwrite=& shift & goto :LOOP_ARGS
- if /i [-r]==[%1] set "Angle=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-bg]==[%1] set "Background=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-grid]==[%1] set "Grid=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-f1]==[%1] set "Fuzz1=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-f2]==[%1] set "Fuzz2=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-p]==[%1] set "Pads=%~2"& shift & shift & goto :LOOP_ARGS
- if /i [-fmt]==[%1] set "OutfileFormatOptions=%~2"& shift & shift & goto :LOOP_ARGS
- set "Infile=%~1"& shift
- set "Outfile=%~1"& shift
- if not defined Outfile goto :USAGE
- :LOOP_CUSTOM_IM_FILTERS
- if "" neq "%~1" set CustomImFilters=%CustomImFilters% %1& shift& goto :LOOP_CUSTOM_IM_FILTERS
- rem CHECK ARGUMENTS -----------------------------
- if not defined OutfileFormatOptions goto :_SKIP_OFO
- if not ":"=="%OutfileFormatOptions:~-1,1%" set "OutfileFormatOptions=%OutfileFormatOptions% "
- :_SKIP_OFO
- if not exist "%Infile%" (
- echo [Not found] "%Infile%".
- exit /b
- )
- if not defined Overwrite if exist "%Outfile%" (
- echo [Already exists] "%Outfile%".
- exit /b
- )
- where /q "magick.exe" >nul 2>nul
- if errorlevel 1 echo [ERROR] ImageMagick's magick.exe is not found.& exit /b 1
- for /f "usebackq delims=" %%A in (`echo "%Infile%"`) do (
- for /f "usebackq delims=" %%B in (`echo "%Outfile%"`) do (
- if /i "%%~fA"=="%%~fB" (
- echo [ERROR] Output and input files are the same.& exit /b 1
- )
- )
- )
- rem PARSE PARAMETERS AND DECIDE WHAT TO DO
- set ShowRotationPreview=
- set ShowFinalPreview=
- set DoRotation=
- set DoCrop1=
- set DoCrop2=
- call :PARSE_VALUE Angle %Angle%
- if defined AngleQuestioned set ShowRotationPreview=1
- if not "%Angle%" == "0" set DoRotation=1
- call :PARSE_VALUE Fuzz1 %Fuzz1%
- if defined Fuzz1Questioned (
- set DoCrop1=1
- set ShowFinalPreview=1
- ) else if %Fuzz1% gtr 0 set DoCrop1=1
- call :PARSE_VALUE Fuzz2 %Fuzz2%
- if defined Fuzz2Questioned (
- set DoCrop2=1
- set ShowFinalPreview=1
- ) else if %Fuzz2% gtr 0 set DoCrop2=1
- call :PARSE_VALUE Pads %Pads%
- if defined PadsQuestioned (
- if defined DoCrop2 set ShowFinalPreview=1
- )
- call :PARSE_VALUE Background %Background%
- if defined BackgroundQuestioned if not defined ShowRotationPreview set ShowFinalPreview=1
- call :PARSE_VALUE Grid %Grid%
- if not defined DoRotation set DoCrop2=
- if not defined DoCrop2 if not defined Fuzz2Questioned set ShowFinalPreview=
- rem PRINT PARAMETERS
- echo.
- call :ECHO_VAR_MSG "Input:" "%Infile%"
- call :ECHO_VAR_MSG "Crop1 fuzz:" "%Fuzz1%" %Fuzz1Questioned%
- call :ECHO_VAR_MSG "Crop2 fuzz:" "%Fuzz2%" %Fuzz2Questioned%
- call :ECHO_VAR_MSG "Pads:" "%Pads%" %PadsQuestioned%
- call :ECHO_VAR_MSG "Angle:" "%Angle%" %AngleQuestioned%
- call :ECHO_VAR_MSG "Background:" "%Background%" %BackgroundQuestioned%
- call :ECHO_VAR_MSG "Grid:" "%Grid%" %GridQuestioned%
- if not defined ShowRotationPreview goto :CROP1_ROTATE
- rem PREVIEW ROTATION ANGLES ---------------------
- :SHOW_ROTATION_PREVIEW
- echo ...
- call :BEGIN_ARRAY
- for %%A in (%Angle%-2 %Angle%-1.5 %Angle%-1 %Angle%-0.5 %Angle% NEW_ROW %Angle%+0.5 %Angle%+1 %Angle%+1.5 %Angle%+2 %Angle%+2.5) do (
- if /i "NEW_ROW"=="%%A" (call :NEW_ROW) else call :ADD_CELL %%A
- )
- call :END_ARRAY
- rem set "PreviewFile=%Temp%\%ScriptName%-%Random%.jpg"
- set "PreviewFile=%Temp%\angles-%Random:~0,2%.jpg"
- magick.exe ^( "%Infile%" -fuzz %Fuzz1%%% -trim +repage -resize "700x700>" ^) %Array% -delete 0 -gravity undefined -splice 0x10 -append -chop 0x10 -bordercolor %Background% -border 10 "%PreviewFile%"
- if errorlevel 1 (
- echo [ERROR] ImageMagick's command has failed.
- del "%PreviewFile%" >nul 2>nul
- exit /b 1
- )
- for /f "usebackq delims=" %%A in (`echo %PreviewCommand%`) do (
- start "" /wait %%A >nul 2>nul
- if errorlevel 1 (
- echo [ERROR] Failed to run command: %%A
- del "%PreviewFile%" >nul 2>nul
- exit /b 1
- )
- )
- if defined AngleQuestioned call :PARSE_VALUE Angle "%Angle%" 1
- if errorlevel 1 exit /b
- if "%Angle%" == "0" (set DoRotation=) else set DoRotation=1
- if defined AngleQuestioned set DoRotation=1
- if defined DoRotation (
- if %Fuzz2% gtr 0 set DoCrop2=1
- if defined BackgroundQuestioned call :PARSE_VALUE Background "%Background%" 1
- if errorlevel 1 exit /b
- if defined AngleQuestioned if defined GridQuestioned call :PARSE_VALUE Grid "%Grid%" 1
- if errorlevel 1 exit /b
- )
- if defined AngleQuestioned (
- set ShowRotationPreview=1
- goto :SHOW_ROTATION_PREVIEW
- ) else (
- set ShowRotationPreview=
- if defined DoRotation if defined BackgroundQuestioned set ShowFinalPreview=1
- )
- rem if defined AngleQuestioned if defined GridQuestioned set _=1
- del "%PreviewFile%" >nul 2>nul
- rem CROP1 AND ROTATE -----------------------------
- :CROP1_ROTATE
- if defined CustomImFilters (set RestoreColors= -background %Background% -bordercolor %Background%) else set RestoreColors=
- rem DEFINE FILES AND FORMATS
- if defined DoCrop2 (
- set "Outfile_=%TempFile%"
- set "OutfileFormatOptions_=png24:"
- set CustomImFilters_=
- ) else (
- set "Outfile_=%Outfile%"
- set "OutfileFormatOptions_=%OutfileFormatOptions%"
- rem set CustomImFilters_=%CustomImFilters%
- )
- rem GET CROP1 BOX
- call :GET_IMAGE_DIMENSIONS "%Infile%" %Fuzz1%
- echo.
- call :ECHO_VAR_MSG "Original:" "%OriginalWidth% x %OriginalHeight%"
- if defined DoCrop1 if %Fuzz1% gtr 0 call :ECHO_VAR_MSG "%Fuzz1%%%%%-fuzz cropped:" "%CropWidth% x %CropHeight% + %CropLeft% %CropTop%"
- if defined DoCrop1 if %Pads:x=% gtr 0 call :ECHO_VAR_MSG "%Pads%px pads kept:" "%CropWidthPadded% x %CropHeightPadded% + %CropLeftPadded% %CropTopPadded%"
- if defined DoCrop1 if %Fuzz1% gtr 0 (set Crop1Str= -crop "%CropWidthPadded%x%CropHeightPadded%+%CropLeftPadded%+%CropTopPadded%") else set Crop1Str=
- if defined DoRotation (set RotationStr= -background %Background% -rotate %Angle%) else set RotationStr=
- echo.
- echo --^> magick.exe "%Infile%"%Crop1Str%%RotationStr% +repage%RestoreColors%%CustomImFilters_% %OutfileFormatOptions_%"%Outfile_%"
- magick.exe "%Infile%"%Crop1Str%%RotationStr% +repage%RestoreColors%%CustomImFilters_% %OutfileFormatOptions_%"%Outfile_%"
- if errorlevel 1 (
- echo [ERROR] ImageMagick's command has failed.
- del "%Outfile_%" >nul 2>nul
- exit /b 1
- )
- if not defined DoCrop2 goto :AFTER_PROCESSING
- rem CROP2 ---------------------------------------
- :CROP2
- rem GET CROP2 BOX
- call :GET_IMAGE_DIMENSIONS "%Outfile_%" %Fuzz2%
- echo.
- call :ECHO_VAR_MSG "%Angle%?-rotated:" "%OriginalWidth% x %OriginalHeight%"
- if %Fuzz2% gtr 0 (
- call :ECHO_VAR_MSG "%Fuzz2%%%%%-fuzz cropped:" "%CropWidth% x %CropHeight% + %CropLeft% %CropTop%"
- set Crop2Str= -crop "%CropWidthPadded%x%CropHeightPadded%+%CropLeftPadded%+%CropTopPadded%" +repage
- ) else set Crop2Str=
- if %Pads:x=% gtr 0 call :ECHO_VAR_MSG "%Pads%px pads kept:" "%CropWidthPadded% x %CropHeightPadded% + %CropLeftPadded% %CropTopPadded%"
- echo.
- echo --^> magick.exe "%Outfile_%"%Crop2Str%%RestoreColors%%CustomImFilters% %OutfileFormatOptions%"%Outfile%"
- magick.exe "%Outfile_%"%Crop2Str%%RestoreColors%%CustomImFilters% %OutfileFormatOptions%"%Outfile%"
- if errorlevel 1 (
- echo [ERROR] ImageMagick's command has failed.
- del "%Outfile_%" >nul 2>nul
- exit /b 1
- )
- del "%Outfile_%" >nul 2>nul
- rem LAST WORDS, FINAL PREVIEW, REPEAT? ----------
- :AFTER_PROCESSING
- rem LAST WORDS
- set _=
- if defined DoRotation (
- if not defined DoCrop2 (
- set _=1
- ) else if defined CustomImFilters set _=1
- )
- if defined _ (
- echo.
- for /f "usebackq tokens=1,2*" %%A in (`magick.exe %Outfile% -format "%%w %%h" info:`) do (
- if defined DoRotation (
- if not defined DoCrop2 (
- if defined CustomImFilters (
- call :ECHO_VAR_MSG "%Angle%?-rotated and filtered:" "%%A x %%B"
- ) else call :ECHO_VAR_MSG "%Angle%?-rotated:" "%%A x %%B"
- ) else if defined CustomImFilters call :ECHO_VAR_MSG "After user filters:" "%%A x %%B"
- )
- )
- )
- if not defined ShowFinalPreview exit /b
- echo.
- rem FINAL PREVIEW
- set "PreviewFile=%Outfile%"
- for /f "usebackq delims=" %%A in (`echo %PreviewCommand%`) do (
- start "" /wait %%A >nul 2>nul
- if errorlevel 1 (
- echo [ERROR] Failed to run command: %%A
- exit /b 1
- )
- )
- rem REPEAT?
- set SameValues=1
- if defined Fuzz1Questioned (
- call :PARSE_VALUE Fuzz1 "%Fuzz1%" 1
- if errorlevel 1 exit /b
- if not defined SameValue set SameValues=
- )
- if %Fuzz1% gtr 0 (set DoCrop1=1) else set DoCrop1=
- if defined DoRotation (
- if defined Fuzz2Questioned (
- call :PARSE_VALUE Fuzz2 "%Fuzz2%" 1
- if errorlevel 1 exit /b
- if not defined SameValue set SameValues=
- )
- if defined BackgroundQuestioned (
- call :PARSE_VALUE Background "%Background%" 1
- if errorlevel 1 exit /b
- if not defined SameValue set SameValues=
- )
- )
- if defined DoRotation if %Fuzz2% gtr 0 (set DoCrop2=1) else set DoCrop2=
- if defined PadsQuestioned (
- call :PARSE_VALUE Pads "%Pads%" 1
- if errorlevel 1 exit /b
- if not defined SameValue set SameValues=
- )
- if not defined Fuzz1Questioned if not defined Fuzz2Questioned if not defined BackgroundQuestioned if not defined PadsQuestioned set ShowFinalPreview=
- if not defined DoRotation if not defined Fuzz1Questioned if not defined PadsQuestioned set ShowFinalPreview=
- if not defined SameValues (goto :CROP1_ROTATE) else echo Done.
- exit /b
- rem FUNCTIONS -----------------------------------
- :ECHO_VAR_MSG
- set "Str=%~1 "
- set "Str=%Str:~0,40%"
- if "" == "%~3" (set _=) else set _=?
- echo %Str%%~2%_%
- exit /b
- :PARSE_VALUE
- if "" == "%~3" set "Value=%~2"& goto :_SKIP_ASKING
- set "Msg=%1 (%~2, '-' to cancel): "
- set "Msg=%Msg:~0,40%"
- set Value=
- set /p Value="%Msg%"
- if "-" == "%Value%" echo ...cancelled& exit /b 1
- if not defined Value set "Value=%~2"
- :_SKIP_ASKING
- set %1Questioned=
- if "?"=="%Value:~-1%" (
- set "%1=%Value:~0,-1%"
- set %1Questioned=1
- rem set AnotherTry=1
- ) else set "%1=%Value%"
- if /i "%~2"=="%Value%" (set SameValue=1) else set SameValue=
- exit /b
- :GET_IMAGE_DIMENSIONS
- if %2 leq 0 for /f "usebackq delims=x tokens=1,2*" %%A in (`magick.exe %1 -format "%%wx%%h" info:`) do (
- set "CropWidth=%%A%"
- set "CropHeight=%%B%"
- set "CropLeft=0"
- set "CropTop=0"
- set "OriginalWidth=%%A"
- set "OriginalHeight=%%B"
- set "CropWidthPadded=%%A"
- set "CropLeftPadded=0"
- set "CropHeightPadded=%%B"
- set "CropTopPadded=0"
- rem set PadX=0
- rem set PadY=0
- exit /b
- )
- for /f "usebackq delims=x+ tokens=1,2,3,4,5,6*" %%A in (`magick.exe %1 -fuzz %~2%% -format "%%@+%%wx%%h" info:`) do (
- set "CropWidth=%%A%"
- set "CropHeight=%%B%"
- set "CropLeft=%%C"
- set "CropTop=%%D%"
- set "OriginalWidth=%%E"
- set "OriginalHeight=%%F"
- )
- set PadY=& for /f "usebackq tokens=1* delims=xX" %%A in ('%Pads%') do set PadX=%%A& set PadY=%%B
- if not defined PadY set PadY=%PadX%
- set /a "CropWidthPadded=CropWidth+2*PadX"
- set /a "CropHeightPadded=CropHeight+2*PadY"
- set /a "CropLeftPadded=%CropLeft%-PadX"
- set /a "CropTopPadded=%CropTop%-PadY"
- if %CropLeftPadded% lss 0 set CropWidthPadded=%OriginalWidth%& set CropLeftPadded=0
- if %CropTopPadded% lss 0 set CropHeightPadded=%OriginalHeight%& set CropTopPadded=0
- exit /b
- :BEGIN_ARRAY
- set Array="(" -clone 0
- exit /b
- :ADD_CELL
- rem set Array=%Array% "(" -clone 0 -background white -rotate "%%[fx:%1]" +repage -bordercolor black -border 1 -fill white -undercolor gray -gravity center -pointsize 50 -annotate +0+0 "%%[fx:%1]?" ")"
- set Array=%Array% "(" -clone 0 -background %Background% -rotate "%%[fx:%1]" -fuzz %Fuzz2%%% -trim +repage -bordercolor %Background% -border 10 -gravity undefined -crop 5x5@ -chop 1x1 -background %Grid% -splice 1x1 -background %Background% -flatten -shave 1x1 -bordercolor %Grid% -border 1x1 -fill white -undercolor gray -gravity center -pointsize 50 -annotate +0+0 "%%[fx:%1]?" ")"
- exit /b
- :NEW_ROW
- set Array=%Array% -delete 0 -gravity undefined -splice 10x0 +append -chop 10x0 ")" "(" -clone 0
- exit /b
- :END_ARRAY
- set Array=%Array% -delete 0 -gravity undefined -splice 10x0 +append -chop 10x0 ")"
- exit /b
- exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement