Advertisement
Guest User

im-crop-rotate.cmd

a guest
Mar 28th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 14.62 KB | None | 0 0
  1. :: coding: cp850
  2. @echo off & setlocal
  3.  
  4. set PreviewCommand=Imagine "%%PreviewFile%%"
  5.  
  6. rem USAGE ---------------------------------------
  7. set "ScriptName=%~nx0"& set Commandline=%*& goto :_AFTER_USAGE
  8. :USAGE
  9. echo.
  10. 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]
  11. echo.
  12. echo  -w    overwrite output file ^(default^)
  13. echo  -W    don't overwrite
  14. echo  -r    rotation angle in degrees;  if the angle has "?" at end, the
  15. echo        value will be used to generate and show preview of several
  16. echo        rotations around this value;  after viewer's window is closed,
  17. echo        you may specify another angle value, either with "?" at end
  18. echo        for new preview, or witout it for final processing;
  19. echo        default preview command is specified in the beginning of this
  20. echo        script's file as: set PreviewCommand=Imagine "%%PreviewFile%%"
  21. echo  -bg   color of pixels, appeared after rotation;  aslo sets a default
  22. echo        background color and border color for custom ImageMagick filters
  23. echo        use "?" at end to be able to choose another color after rotation
  24. echo        preview is closed, and for displaying another preview
  25. echo  -grid grid color in angles preview;  ignored if there is no rotation;
  26. echo        use "?" at end to be able to choose another one for new preview
  27. echo  -f1   percent value of colors considered to be equal for trimming
  28. echo        edges before rotation
  29. echo  -f2   percent value of colors considered to be equal for trimming
  30. echo        edges after rotation;  ignored if no rotation is done
  31. echo  -p    pads of original image to keep while trimming ^(e.g. 80x40, 60^)
  32. echo        -f1, -f2 and -p options accepts "?" after actual value, -
  33. echo        in this case a final preview will be shown with further
  34. echo        possibility to enter another value
  35. echo  -fmt  ImageMagick's format options, preceding output file, e.g.
  36. echo        "-quality 80 jpg:"
  37. echo        "-quality 50 -define webp:lossless=true"
  38. echo.
  39. echo  Examples:
  40. echo  %ScriptName% -r 4? -f1 80 -f2 0 scan1.jpg scan1-.jpg
  41. exit /b 1
  42. :_AFTER_USAGE
  43.  
  44.  
  45. rem DEFAULTS ------------------------------------
  46. set Overwrite=
  47. set Angle=0?
  48. set Fuzz1=60
  49. set Fuzz2=30
  50. set Pads=50
  51. set Grid=gray
  52. set Background=white
  53. set OutfileFormatOptions=
  54. set CustomImFilters=
  55. rem set "TempFile=%Temp%\%ScriptName%-%Random%.png"
  56. set "TempFile=%Temp%\crop-%Random:~0,2%.png"
  57.  
  58.  
  59. rem PARSE ARGUMENTS -----------------------------
  60. :LOOP_ARGS
  61. if /i [-w]==[%1] set Overwrite=1& shift & goto :LOOP_ARGS
  62. if /i [-W]==[%1] set Overwrite=& shift & goto :LOOP_ARGS
  63. if /i [-r]==[%1] set "Angle=%~2"& shift & shift & goto :LOOP_ARGS
  64. if /i [-bg]==[%1] set "Background=%~2"& shift & shift & goto :LOOP_ARGS
  65. if /i [-grid]==[%1] set "Grid=%~2"& shift & shift & goto :LOOP_ARGS
  66. if /i [-f1]==[%1] set "Fuzz1=%~2"& shift & shift & goto :LOOP_ARGS
  67. if /i [-f2]==[%1] set "Fuzz2=%~2"& shift & shift & goto :LOOP_ARGS
  68. if /i [-p]==[%1] set "Pads=%~2"& shift & shift & goto :LOOP_ARGS
  69. if /i [-fmt]==[%1] set "OutfileFormatOptions=%~2"& shift & shift & goto :LOOP_ARGS
  70. set "Infile=%~1"& shift
  71. set "Outfile=%~1"& shift
  72. if not defined Outfile goto :USAGE
  73.  
  74. :LOOP_CUSTOM_IM_FILTERS
  75. if "" neq "%~1" set CustomImFilters=%CustomImFilters% %1& shift& goto :LOOP_CUSTOM_IM_FILTERS
  76.  
  77.  
  78. rem CHECK ARGUMENTS -----------------------------
  79. if not defined OutfileFormatOptions goto :_SKIP_OFO
  80. if not ":"=="%OutfileFormatOptions:~-1,1%" set "OutfileFormatOptions=%OutfileFormatOptions% "
  81. :_SKIP_OFO
  82.  
  83. if not exist "%Infile%" (
  84.     echo [Not found] "%Infile%".
  85.     exit /b
  86. )
  87.  
  88. if not defined Overwrite if exist "%Outfile%" (
  89.     echo [Already exists] "%Outfile%".
  90.     exit /b
  91. )
  92.  
  93. where /q "magick.exe" >nul 2>nul
  94. if errorlevel 1 echo [ERROR] ImageMagick's magick.exe is not found.& exit /b 1
  95.  
  96. for /f "usebackq delims=" %%A in (`echo "%Infile%"`) do (
  97.     for /f "usebackq delims=" %%B in (`echo "%Outfile%"`) do (
  98.         if /i "%%~fA"=="%%~fB" (
  99.             echo [ERROR] Output and input files are the same.& exit /b 1
  100.         )
  101.     )
  102. )
  103.  
  104. rem PARSE PARAMETERS AND DECIDE WHAT TO DO
  105. set ShowRotationPreview=
  106. set ShowFinalPreview=
  107. set DoRotation=
  108. set DoCrop1=
  109. set DoCrop2=
  110.  
  111. call :PARSE_VALUE Angle %Angle%
  112. if defined AngleQuestioned set ShowRotationPreview=1
  113. if not "%Angle%" == "0" set DoRotation=1
  114.  
  115. call :PARSE_VALUE Fuzz1 %Fuzz1%
  116. if defined Fuzz1Questioned (
  117.     set DoCrop1=1
  118.     set ShowFinalPreview=1
  119. ) else if %Fuzz1% gtr 0 set DoCrop1=1
  120.  
  121. call :PARSE_VALUE Fuzz2 %Fuzz2%
  122. if defined Fuzz2Questioned (
  123.     set DoCrop2=1
  124.     set ShowFinalPreview=1
  125. ) else if %Fuzz2% gtr 0 set DoCrop2=1
  126.  
  127. call :PARSE_VALUE Pads %Pads%
  128. if defined PadsQuestioned (
  129.     if defined DoCrop2 set ShowFinalPreview=1
  130. )
  131.  
  132. call :PARSE_VALUE Background %Background%
  133. if defined BackgroundQuestioned if not defined ShowRotationPreview set ShowFinalPreview=1
  134.  
  135. call :PARSE_VALUE Grid %Grid%
  136.  
  137. if not defined DoRotation set DoCrop2=
  138. if not defined DoCrop2 if not defined Fuzz2Questioned set ShowFinalPreview=
  139.  
  140. rem PRINT PARAMETERS
  141. echo.
  142. call :ECHO_VAR_MSG "Input:" "%Infile%"
  143. call :ECHO_VAR_MSG "Crop1 fuzz:" "%Fuzz1%" %Fuzz1Questioned%
  144. call :ECHO_VAR_MSG "Crop2 fuzz:" "%Fuzz2%" %Fuzz2Questioned%
  145. call :ECHO_VAR_MSG "Pads:" "%Pads%" %PadsQuestioned%
  146.  
  147. call :ECHO_VAR_MSG "Angle:" "%Angle%" %AngleQuestioned%
  148. call :ECHO_VAR_MSG "Background:" "%Background%" %BackgroundQuestioned%
  149. call :ECHO_VAR_MSG "Grid:" "%Grid%" %GridQuestioned%
  150.  
  151. if not defined ShowRotationPreview goto :CROP1_ROTATE
  152.  
  153.  
  154. rem PREVIEW ROTATION ANGLES ---------------------
  155. :SHOW_ROTATION_PREVIEW
  156.  
  157. echo  ...
  158. call :BEGIN_ARRAY
  159. 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 (
  160.     if /i "NEW_ROW"=="%%A" (call :NEW_ROW) else call :ADD_CELL %%A
  161. )
  162. call :END_ARRAY
  163.  
  164. rem set "PreviewFile=%Temp%\%ScriptName%-%Random%.jpg"
  165. set "PreviewFile=%Temp%\angles-%Random:~0,2%.jpg"
  166. magick.exe ^( "%Infile%" -fuzz %Fuzz1%%% -trim +repage -resize "700x700>" ^) %Array% -delete 0 -gravity undefined -splice 0x10 -append -chop 0x10 -bordercolor %Background% -border 10 "%PreviewFile%"
  167. if errorlevel 1 (
  168.     echo [ERROR] ImageMagick's command has failed.
  169.     del "%PreviewFile%" >nul 2>nul
  170.     exit /b 1
  171. )
  172. for /f "usebackq delims=" %%A in (`echo %PreviewCommand%`) do (
  173.     start "" /wait %%A >nul 2>nul
  174.     if errorlevel 1 (
  175.         echo [ERROR] Failed to run command: %%A
  176.         del "%PreviewFile%" >nul 2>nul
  177.         exit /b 1
  178.     )
  179. )
  180.  
  181. if defined AngleQuestioned call :PARSE_VALUE Angle "%Angle%" 1
  182. if errorlevel 1 exit /b
  183. if "%Angle%" == "0" (set DoRotation=) else set DoRotation=1
  184. if defined AngleQuestioned set DoRotation=1
  185.  
  186. if defined DoRotation (
  187.     if %Fuzz2% gtr 0 set DoCrop2=1
  188.  
  189.     if defined BackgroundQuestioned call :PARSE_VALUE Background "%Background%" 1
  190.     if errorlevel 1 exit /b
  191.  
  192.     if defined AngleQuestioned if defined GridQuestioned call :PARSE_VALUE Grid "%Grid%" 1
  193.     if errorlevel 1 exit /b
  194. )
  195.  
  196. if defined AngleQuestioned (
  197.     set ShowRotationPreview=1
  198.     goto :SHOW_ROTATION_PREVIEW
  199. ) else (
  200.     set ShowRotationPreview=
  201.     if defined DoRotation if defined BackgroundQuestioned set ShowFinalPreview=1
  202. )
  203. rem if defined AngleQuestioned if defined GridQuestioned set _=1
  204.  
  205. del "%PreviewFile%" >nul 2>nul
  206.  
  207.  
  208. rem CROP1 AND ROTATE -----------------------------
  209. :CROP1_ROTATE
  210.  
  211. if defined CustomImFilters (set RestoreColors= -background %Background% -bordercolor %Background%) else set RestoreColors=
  212.  
  213. rem DEFINE FILES AND FORMATS
  214. if defined DoCrop2 (
  215.     set "Outfile_=%TempFile%"
  216.     set "OutfileFormatOptions_=png24:"
  217.     set CustomImFilters_=
  218. ) else (
  219.     set "Outfile_=%Outfile%"
  220.     set "OutfileFormatOptions_=%OutfileFormatOptions%"
  221.     rem set CustomImFilters_=%CustomImFilters%
  222. )
  223.  
  224. rem GET CROP1 BOX
  225. call :GET_IMAGE_DIMENSIONS "%Infile%" %Fuzz1%
  226. echo.
  227. call :ECHO_VAR_MSG "Original:" "%OriginalWidth% x %OriginalHeight%"
  228. if defined DoCrop1 if %Fuzz1% gtr 0 call :ECHO_VAR_MSG "%Fuzz1%%%%%-fuzz cropped:" "%CropWidth% x %CropHeight% + %CropLeft% %CropTop%"
  229.  
  230. if defined DoCrop1 if %Pads:x=% gtr 0 call :ECHO_VAR_MSG "%Pads%px pads kept:" "%CropWidthPadded% x %CropHeightPadded% + %CropLeftPadded% %CropTopPadded%"
  231. if defined DoCrop1 if %Fuzz1% gtr 0 (set Crop1Str= -crop "%CropWidthPadded%x%CropHeightPadded%+%CropLeftPadded%+%CropTopPadded%") else set Crop1Str=
  232. if defined DoRotation (set RotationStr= -background %Background% -rotate %Angle%) else set RotationStr=
  233.  
  234. echo.
  235. echo --^> magick.exe "%Infile%"%Crop1Str%%RotationStr% +repage%RestoreColors%%CustomImFilters_% %OutfileFormatOptions_%"%Outfile_%"
  236. magick.exe "%Infile%"%Crop1Str%%RotationStr% +repage%RestoreColors%%CustomImFilters_% %OutfileFormatOptions_%"%Outfile_%"
  237. if errorlevel 1 (
  238.     echo [ERROR] ImageMagick's command has failed.
  239.     del "%Outfile_%" >nul 2>nul
  240.     exit /b 1
  241. )
  242.  
  243. if not defined DoCrop2 goto :AFTER_PROCESSING
  244.  
  245.  
  246. rem CROP2 ---------------------------------------
  247. :CROP2
  248.  
  249. rem GET CROP2 BOX
  250. call :GET_IMAGE_DIMENSIONS "%Outfile_%" %Fuzz2%
  251.  
  252. echo.
  253. call :ECHO_VAR_MSG "%Angle%?-rotated:" "%OriginalWidth% x %OriginalHeight%"
  254.  
  255. if %Fuzz2% gtr 0 (
  256.     call :ECHO_VAR_MSG "%Fuzz2%%%%%-fuzz cropped:" "%CropWidth% x %CropHeight% + %CropLeft% %CropTop%"
  257.     set Crop2Str= -crop "%CropWidthPadded%x%CropHeightPadded%+%CropLeftPadded%+%CropTopPadded%" +repage
  258. ) else set Crop2Str=
  259.  
  260. if %Pads:x=% gtr 0 call :ECHO_VAR_MSG "%Pads%px pads kept:" "%CropWidthPadded% x %CropHeightPadded% + %CropLeftPadded% %CropTopPadded%"
  261.  
  262. echo.
  263. echo --^> magick.exe "%Outfile_%"%Crop2Str%%RestoreColors%%CustomImFilters% %OutfileFormatOptions%"%Outfile%"
  264. magick.exe "%Outfile_%"%Crop2Str%%RestoreColors%%CustomImFilters% %OutfileFormatOptions%"%Outfile%"
  265. if errorlevel 1 (
  266.     echo [ERROR] ImageMagick's command has failed.
  267.     del "%Outfile_%" >nul 2>nul
  268.     exit /b 1
  269. )
  270. del "%Outfile_%" >nul 2>nul
  271.  
  272.  
  273. rem LAST WORDS, FINAL PREVIEW, REPEAT? ----------
  274. :AFTER_PROCESSING
  275.  
  276. rem LAST WORDS
  277. set _=
  278. if defined DoRotation (
  279.     if not defined DoCrop2 (
  280.         set _=1
  281.     ) else if defined CustomImFilters set _=1
  282. )
  283. if defined _ (
  284.     echo.
  285.     for /f "usebackq tokens=1,2*" %%A in (`magick.exe %Outfile% -format "%%w %%h" info:`) do (
  286.         if defined DoRotation (
  287.             if not defined DoCrop2 (
  288.                 if defined CustomImFilters (
  289.                     call :ECHO_VAR_MSG "%Angle%?-rotated and filtered:" "%%A x %%B"
  290.                 ) else call :ECHO_VAR_MSG "%Angle%?-rotated:" "%%A x %%B"
  291.             ) else if defined CustomImFilters call :ECHO_VAR_MSG "After user filters:" "%%A x %%B"
  292.         )
  293.     )
  294. )
  295.  
  296. if not defined ShowFinalPreview exit /b
  297. echo.
  298.  
  299. rem FINAL PREVIEW
  300. set "PreviewFile=%Outfile%"
  301. for /f "usebackq delims=" %%A in (`echo %PreviewCommand%`) do (
  302.     start "" /wait %%A >nul 2>nul
  303.     if errorlevel 1 (
  304.         echo [ERROR] Failed to run command: %%A
  305.         exit /b 1
  306.     )
  307. )
  308.  
  309. rem REPEAT?
  310. set SameValues=1
  311.  
  312. if defined Fuzz1Questioned (
  313.     call :PARSE_VALUE Fuzz1 "%Fuzz1%" 1
  314.     if errorlevel 1 exit /b
  315.     if not defined SameValue set SameValues=
  316. )
  317. if %Fuzz1% gtr 0 (set DoCrop1=1) else set DoCrop1=
  318.  
  319. if defined DoRotation (
  320.     if defined Fuzz2Questioned (
  321.         call :PARSE_VALUE Fuzz2 "%Fuzz2%" 1
  322.         if errorlevel 1 exit /b
  323.         if not defined SameValue set SameValues=
  324.     )
  325.     if defined BackgroundQuestioned (
  326.         call :PARSE_VALUE Background "%Background%" 1
  327.         if errorlevel 1 exit /b
  328.         if not defined SameValue set SameValues=
  329.     )
  330. )
  331. if defined DoRotation if %Fuzz2% gtr 0 (set DoCrop2=1) else set DoCrop2=
  332.  
  333. if defined PadsQuestioned (
  334.     call :PARSE_VALUE Pads "%Pads%" 1
  335.     if errorlevel 1 exit /b
  336.     if not defined SameValue set SameValues=
  337. )
  338.  
  339. if not defined Fuzz1Questioned if not defined Fuzz2Questioned if not defined BackgroundQuestioned if not defined PadsQuestioned set ShowFinalPreview=
  340. if not defined DoRotation if not defined Fuzz1Questioned if not defined PadsQuestioned set ShowFinalPreview=
  341.  
  342. if not defined SameValues (goto :CROP1_ROTATE) else echo Done.
  343.  
  344.  
  345. exit /b
  346.  
  347.  
  348. rem FUNCTIONS -----------------------------------
  349.  
  350. :ECHO_VAR_MSG
  351.     set "Str=%~1                                                              "
  352.     set "Str=%Str:~0,40%"
  353.     if "" == "%~3" (set _=) else set _=?
  354.     echo %Str%%~2%_%
  355.     exit /b
  356.  
  357. :PARSE_VALUE
  358.     if "" == "%~3" set "Value=%~2"& goto :_SKIP_ASKING
  359.  
  360.     set "Msg=%1 (%~2, '-' to cancel):                                         "
  361.     set "Msg=%Msg:~0,40%"
  362.     set Value=
  363.     set /p Value="%Msg%"
  364.     if "-" == "%Value%" echo     ...cancelled& exit /b 1
  365.     if not defined Value set "Value=%~2"
  366.  
  367.     :_SKIP_ASKING
  368.     set %1Questioned=
  369.     if "?"=="%Value:~-1%" (
  370.         set "%1=%Value:~0,-1%"
  371.         set %1Questioned=1
  372.         rem set AnotherTry=1
  373.     ) else set "%1=%Value%"
  374.  
  375.     if /i "%~2"=="%Value%" (set SameValue=1) else set SameValue=
  376.     exit /b
  377.  
  378. :GET_IMAGE_DIMENSIONS
  379.     if %2 leq 0 for /f "usebackq delims=x tokens=1,2*" %%A in (`magick.exe %1 -format "%%wx%%h" info:`) do (
  380.         set "CropWidth=%%A%"
  381.         set "CropHeight=%%B%"
  382.         set "CropLeft=0"
  383.         set "CropTop=0"
  384.         set "OriginalWidth=%%A"
  385.         set "OriginalHeight=%%B"
  386.         set "CropWidthPadded=%%A"
  387.         set "CropLeftPadded=0"
  388.         set "CropHeightPadded=%%B"
  389.         set "CropTopPadded=0"
  390.         rem set PadX=0
  391.         rem set PadY=0
  392.         exit /b
  393.     )
  394.     for /f "usebackq delims=x+ tokens=1,2,3,4,5,6*" %%A in (`magick.exe %1 -fuzz %~2%% -format "%%@+%%wx%%h" info:`) do (
  395.         set "CropWidth=%%A%"
  396.         set "CropHeight=%%B%"
  397.         set "CropLeft=%%C"
  398.         set "CropTop=%%D%"
  399.         set "OriginalWidth=%%E"
  400.         set "OriginalHeight=%%F"
  401.     )
  402.     set PadY=& for /f "usebackq tokens=1* delims=xX" %%A in ('%Pads%') do set PadX=%%A& set PadY=%%B
  403.     if not defined PadY set PadY=%PadX%
  404.     set /a "CropWidthPadded=CropWidth+2*PadX"
  405.     set /a "CropHeightPadded=CropHeight+2*PadY"
  406.     set /a "CropLeftPadded=%CropLeft%-PadX"
  407.     set /a "CropTopPadded=%CropTop%-PadY"
  408.     if %CropLeftPadded% lss 0 set CropWidthPadded=%OriginalWidth%& set CropLeftPadded=0
  409.     if %CropTopPadded% lss 0 set CropHeightPadded=%OriginalHeight%& set CropTopPadded=0
  410.     exit /b
  411.  
  412. :BEGIN_ARRAY
  413.     set Array="(" -clone 0
  414.     exit /b
  415.  
  416. :ADD_CELL
  417.     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]?" ")"
  418.     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]?" ")"
  419.     exit /b
  420.  
  421. :NEW_ROW
  422.     set Array=%Array% -delete 0 -gravity undefined -splice 10x0 +append -chop 10x0 ")" "(" -clone 0
  423.     exit /b
  424.  
  425. :END_ARRAY
  426.     set Array=%Array% -delete 0 -gravity undefined -splice 10x0 +append -chop 10x0 ")"
  427.     exit /b
  428.  
  429.  
  430. exit /b
  431.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement