Converting text to images

From PCLinuxOSHelp Knowledge Base
Jump to: navigation, search

Converting text or text files to images

Convert a line of text to a image

First we will look at converting a simple line of text to a .PNG image file. We will use the following command. We will create a email image that could be used on a webpage to help thwart spamming.

The Code
convert -size 250x30 xc:transparent -font /usr/share/fonts/webcore/verdanab.ttf -fill black -pointsize 12 -draw "text 5,15 'myname@mydomainname.com'" -trim email.png


The size attribute is in pixels listed as the width and height.
The xc attribute is the background color, here we have it set to transparent. You can use any of the 256 web safe colors.
The font attribute this is the font face that you are going to use. The font type can be found in the path /usr/share/font. Here we are using a .ttf font.
The fill attribute is the color of the font stroke. Here we are using a black.
The pointsize attribute is our font size. This is specified in points. IE 12 is about the average print size that you find in most books or magazines.
The draw We are telling it that we are drawing Text, and to start out drawing from the corner or 0,0. we are starting 5 pixels from the left and 15 pixels down from the top left corner, with the text we want to display.
The trim attribute. This tells the program to trim off the excess spaces from the bottom and the edges. This will reduce the over all size of the finished image if it is less than the size specified in the size attribute.
The final part is the filename.

This is the result: the first is without the trim attribute. Since it is hard to distinguish the size of the file with a transparent background I will include the file dimensions and overall size.

Below are the images with a gray background. It is much easier to tell what the trim attribute does with a colored background.

Emailwithouttrim.png

Dimensions: 250px x 30 px File Size: 1.6 KiB (1,677 bytes)


This is with the trim attribute

Emai.png

Dimensions: 158px x 12 px File Size: 1.6 KB (1,673 bytes)

Converting a text file to a image

While most of the attributes above will remain the same the size, the xc, and the draw will change.

The Code
convert -size 400x400 xc:gray -font /usr/share/fonts/webcore/comic.ttf -fill black -pointsize 12 -draw "text 0,2 '$(cat set-cpu-freq.txt)'" -trim test-text.png


In this example we are going to increase our over all image size to 400px X 400px. We have also changed the xc attribute (background) to gray instead of transparent. We will also change the draw attribute to read from a text file.

So this is what it will look like without the trim attribute

Testnotrim-text.png

And this is what it looks like using the trim attribute. As you can see, the trim command has stripped off excess white space from the top, bottom and sides.

Test-text.png