If you need to batch process many images, perhaps it's simpler use ImageMagick (http://www.imagemagick.org) calling the executables through a batch file (in Windows - in Linux or OsX you can use a shell script).
With convert
you can trim an image and add a transparent border around the remaining image. In my example I've used identify
to get the original image size.
The batch file here is for just an image, but is simple extend it to manage all the images of a directory:
@ECHO OFF
rem
rem
rem Create a copy of a given image centering the data on the transparent background
rem
rem
setlocal EnableDelayedExpansion
rem Check whether the input file exists
IF NOT EXIST "%1" GOTO no_file
rem base path
SET im_path=c:\Program Files\ImageMagick
rem Begin the processing (from here the code is variable dependent, so you can cycle e.g. on a folder)
FOR /f %%a in ("%1") DO (
SET curr_image=%1
SET out_image_name=%%~na_center%%~xa
)
rem Step 1: Get image size by invoking identify.exe
SET im_size_cmd="%im_path%\identify.exe" -format "%%[fx:w]x%%[fx:h]" "%CD%\%curr_image%"
FOR /f "usebackq" %%i IN (`"%im_size_cmd%"`) DO SET im_size=%%i
ECHO Image size: %im_size%
rem Step 2: Trim current image ("removes any edges that are exactly the same color as the corner pixels")
rem "%im_path%\convert.exe" "%CD%\%curr_image%" -trim +repage "%CD%\%out_image_name%"
rem Step 3: Add a transparent border centered around the image until the initial dimension
rem "%im_path%\convert.exe" "%CD%\%out_image_name%" -gravity center -alpha on -background none -bordercolor none -extent %im_size% +repage "%CD%\%out_image_name%"
rem Step 2 and 3 could be processed with one command:
"%im_path%\convert.exe" "%CD%\%curr_image%" -trim -gravity center -alpha on -background none -bordercolor none -extent %im_size% +repage "%CD%\%out_image_name%"
rem End of processing
GOTO ok_end
rem Output in case of non existing input
:no_file
ECHO Usage: %0 ^<image_name^>
GOTO end
rem End of process output
:ok_end
ECHO Process ended
rem End of batch file
:end