How to Generate an Image of Text


How to Add a Text Label to an Image

How to Generate an Image of Text

Let's say you want to generate an image of some text -- e.g., you have a string of letters "Disaggregate Consulting" and you want to generate an image of it in, e.g, JPG format. Something that looks like this, for example:

Method A: An Image, but Not So Great

I use the ImageMagick package of image-processing commands for this task.

The instructions are rather simple. The reason I'm writing them down is because they do require some knowledge that isn't given in the man pages. Note that I'm using Linux, but ImageMagick is available in all major operating systems.

To carry out these instructions, you will need to be able use a command line interface. You will probably need to be able to read ImageMagick's man pages, in particular the man pages of its individual commands such as convert.

The basic concept behind these instructions is that ImageMagick considers any file that contains text as a being a virtual image of that text. Our goal is to convert that virtual image into a real image.

The default size of text's virtual image is that of "Letter" sized paper. (See the ImageMagick man page's "-page" option for details.)

So, to create an image of a text, we convert this virtual image of the text into a different format. In this case, we'll take text from standard input (stdin) and convert it.

The command line is:

echo "This is a test" | convert -background yellow -page 200x50 text:- mediocreimage.jpg

"convert" is an ImageMagick program that converts from one file format to another. The "text:-" is what brings text into the command; the "text:" portion tells convert that what follows is an image of type "text," and the "-" tells convert that the text is found via stdin, the standard input.

(The form "<type>:" is used to notify the command what kind of image the file is when ImageMagick is unable to determine the image type from the file's extension (e.g., .jpg, .gif). In this case, since there is no extension -- we're taking the information from stdin -- we need to explicity use "text:". You may notice from the ImageMagick man page that there's a lot of differnt image types; any of them can be used in this way when needed. In fact, we'll use a different type tas a prefix in the next section.)

The option "-page 200x15" tells convert that the incoming text should be considered as being inside an image that is 200 pixels wide and 50 high. This is a subtle point; convert will let you produce an appropriately-sized image by using the "-resize" or "-geometry" options, but in that case you are resizing the Letter-sized image down to a little image, and the resulting text will be impossible to see.

The option "-background yellow" tells convert the the virtual text image has a yellow background, and magically it does.

The result looks like this:

Method B: A Better Image of the Text

Now, the previous section generates an image, but frankly I don't like its looks; it's poorly centered, for example. Here's how to generate a much better image, in a few steps. Note that ImageMagick will let you combine these steps, but I'm showing this step by step for educational purposes.

The first step is to create a blank label of the correct size:

convert xc:yellow -resize 200x15! blank.ppm

The "xc:yellow" is a virtual image of type "xc," which stands for "contant image uniform color." This is a virtual image, and can be converted into a real image. In this case, I create a yellow virtual image of size120 pixels wide and 15 pixels tall -- exactly, because I added the "!" (unless I specify the size exactly, I sometimes get odd results in this exercise). I use the lossless "ppm" format for interemediate files; they are large files, but since I'm deleting them anyway it makes no difference.

convert blank.ppm -gravity "Center" -draw "text 0,0 'Disaggregate Consulting'" textimage.jpg

Convert to a jpg, but at the same time add the text. The option '-gravity "Center"' places the text into the center of the output file, and the "text 0,0" command places the string into the exact location defined by the "gravity" option instead of an offset to it.

The resulting image is far better:

Actually, it's possible to all this in a single-line command:

convert xc:yellow -resize 200x15! -gravity "Center" -draw "text 0,0 'Disaggregate Consulting'" textimage.jpg

How to Add a Label to an Image

So, let's say you want to add a label to an image -- take a jpg image and have text show up underneath it as part of the image. For example, let's add a caption to this image:

Note that the image has text written onto it. I've taken the original image, added that yellow space on the bottom, and placed text into it. Now I will show you how to do the same.

The "-label" option of convert does NOT add text to the image! That option sets an internal label in the file structure of certain kinds of image files. The only time "-label" draws text on an image is when its used in ImageMagick's "montage" command.

Before I start, I make certain that my basic image is the right size. If I add the text into the image and then start to resize the image, the text will distorted. All my intermediate files will be in the lossless "ppm" format.

To begin, I create a blank image with the correct background color for the label:

convert xc:yellow -resize 1x15! blank.ppm

Note that the label is narrow, just 1 pixel wide. This is because when I add images together, the shorter one will stretch -- and I don't want the basic image that I'm trying to caption to be the one that streteches.

Next, I add the images together:

convert -append original.jpg blank.ppm internmediate_file.ppm

This creates an image with a blank area at the bottom. I have to specify the background color, because the background color is what's used to fill in the space as the narrower image is widened to match the wider image.

In other words, we get:

To add the text, use:

convert internmediate_file.ppm -gravity "South" -draw "text 0,3 'Moshe and Friend'" output_file.jpg

As before, the "gravity" option controls where the text will appear, but this time I use "South" to place it at the bottom center, right in the center of the blank area that I just added. I use ""text 0,3" to move the text up slightly; otherwise the descenders are sometimes cut off.

We get the following image, with the desired caption:

(I am told that it 's possible to do this all these steps on a single comand line, but I will leave that as an exercise for the reader.)

Contact Information

This page is really in the wrong place for historical reasons; it should be on my professional web site, where I've got my professional contact information as well as some interesting speech technology software.

BACK to the Quddities page.