banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Convert Text Or A Text File To An Image Format


by YouCanToo

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 to create an email image that could be used on a webpage to help thwart spamming.

convert -size 200x30 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 attribute. 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 overall 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.



Dimensions: 250px x 30 px
File Size: 1.09 KB (1,114 bytes)

This is with the trim attribute:



Dimensions: 210px x 11 px
File Size: 1.1 KB (1,125 bytes)

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





Converting a text file to an image

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

convert -size 1024x1048 xc:gray -font /usr/share/fonts/webcore/verdanab.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 overall image size to 1024px X 1048px. 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:



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



You can find this and many other good how-tos in the PCLinuxOS Knowledge Base Wiki.



Previous Page              Top              Next Page