-1

I have many gif images.

The background of those images is transparent.

Then I have a collection of pixels that are placed on top of this transparent background. This collection needs to be moved to the exact center of the image. I need to do that accurately for all those images, and doing it manually is laborious and its hard to get it exactly right.

What would be the best way to automatically move the visible pixel collection to the center of the picture?

john-jones
  • 7,490
  • 18
  • 53
  • 86
  • It's a bit unclear what you are asking. You have a problem with a code that is suppose to fill the center of an image using canvas (or do you want to use gimp which makes the qeustion off-topic)? If with canvas please share what you've tried so far. I would suggest editing the question to make it more clear. –  Sep 06 '13 at 17:54

1 Answers1

1

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
Paolo Gibellini
  • 310
  • 13
  • 21