-1

我有很多 gif 图像。

这些图像的背景是透明的。

然后我有一组放置在这个透明背景之上的像素。此集合需要移动到图像的确切中心。我需要为所有这些图像准确地做到这一点,而手动进行是费力的,而且很难完全正确地做到这一点。

将可见像素集合自动移动到图片中心的最佳方法是什么?

4

1 回答 1

1

如果您需要批处理许多图像,也许使用 ImageMagick ( http://www.imagemagick.org ) 通过批处理文件调用可执行文件更简单(在 Windows 中 - 在 Linux 或 OsX 中,您可以使用 shell 脚本)。

您可以修剪图像并在convert剩余图像周围添加透明边框。在我的示例中,我习惯于identify获取原始图像大小。

这里的批处理文件只是一个图像,但可以简单地扩展它来管理一个目录的所有图像:

@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
于 2013-09-10T13:17:25.857 回答