我有很多gif图片。
这些图像的背景是透明的。
然后我有一组放在这个透明背景上的像素。 需要将此集合移动到图像的确切中心。 我需要准确地为所有这些图像做到这一点,并且手动完成它是很费力的,很难完全正确。
将可见像素集合自动移动到图片中心的最佳方法是什么?
答案 0 :(得分: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