在 imagemagick 中建立編號圖像系列

在 imagemagick 中建立編號圖像系列

如何建立編號圖片系列?我必須給數千張圖像編號(在上面寫字)。

是否有捷徑可尋:

轉換 input.png -font arial -fill black -pointsize 10 -annotate +20+20“1”輸出_0001.png

轉換 input.png -font arial -fill black -pointsize 10 -annotate +20+20“2”輸出_0002.png

....

轉換 input.png -font arial -fill black -pointsize 10 -annotate +20+20“1250”輸出_1250.png

答案1

使用for循環:

for i in `seq 1 1250`
do convert input.png -font arial -fill black -pointsize 10 -annotate +20+20 $i output_$(printf %04d $i).png
done

編輯:您在問題中沒有指定您使用的是 Windows。由於我不知道 cmd.exe 也不了解 PowerShell,因此我將提出兩種替代方案:

如果您有 Perl 發行版(例如http://strawberryperl.com或者http://dwimperl.com),嘗試(未經測試):

#!/usr/bin/perl
use v5.14;
for (1 .. 1250) {
    my $x = sprintf "%04d", $_;
    system qw/convert input.png -font arial -fill black -pointsize 10 -annotate +20+20/, $_, "output_$x.png";
}

如果您有 C 編譯器,請嘗試(再次未經測試);

#include<stdio.h>
#include<stdlib.h>

int main(void){
    char cmd[1000];
    int i;
    for(i = 1 ; i <= 1250 ; i++){
        sprintf(cmd, "convert input.png -font arial -fill black -pointsize 10 -annotate +20+20 %d output_%04d.png", i, i);
        system(cmd);
    }
    return 0;
}

答案2

在任何地方都找不到快速答案,所以我編寫了一個批次腳本來完全滿足您的需求:

@echo off
setlocal enableextensions enabledelayedexpansion
set /a count = 0
For %%A in (*.png) DO (
  set /a count += 1
  echo Processing Image !count!
  magick convert -font arial-black -pointsize 250 -fill white -strokewidth 5  -stroke black -gravity southeast -annotate +100+0 "!count!" "%%A" "!count!_%%A"
)
endlocal
pause

如果您是批次新手,則需要注意以下幾點:

For %%A in (*.png) DO (

這意味著它只會處理該資料夾中的 PNG 檔案。將其變更為您正在使用的任何檔案副檔名。

看看主要指令:

magick convert -font arial-black -pointsize 250 -fill white -strokewidth 5  -stroke black -gravity southeast -annotate +100+0 "!count!" "%%A" "!count!_%%A"

有幾項您可以更改,包括字體、數字的顏色(填充),以及是否希望有輪廓(描邊和描邊寬度,如果不需要,請將其刪除。)確定數字的列印位置,在本例中東南意味著它將在右下角。

而且您肯定會想要更改 -pointsize,它控制數字的大小。我選擇了值 250,因為我需要編號的圖像都是 1920x1080,但字體大小需要根據目標圖像的大小進行更改。

和這個...

set /a count = 0

意味著印在影像上的第一個數字將為 1。

希望這對將來找到它的人有所幫助!

答案3

代替:output_1250.png

你要:output_%04d.png

%d告訴 imagemagick 插入一個增量數字,例如01.png 02.png,並且%04d意味著使數字長度為 4 位,並用零填充,例如output_0001.png, output_0002.png...output_1250.png

這樣您只需使用一個轉換指令。

相關內容