bat-file for semi automatic cropping of transparent PNG overlays

f95zoneuser463

Member
Game Developer
Aug 14, 2017
219
1,019
I needed a faster way to crop down many transparent full 1080p overlay PNGs for my game. At the same time I NEED to keep the info from where top-left-position it was cropped. This position is later required to place the image back at the exact same position in Ren'Py-script.

Requires ImageMagick!

Code:
@echo off
color 0f
title %1
cls
echo This batchfile requires imagemagick to be installed on the computer!
echo https://www.imagemagick.org/script/download.php
echo Remove the pause lines to run automatically.
echo.
pause
cls

REM THIS LINE WILL PRINT THE TRIM BOUNDING BOX:
REM documentation here: https://www.imagemagick.org/script/escape.php
REM magick %1 -format "%%@" info:
REM 295x267+971+548
REM crop-image-xsize | crop-image-ysize | crop-xtopleft | crop-ytopleft

REM WE NEED THE LAST TO COORDINATES, SAVE THEM TO A VARIABLE
REM https://stackoverflow.com/questions/2323292/assign-output-of-a-program-to-a-variable
for /f %%i in ('magick %1 -format "%%@" info:') do set TRIMBB=%%i

echo This line should show the trim bounding box:
echo %TRIMBB%
echo.

REM NOW WE NEED TO EXTRACT THE LAST TWO NUMBERS:
for /F "tokens=2,3 delims=+" %%a in ("%TRIMBB%") do (
    set XPOS=%%a
    set YPOS=%%b
)
echo This line should show extracted x- and y-positon:
echo xpos=%XPOS% ypos=%YPOS%
echo.

REM trim the image:
magick %1 -trim  "%~n1_cropped_%XPOS%_%YPOS%.png"

REM TODO: instead of appending the x/ypos to the filename find a way to store it in metadata
REM TODO: create second batchfile to quickly read metadata via drag and drop from cropped images
REM TODO: pass file to pngcrush to optimize size automatically
pause
I'm looking for another way to store this position tied to the image (Meta-Data). I tried to set a comment with Imagemagick but could not get it to work. So for now I just added the position to the filename. If somebody knows how to write and read this position to a PNG using some kind of small metadata (not XMP) ... that would be great.

The following zip contains the .bat-file and a random .png from my game for testing.


The main reason for cropping all overlay is not size or memory-consumtion but the poor performance of the RenPy-function im.MatrixColor().

Maybe this is useful for other developers too.

EDIT:
Nevermind. Got it everything working how I want it:


You don't have permission to view the spoiler content. Log in or register now.

I just could not get the comment working with ImageMagick for some reason, so ExifTool does the job now.

EDIT: 10. July. 2018
read metadata .bat improved to spit out Copy & Paste-Ready Ren'Py-Line:

Code:
@echo off
color 0f
title %1
cls
for /f "tokens=1,3" %%a in ('exiftool %1') do (
    if "%%a" == "Comment" ( set comment=%%b )        
)

for /f "tokens=1,2 delims=," %%a in ("%comment%") do (
    set XPOS=%%a
    set YPOS=%%b
)

echo This line should show extracted x- and y-positon:
echo xpos=%XPOS% ypos=%YPOS%
echo.

echo Format for RenPy show image:
echo at Position(xpos=%XPOS%,xanchor=0,ypos=%YPOS%,yanchor=0)
echo.
pause
 
  • Like
Reactions: Alteus and gue5t