Skip to content

Instantly share code, notes, and snippets.

@pr3sidentspence
Last active December 3, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pr3sidentspence/0fd991ec794d1d36c6c96a3853e84015 to your computer and use it in GitHub Desktop.
Save pr3sidentspence/0fd991ec794d1d36c6c96a3853e84015 to your computer and use it in GitHub Desktop.
chop.bat uses ImageMagick to cut all .bmp files in the current folder into three .BMP files. Usage: chop.bat optional:([topCut1] [bottomCut1] [topCut2] [bottomCut2] [topCut2] [bottomCut2])
ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
REM chop.bat uses ImageMagick to cut all .bmp files in the current folder
REM into three .BMP files.
REM
REM Usage:
REM chop.bat optional:([topCut1] [bottomCut1] [topCut2] [bottomCut2] [topCut2] [bottomCut2])
REM
REM If no arguments are provided chop.bat will use the following:
SET topCut1=0
SET bottomCut1=4550
SET topCut2=2350
SET bottomCut2=2275
SET topCut3=4650
SET bottomCut3=0
REM Begin init ----------------------------------------------
SET function=init
SET "fmtString=[4][11][11][-17][1][70] %DATE% %TIME%"
CALL :db %function% "started "
IF NOT "%~1" == "" (SET topCut1=%1)
IF NOT "%~2" == "" (SET bottomCut1=%2)
IF NOT "%~3" == "" (SET topCut2=%3)
IF NOT "%~4" == "" (SET bottomCut2=%4)
IF NOT "%~5" == "" (SET topCut3=%5)
IF NOT "%~6" == "" (SET bottomCut3=%6)
SET "inFile="
SET "topFile="
SET "middleFile="
SET "bottomFile="
CMD /C EXIT 0
CALL :db %function% "chopping top image at %topCut1% from top and %bottomCut1% from the bottom
CALL :db %function% "chopping middle image at %topCut2% from top and %bottomCut2% from the bottom
CALL :db %function% "chopping bottom image at %topCut3% from top and %bottomCut3% from the bottom
CALL :testFolder
REM Set up temp and output folders
IF NOT EXIST temp\ (
CALL :db %function% "creating Temp folder."
MD temp
) ELSE (
CALL :db %function% "temp folder already exists."
)
IF NOT EXIST output\ (
CALL :db %function% "Creating ouput folder."
MD output
) ELSE (
CALL :db %function% "output folder already exists."
)
SET "function="
GOTO :main
GOTO :eof
REM End init ------------------------------------------------
REM Begin :main -------------------------------------------------
:main
SET function=main
SETLOCAL
CALL :db %function% "started. "
FOR %%a IN (*.bmp) DO (
CALL :batchDumb %%a %function%
CALL :db %function% "!topFile! chopping at %topCut1% from top and %bottomCut1% from the bottom
MAGICK !inFile! -chop 0x%topCut1% -gravity South -chop 0x%bottomCut1% .\output\!topFile!
CALL :db %function% "!middleFile! chopping at %topCut2% from top and %bottomCut2% from the bottom
MAGICK !inFile! -chop 0x%topCut2% -gravity South -chop 0x%bottomCut2% .\output\!middleFile!
CALL :db %function% "!bottomFile! chopping at %topCut3% from top and %bottomCut3% from the bottom
MAGICK !inFile! -chop 0x%topCut3% -gravity South -chop 0x%bottomCut3% .\output\!bottomFile!
)
CALL :db %function% "ended successfully"
ENDLOCAL
EXIT 0
GOTO :eof
REM End :main -------------------------------------------------
REM Begin :db ----------------------------------------------
:db
CALL :format %fmtString% %1:%ERRORLEVEL% " " %2
EXIT /B %ERRORLEVEL%
REM Begin :db ----------------------------------------------
REM Begin :format ----------------------------------------------
:format fmt str1 str2 ... -- outputs columns of strings right or left aligned
:: -- fmt [in] - format string specifying column width and alignment, i.e. "[-10][10][10]"
:$created 20060101 :$changed 20091130 :$categories Echo
:$source https://www.dostips.com
SETLOCAL
set "fmt=%~1"
set "line="
set "spac= "
set "i=1"
for /f "tokens=1,2 delims=[" %%a in ('"echo..%fmt:]=&echo..%"') do (
set /a i+=1
call call set "subst=%%%%~%%i%%%spac%%%%%~%%i%%"
if %%b0 GEQ 0 (
call set "subst=%%subst:~0,%%b%%"
) ELSE (
call set "subst=%%subst:~%%b%%"
)
call set "const=%%a"
call set "line=%%line%%%%const:~1%%%%subst%%"
)
echo.%line%
EXIT /B %ERRORLEVEL%
REM Begin :format ----------------------------------------------
REM Begin :batchDumb ----------------------------------------------
:batchDumb
SET function=batchDumb
CALL :db %function% "started. "
SET inFile=%1
CALL :db %function% "Set inFile to %inFile%."
SET "topFile=%inFile:~0,-4%-a.jpg"
CALL :db %function% "Set topFile to %topFile%"
SET "middleFile=%inFile:~0,-4%-b.jpg"
CALL :db %function% "Set middleFile to %middleFile%"
SET "bottomFile=%inFile:~0,-4%-c.jpg"
CALL :db %function% "Set bottomFile to %bottomFile%"
SET function=%2
EXIT /B 0
REM Begin :batchDumb ----------------------------------------------
REM Begin :testFolder ----------------------------------------------
:testFolder
SETLOCAL
SET function=testFolder
CALL :db %function% "started. Testing for .bmps."
IF NOT EXIST *.bmp (
CALL :db %function% "Folder %CD% contains no bitmap (.bmp) files. Exiting batch."
EXIT 1
GOTO :eof
ECHO No one should see this. Ever!
)
ENDLOCAL
EXIT /B 0
REM End :testFolder ----------------------------------------------
REM Begin :saveOutfile ----------------------------------------------
:saveOutfile
SET function=saveOutfile
MAGICK temp\!rotFile! -crop %1x%2+%3+%4 output\!outFile!
CALL :db %function% "saving to temp\!outfile!."
EXIT /B 0
REM End :saveOutfile ----------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment