How to draw a picture with text in the center of that via command-line?

I need to paint an image with text in the center via command-line to further put into the video. A simple yellow text on black background.

I tried FFmpeg way:

ffmpeg -loop 1 -r 1 -i b.png -b:v 1M -vf "scale=320:240, drawtext=fontfile=/usr/share/fonts/truetype/DejaVuSans.ttf: text='Test Text'" -vcodec mpeg4 -acodec aac preroll.mp4

Which gave me the following error:

[AVFilterGraph @ 0xf56020] No such filter: 'drawtext'

I tried to install ivtools and tried drawtool and idraw which are obviously for desktop environments.

How to create a simple image with text in Ubuntu programatically?


Solution 1:

What about this command:

convert -background black -size 800x480 -fill "#ff0080"  -pointsize 72 -gravity center label:'Ask Ubuntu' output.png

enter image description here

By using above command you create an empty .png image which its background color is black.

-background color
   set the background color.
   the default background color (if none is specified or found in the image) is white.

The created image size will be 800x480(width[xheight]).

-size width[xheight][+offset]
   set the width and height of the raw image.

-fill color
   color to use when filling a graphic primitive.

This option accepts a color name, a hex color, or a numerical RGB, RGBA, HSL, HSLA, CMYK, or CMYKA specification. See Color Names for a description of how to properly specify the color argument.

For example,

 -fill blue
 -fill "#ff0080"
 -fill "rgb(255,0,128)" 

Enclose the color specification in quotation marks to prevent the "#" or the parentheses from being interpreted by your shell.

To print a complete list of color names, use the -list color option.

-pointsize size
   pointsize of the PostScript, OPTION1, or TrueType font.

-gravity type
   sets the current gravity suggestion for various other settings and options.

Choices include: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast. Use -list gravity to get a complete list of -gravity settings available in your ImageMagick installation.

label:'your Text Here' write your text into image.png.

Also see more examples.

See full command-line options

Solution 2:

If we were talking about outside video, I would make a simple SVG in inkscape with a placeholder string like OLISTRING, get it how I want it to look then manipulate that on the command line to substitute my dynamic text in. And use Inkscape to rasterise it.

inkscape -z -e intro.png -w 1920 -h 1080 <(sed 's/OLISTRING/My Video!/' drawing.svg)

The real benefit with this approach is you're only limited by what you can do in Inkscape... You also get a pretty crisp output thanks to Inkscape's rendering engine.

Here's the outcome:

enter image description here

As rendered from this (going through my sed)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="1920"
   height="1080"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.4 r9939"
   sodipodi:docname="New document 1">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#000000"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="1"
     inkscape:pageshadow="2"
     inkscape:zoom="0.35"
     inkscape:cx="375"
     inkscape:cy="520"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1920"
     inkscape:window-height="1130"
     inkscape:window-x="-2"
     inkscape:window-y="-3"
     inkscape:window-maximized="1" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(0,27.637817)">
    <flowRoot
       xml:space="preserve"
       id="flowRoot2985"
       style="fill:black;stroke:none;stroke-opacity:1;stroke-width:1px;stroke-linejoin:miter;stroke-linecap:butt;fill-opacity:1;font-family:Sans;font-style:normal;font-weight:normal;font-size:72px;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;text-align:center"><flowRegion
         id="flowRegion2987"><rect
           id="rect2989"
           width="1917.1428"
           height="662.85712"
           x="0"
           y="174.28572"
           style="text-anchor:middle;text-align:center;font-size:72px" /></flowRegion><flowPara
         id="flowPara2991"></flowPara></flowRoot>    <flowRoot
       xml:space="preserve"
       id="flowRoot2993"
       style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       transform="translate(0,192.36218)"><flowRegion
         id="flowRegion2995"><rect
           id="rect2997"
           width="1917.1428"
           height="700"
           x="0"
           y="151.42857" /></flowRegion><flowPara
         id="flowPara2999"
         style="font-size:244px;font-weight:bold;text-align:center;text-anchor:middle;fill:#ffff00;-inkscape-font-specification:Sans Bold">OLISTRING</flowPara></flowRoot>    <flowRoot
       xml:space="preserve"
       id="flowRoot3001"
       style="fill:black;stroke:none;stroke-opacity:1;stroke-width:1px;stroke-linejoin:miter;stroke-linecap:butt;fill-opacity:1;font-family:Sans;font-style:normal;font-weight:normal;font-size:40px;line-height:125%;letter-spacing:0px;word-spacing:0px"><flowRegion
         id="flowRegion3003"><rect
           id="rect3005"
           width="0"
           height="85.714287"
           x="962.85712"
           y="397.14285" /></flowRegion><flowPara
         id="flowPara3007"></flowPara></flowRoot>  </g>
</svg>

Solution 3:

I found a solution: installed Imagestick:

sudo apt-get install imagemagick

And then I used something like:

convert -pointsize 36 -fill red -draw 'text 10,10 "Loh pidr text" ' b.png test1.jpg

And then use

man convert

to see documentation and play aroud.