2

How can I create a numbered image series? I have to give number(writing on them) to thousands of images.

Is there an easy way to do this:

convert input.png -font arial -fill black -pointsize 10 -annotate +20+20 "1" output_0001.png

convert input.png -font arial -fill black -pointsize 10 -annotate +20+20 "2" output_0002.png

....

convert input.png -font arial -fill black -pointsize 10 -annotate +20+20 "1250" output_1250.png

3 Answers 3

4

Use a for loop:

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

EDIT: You did not specify in your question that you were on Windows. Since I do not know cmd.exe nor PowerShell, I will propose two alternative solutions:

If you have a Perl distribution (e.g. http://strawberryperl.com or http://dwimperl.com), try (untested):

#!/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";
}

If you have a C compiler, try (again untested);

#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;
}
4
  • Looks good but I'm getting "i was unexpected at this time" in command prompt.
    – destor
    May 1, 2015 at 10:54
  • It works over here (bash 4.3.33). What shell are you using? Try putting it in a #!/bin/bash script
    – user49740
    May 1, 2015 at 10:56
  • I was trying on windows 7. I copied your code in a bat file.
    – destor
    May 1, 2015 at 11:06
  • 1
    I didn't test C code but Perl script worked fine for me, thanks.
    – destor
    May 2, 2015 at 9:30
1

Instead of: output_1250.png

You want: output_%04d.png

%d tells imagemagick to insert an incremental number, e.g. 01.png 02.png, and %04d means to make the number 4 digits long, padding it with zeroes e.g. output_0001.png, output_0002.png ... output_1250.png

This way you only use one convert command.

1
  • 1
    Thanks but i actually meant writing numbers on the images, not only giving them an output number.
    – destor
    May 1, 2015 at 10:01
1

Couldn't find a quick answer anywhere, so I wrote a batch script to do exactly what you need:

@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

If you're new to batch, these are the bits you'll need to pay attention to:

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

This means it will only process the PNG files in this folder. Change it to whatever file extension you're using.

And take a look at the main command:

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

There are several things you can change, including the font, the color of the number (fill), as well as whether you'd like there to be an outline (stroke and strokewidth, remove if you don't want them.) Gravity determines where the number will be printed, in this case southeast means it'll be in the bottom right corner.

And you'll definitely want to change -pointsize, which controls how big the number is. I chose a value of 250 since the images I needed to number were all 1920x1080, but the font size will need to be changed depending on how big your target image is.

And this...

set /a count = 0

Means the first number printed on an image will be 1. Set this to -1 if you want it to be zero, higher or lower depending on the start number you need.

Hope this helps whoever finds it in the future!

1
  • EDIT: I came up with a neat script that figures out the size, stroke and offset automatically. Adds a drop shadow too. See here: pastebin.com/EzvKX2B5 Jul 28, 2022 at 7:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .