Loading...
X

How to add a caption to a large number of images

Bulk add captions to images

The article “How to add text to images in ImageMagick” shows many examples of adding text with the ability to change all its properties using the “magick” program.

The “magick” utility is part of the ImageMagick package. For details on installing ImageMagick, including which dependencies you need to install to support the maximum number of formats, as well as a description of the command structure, examples of usage and all options, see the article: ImageMagick guide: installing, using, and troubleshooting.

But what if you need to add text to dozens or hundreds of images? This article will show you how.

Suppose there is a task to add text to all JPG files in the current directory and save them in the specified folder, while each file must be written with its original name.

First, let's note that in order not to specify each file separately, we will use “Wildcards in the file name”, that is, we will specify “*.jpg” as the input file.

As the output file, we will use “Specifying a file name pattern”.

Some “properties” must be defined in a special way in order to be used. For example, only “properties” prefixed with “filename:” can be used to change the output filename of an image.

Let's create a “withtext” directory:

mkdir withtext

The following command reads all .jpg files in the current directory one by one, adds the inscription “zaLinux.ru” to each of them, and saves the resulting images to the “withtext” directory with the same names:

magick *.jpg -stroke black -undercolor Red -strokewidth 3 -font Times-New-Roman -pointsize 72 -fill DarkViolet -annotate +650+500 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

To make it easier for you to understand, the OPTIONS FOR ADDING TEXT have been removed from the following template:

magick *.jpg OPTIONS FOR ADDING TEXT -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

The following are used as OPTIONS FOR ADDING TEXT:

-stroke black -undercolor Red -strokewidth 3 -font Times-New-Roman -pointsize 72 -fill DarkViolet -annotate +650+500 'zaLinux.ru'

If any of these options is not clear to you, then refer to the article, the link to which is given above.

Adding text can be combined with any other operations, for example, the following command compresses all JPEG images to 1000 pixels, adds text with the specified properties, and saves the new images to the “withtext” directory:

magick *.jpg -scale 1000 -stroke black -undercolor Red -strokewidth 3 -font Times-New-Roman -pointsize 72 -fill DarkViolet -annotate +650+500 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

How to add a watermark to a large number of images

Section “How to add text with transparency to a photo. How to make a translucent background for the added text” shows how to add transparent text – this approach can be used to add a watermark to a large number of images.

You can also make the text stroke partially transparent (or remove it altogether). In the following example, both the stroke (black) and text (white) are both set to opacity 0.3:

magick *.jpg -scale 1000 -stroke 'rgba(0,0,0,0.3)' -strokewidth 3 -font Liberation-Serif -pointsize 72 -fill 'rgba(255,255,255,0.3)' -annotate +650+500 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

In the following example, text is also added to all JPEG files in the current directory, the resulting images are saved in the “withtext” directory. Text properties: font color white, opacity 0.4, background color black, opacity 0.4:

magick *.jpg -scale 1000 -undercolor 'rgba(0,0,0,0.4)' -font Liberation-Serif -pointsize 72 -fill 'rgba(255,255,255,0.4)' -annotate +650+500 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

How to choose the size of the text and its coordinates depending on the size of the image

ImageMagick has the ability to do mathematical calculations using various image properties. Mathematical calculations can be done in the %[fx:…] construct.

You can see the conventions for various image properties on this page: https://imagemagick.org/script/escape.php

For example, the width in the expression [fx:…] is denoted by the letter “w”, and the height by the letter “h”.

The following expression means width divided by 2:

%[fx:w/400]

And this expression means width minus 400 pixels:

%[fx:w-400]

These expressions can be substituted as coordinates for the -annotate option.

For example, the following -annotate option will add a caption to an image, indented 400px from the right side and 25px from the bottom of the image:

-annotate +%[fx:w-400]+%[fx:h-25] 'zaLinux.ru'

Full command example:

magick IMAGE.jpg -pointsize 72 -fill 'rgba(148,0,211,0.5)' -annotate +%[fx:w-400]+%[fx:h-25] 'zaLinux.ru' test.jpg

Automatic coordinate calculation can be used in web services, to automatically add watermarks to photos on websites, or to batch process many photos.

The following command is similar to the ones already discussed, it adds a label to all JPEG images in the current folder and saves them to the “withtext” directory. The peculiarity of this command is that it automatically calculates the position of the text, placing it 400 and 25 pixels from the bottom right edge:

magick *.jpg -scale 1000 -undercolor 'rgba(0,0,0,0.4)' -font Liberation-Serif -pointsize 72 -fill 'rgba(255,255,255,0.4)' -annotate +%[fx:w-400]+%[fx:h-25] 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

And in this command, the font size, as well as the indentation from the bottom right corner, is calculated automatically, adjusting to the width and height:

magick *.jpg -scale 1000 -undercolor 'rgba(0,0,0,0.4)' -font Liberation-Serif -pointsize %[fx:w/14] -fill 'rgba(255,255,255,0.4)' -annotate '+%[fx:w-(w/2.5)]+%[fx:h-(h/23)]' 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

Note that the expression for calculating the coordinates is now in quotation marks because it uses parentheses. Parentheses are needed to indicate the priority of performing mathematical operations. And the quotes are needed for the reason that the parentheses have a special meaning for the Linux shell, and in order for them to be sent directly to the “magick” utility instead of being interpreted by the shell, the entire value of the option is placed in quotes.

The following command sets the new size of the images, and there is no need to change such parameters of the inscription as the font size and coordinates of its location – they are calculated automatically based on the width and height of the image:

magick *.jpg -scale 2000 -undercolor 'rgba(0,0,0,0.4)' -font Liberation-Serif -pointsize %[fx:w/14] -fill 'rgba(255,255,255,0.4)' -annotate '+%[fx:w-(w/2.5)]+%[fx:h-(h/23)]' 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

In the previous commands, the final image size was specified explicitly, that is, all photos were reduced to the same size. But in fact, it is not necessary to change the size of the pictures. For example, the following command will select the appropriate font size and caption coordinates so that the caption is large and legible and does not go beyond the image – the font size and padding are selected for each photo individually depending on its width and height:

magick *.jpg -undercolor 'rgba(0,0,0,0.4)' -font Liberation-Serif -pointsize %[fx:w/14] -fill 'rgba(255,255,255,0.4)' -annotate '+%[fx:w-(w/2.5)]+%[fx:h-(h/23)]' 'zaLinux.ru' -set filename:currentfile '%t' 'withtext/%[filename:currentfile].jpg'

Leave Your Observation

Your email address will not be published. Required fields are marked *