Friday, July 30, 2010

Creating text images via CLI on linux

Today I needed some text images. I could go to gimp and created it there, but I needed several and I really don't like repetitive work. The wonderful web is always your friend for these things.

So I found this blog that described creating a postscript page with enscript and converting this to a image format with convert. It boils down to the following command
$ echo "Some text" | enscript -B -f "Times-Roman36" -o - | convert -trim +repage -negate \
-border 2x2 -bordercolor gray - text.png

Enscript makes the postscript page. You can specify the font and the font size. (Enscript can do much much more then that (like creating tables), so be sure to check the manpage if you are interested in generating postscript files). With convert we trim it (remove whitespace around the text, so only the text remains and not a full page), negate it to make text white, make a 2 pixel border and make it gray. The result looks like this


Now I've used ImageMagick several times (I my opinion this is one hell of a CLI tool) and I think it was capable of generating text images itself (without the need of enscript). So I went throug the manpage and came op with the command
$ convert -background gray -fill white -font Times-Roman -pointsize 36 label:"Some text" \
text2.png

This produces:

Creating text with convert has much more possibilities. Have a look on the examples page for more info.
If you want to know which fonts are available, you can check the type-ghostscript file.
$ locate type-ghostscript.xml |grep ImageMagick
/usr/lib64/ImageMagick-6.5.8/config/type-ghostscript.xml
$ grep "type name=" /usr/lib64/ImageMagick-6.5.8/config/type-ghostscript.xml | sed -e \
's/.*type name=//' | awk '{print $1}'
"AvantGarde-Book"
"AvantGarde-BookOblique"
"AvantGarde-Demi"
"AvantGarde-DemiOblique"
"Bookman-Demi"
"Bookman-DemiItalic"
"Bookman-Light"
"Bookman-LightItalic"
"Courier"
"Courier-Bold"
"Courier-Oblique"
"Courier-BoldOblique"
"fixed"
"Helvetica"
"Helvetica-Bold"
"Helvetica-Oblique"
"Helvetica-BoldOblique"
"Helvetica-Narrow"
"Helvetica-Narrow-Oblique"
"Helvetica-Narrow-Bold"
"Helvetica-Narrow-BoldOblique"
"NewCenturySchlbk-Roman"
"NewCenturySchlbk-Italic"
"NewCenturySchlbk-Bold"
"NewCenturySchlbk-BoldItalic"
"Palatino-Roman"
"Palatino-Italic"
"Palatino-Bold"
"Palatino-BoldItalic"
"Times-Roman"
"Times-Bold"
"Times-Italic"
"Times-BoldItalic"
"Symbol"


You can always use a true type font on your system and use that
$ locate -r "\.ttf$"|grep -i liberation| grep -i mono
/usr/share/fonts/liberation/LiberationMono-Bold.ttf
/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf
/usr/share/fonts/liberation/LiberationMono-Italic.ttf
/usr/share/fonts/liberation/LiberationMono-Regular.ttf
$ convert -background gray -fill white -font /usr/share/fonts/liberation/LiberationMono-Bold.ttf -pointsize 36 label:"Some text" text3.png

No comments:

Post a Comment