1 lien privé
Linux is a powerful and versatile operating system that can handle a wide range of tasks. One of those tasks is adding text to images, which can be useful for creating graphics, designing logos, or simply adding captions to photos. In this article, we’ll show you how to add text to images using Linux command line tools.
To do this, you’ll use the convert command from ImageMagick. The basic syntax for adding text is:
convert input_image -pointsize 20 -fill white -annotate +100+100 'Your Text Here' output_image
Let’s break this down:
`input_image` is the name of your input image file.
`-pointsize` is the size of the font you want to use.
`-fill` is the color of the text.
`-annotate` is the position of the text. The +100+100 values indicate the X and Y coordinates of the text relative to the top left corner of the image.
`output_image` is the name of the output image file.
Here’s an example command:
convert myimage.png -pointsize 24 -fill white -annotate +50+50 'Hello World' output.png
This command adds the text “Hello World” to the image myimage.png in white 24-point font, positioned 50 pixels from the top left corner of the image. The output image is saved as output.png.
Step 4: Adjust Text Properties
You can adjust the font, color, and position of your text by modifying the command parameters. For example, you can use a different font by adding the -font parameter, like this:
convert input_image -pointsize 20 -font Times-Roman -fill white -annotate +100+100 'Your Text Here' output_image
You can also adjust the opacity of the text using the -alpha parameter. A value of Opaque will make the text fully opaque, while a value of Transparent will make it fully transparent. You can use values between 0 and 1 to create a semi-transparent effect.
Step 5: Save Your Image
Once you’re happy with your text, you can save your image using the -quality parameter. This determines the compression level of the image, with higher values producing better quality images but larger file sizes.
For example:
convert input_image -pointsize 20 -fill white -annotate +100+100 'Your Text Here' -quality 90 output_image.jpg
This command adds text to the input image and saves the output as a JPEG file with a quality of 90.
Step 6: Batch Processing
If you need to add text to multiple images, you can use a batch processing script to automate the process. For example, you can create a shell script that loops through a directory of images and adds text to each one.
Here’s an example script that adds the same text to all images in a directory:
/#!/bin/bash
for file in *.png
do
convert "$file" -pointsize 24 -fill white -annotate +50+50 'Hello World' "${file%.png}_text.png"
done
This script loops through all PNG files in the current directory, adds the text “Hello World” to each one, and saves the output as a new file with “_text” appended to the original file name.