Linux Applications

ImageMagick: Creating Text Images

ImageMagick can be used for a lot of things, and we all know that, but did you know that you can also create texts and some fancy stuff with it? What this means is that you don’t need a paint or even an existing image to create a piece of text. Why you’d want to create a piece of text is totally up to you. But the point is, ImageMagick offers a variety of switches just for this purpose. In this tutorial, we will learn how to create a novel image with just a piece of text in it.

Installing ImageMagick

We need the ImageMagick for this process. First, let’s install it:

sudo apt-get install imagemagick

Creating a Text Image from Scratch

Let’s say that we want to create an image from scratch. We don’t have to go to paint if we only want to add some text to it. Basically, we don’t have an initial image here. But let’s say we write the following:

convert -background lightblue -fill black  -pointsize 80 label:LinuxHint image.jpeg

Please notice how we didn’t give it an input image. What this does is to create a new image that looks like this:

  • background : This is the color of the background. Here, it is set to light blue.
  • fill : The color that is used to fill a graphic primitive. Use the option -list color to list all the colors that are available to you. Here, the text is filled with black.
  • pointsize : It’s the pointsize of the font. Here, we set it to 80.
  • label : To an image, it gives it a label. Here, it’s LinuxHint.
  • image.jpeg : The name of the output image.

Now, let’s add another switch – the size switch:

convert -background lightblue -fill black  -size 165x70 -pointsize 24 label:LinuxHint image2.jpeg

The size switch is used to set the width and the height of the image. The size of the canvas itself is now 165×70. By default, the text is located at the NorthWest (unless you specify the location).

But, you don’t have to give it the complete dimensions! Here, in this first example, we write 160x. We leave the second part blank.

convert -background lightblue -fill black -size 160x  label:LinuxHint image2.1.jpeg

Or, you can also do this:

convert -background lightblue -fill black -size x40  label:LinuxHint image2.2.jpeg

Now, note that the text finds itself at the top-left corner of the image if the canvas is too big. ImageMagick offers you an option to reposition the text. So, to reposition the text, use the gravity switch.

The choices for the gravity include NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast.

Let’s choose the SouthEast for the sake of our example:

convert -background lightblue -fill black  -size 165x70  -pointsize 24  -gravity SouthEast label:LinuxHint image3.jpeg

You can obviously play around with the choices offered.

Now, we don’t want a one-liner but we want multiple lines. Basically, what if you had lots to say? Please note that for the label, we use \n to create a new line.

convert -background lightblue  -fill black  -pointsize 20 label:'LinuxHint\nYour One Stop for\nall things Linux'  image4.jpeg

Vertical Labels

A vertical label is basically just a piece of text that is positioned vertically instead of horizontally. Here, you can opt to use the code that’s a bit more complex or use your brain for an instance and write this:

convert -background lightblue -fill black -pointsize 30 label:'L\ni\nn\nu\nx\nH\ni\nn\nt' image5.jpg

Once again, you can add more switches to beautify it if you wish. For example:

convert -background lightblue -fill black -gravity Center -size 500x500 -pointsize 30 label:'L\ni\nn\nu\nx\nH\ni\nn\nt' image6.jpg

Well, the idea that we are trying to get across is that you can add any switch you want and turn it into something special.

Caption

Much like the label that is used to add text, you can also use the caption. The difference between the two is that the caption word wraps everything. If you have a very long sentence, for example, the label (if you don’t give it a size) will try to fit everything on one line. If you use the size switch with label, there’s a chance it will overflow. The caption will automatically word wrap it (use new lines to finish the sentence). But the caption also needs the right canvas size, or else it will also overflow.

Let me show you:

convert -background lightblue -fill black -gravity Center -size 320x140 -pointsize 30 label:'LinuxHint, your one stop hub for all things Linux' image7.jpg

As you can see, the size that we used wasn’t big enough to contain the entire line, so it’s overflowing.

Now, let’s give it some leeway (-size 320x).

convert -background lightblue -fill black -gravity Center -size 320x -pointsize 30 label:'LinuxHint, your one stop hub for all things Linux' image7.2.jpg


Please note that if we give it some leeway, it can write the entire line in a single line! It doesn’t word wrap.

Now, let’s use the caption:

convert -background lightblue  -fill black -pointsize 36 -size 320x   caption:'LinuxHint, your one stop hub for all things Linux' image8.jpg

We gave it a leeway, but it word wrapped. Please note that as with label, if the size of the canvas isn’t big enough, it can overflow or be cut off. For example:

convert -background lightblue  -fill black -pointsize 32 -size 320x60  -gravity South caption: 'LinuxHint, your one stop hub for all things Linux' image9.jpg

But you can write many lines using the caption as well. For example:

convert -background lightblue  -fill black -pointsize 32 -size 320x  -gravity Center caption: 'LinuxHint, your one stop hub for all things Linux.\nVisit us at LinuxHint.com' image10.jpg

For more information about the text images, please visit https://legacy.imagemagick.org/Usage/text/.

Conclusion

ImageMagick is truly fantastic. With just one line of code, we can create just about anything we want. In particular, in this tutorial, we learned a few things. We learned that we don’t need an initial image to create the text images. We learned that we can create the text images and adjust it using a variety of switches such as the size switch, the gravity switch, the pointsize switch, etc. We learned that we can create the text image horizontally and vertically. And that we can word wrap it using the caption instead of the label. Now, this is just the basics of it. There’s a lot more to it. But if you can handle the basics, you can also handle the hard stuff. The next time you need to create a text image, use the ImageMagick instead of Paint!

About the author

Kalyani Rajalingham

I'm a linux and code lover.