Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created June 16, 2026 06:46
Show Gist options
  • Select an option

  • Save KevinGutowski/d6e8231c7e99f021d03f1a3816ebc524 to your computer and use it in GitHub Desktop.

Select an option

Save KevinGutowski/d6e8231c7e99f021d03f1a3816ebc524 to your computer and use it in GitHub Desktop.
Simple script to crop pokemon card images
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Pokemon card cropper for Windows.
rem Usage:
rem 1. Drag image files or folders onto this .bat
rem 2. Or put this .bat in a folder with images and double-click it
rem
rem Cropped PNGs are written to a "cropped" folder next to this .bat.
set "CARD_W=63"
set "CARD_H=88"
call :find_magick
if not defined MAGICK_CMD (
echo ImageMagick 7 was not found.
echo.
where winget >nul 2>nul
if errorlevel 1 (
echo Automatic install needs Windows Package Manager ^(winget^), but winget was not found.
echo.
) else (
set /p "INSTALL_IM=Install ImageMagick now? [Y/N] "
if /I "!INSTALL_IM!"=="Y" (
winget install --id ImageMagick.ImageMagick --source winget --accept-package-agreements --accept-source-agreements
if not errorlevel 1 call :find_magick
if not defined MAGICK_CMD (
echo.
echo ImageMagick may be installed, but this window cannot find magick.exe yet.
echo Close this window and run the script again.
pause
exit /b 1
)
)
)
)
if not defined MAGICK_CMD (
echo ERROR: ImageMagick 7 is required.
echo.
echo Install it from https://imagemagick.org and run this script again.
echo During install, enable "Add application directory to your system path".
pause
exit /b 1
)
set "OUTDIR=%~dp0cropped"
if not exist "%OUTDIR%" mkdir "%OUTDIR%"
if "%~1"=="" (
echo Processing images next to this script...
for %%F in ("%~dp0*.jpg" "%~dp0*.jpeg" "%~dp0*.png" "%~dp0*.webp" "%~dp0*.tif" "%~dp0*.tiff") do (
if exist "%%~fF" call :process "%%~fF"
)
) else (
:args
if "%~1"=="" goto done
if exist "%~1\" (
for %%F in ("%~1\*.jpg" "%~1\*.jpeg" "%~1\*.png" "%~1\*.webp" "%~1\*.tif" "%~1\*.tiff") do (
if exist "%%~fF" call :process "%%~fF"
)
) else (
call :process "%~1"
)
shift
goto args
)
:done
echo.
echo Done. Cropped files are in:
echo "%OUTDIR%"
pause
exit /b 0
:process
set "IN=%~1"
set "OUT=%OUTDIR%\%~n1.png"
set "W="
set "H="
if /I "%~dp1"=="%OUTDIR%\" (
echo Skipping already-cropped file: "%IN%"
exit /b 0
)
if exist "%OUT%" (
echo Skipping existing output: "%OUT%"
exit /b 0
)
for /f "tokens=1,2" %%A in ('""%MAGICK_CMD%" identify -format "%%w %%h" "%IN%""') do (
set "W=%%A"
set "H=%%B"
)
if not defined W (
echo Skipping unreadable file: "%IN%"
exit /b 0
)
set /a "CROP_W=(H * CARD_W + CARD_H / 2) / CARD_H"
if !CROP_W! LEQ !W! (
set "CW=!CROP_W!"
set "CH=!H!"
) else (
set "CW=!W!"
set /a "CH=(W * CARD_H + CARD_W / 2) / CARD_W"
)
rem Official PNG alpha fit: 46.5 x 46.5 radius at 868 x 1212.
set /a "RX10=(CW * 465 + 434) / 868"
set /a "RY10=(CH * 465 + 606) / 1212"
set /a "RXI=RX10 / 10"
set /a "RXD=RX10 %% 10"
set /a "RYI=RY10 / 10"
set /a "RYD=RY10 %% 10"
set "RX=!RXI!.!RXD!"
set "RY=!RYI!.!RYD!"
set /a "X1=CW-1"
set /a "Y1=CH-1"
echo Cropping "%~nx1" !W!x!H! -^> !CW!x!CH!
"%MAGICK_CMD%" "%IN%" -auto-orient -gravity center -crop !CW!x!CH!+0+0 +repage -alpha set ^
^( -size !CW!x!CH! xc:none -fill white -draw "roundrectangle 0,0 !X1!,!Y1! !RX!,!RY!" ^) ^
-compose DstIn -composite "%OUT%"
if errorlevel 1 (
echo ERROR processing "%IN%"
)
if not errorlevel 1 echo Wrote "%OUT%"
exit /b 0
:find_magick
set "MAGICK_CMD="
if exist "%~dp0tools\ImageMagick\magick.exe" (
set "MAGICK_CMD=%~dp0tools\ImageMagick\magick.exe"
exit /b 0
)
for /f "delims=" %%M in ('where magick 2^>nul') do (
set "MAGICK_CMD=%%M"
exit /b 0
)
for /d %%D in ("%ProgramFiles%\ImageMagick-7*") do (
if exist "%%~fD\magick.exe" (
set "MAGICK_CMD=%%~fD\magick.exe"
exit /b 0
)
)
for /d %%D in ("%ProgramFiles(x86)%\ImageMagick-7*") do (
if exist "%%~fD\magick.exe" (
set "MAGICK_CMD=%%~fD\magick.exe"
exit /b 0
)
)
exit /b 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment