Page 1 of 1

Shell scripting with variables / convert text

Posted: 2013-08-04T12:47:47-07:00
by hildwin
Hi, I'm quite new to this stuff.
I've got a webcam which produces jpg-files. These files should get a timestamp via the convert command. All work should be done in a shell script.
I tried on command line first:

Code: Select all

irt=`date +%e.' '%B' '%Y' '%H:%M' Uhr'`
This writes the actual time into "irt".

Code: Select all

> echo $irt
4. August 2013 21:41 Uhr

Code: Select all

convert image1t.jpg -font Kievit.ttf -fill white -pointsize 20 -draw "text 20,720 $irt" image1s.jpg

returns the error
convert.im6: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3158.

Code: Select all

convert image1t.jpg -font Kievit.ttf -fill white -pointsize 20 -draw 'text 20,720 $irt' image1s.jpg
returns the error
convert.im6: non-conforming drawing primitive definition `irt' @ error/draw.c/DrawImage/3158.

What is wrong? Pls help!
Thanks,

Re: Shell scripting with variables / convert text

Posted: 2013-08-04T14:11:26-07:00
by snibgo
As your question is about scripting, you should tell us what script language you are using (eg bash on Unix, or whatever).

Re: Shell scripting with variables / convert text

Posted: 2013-08-04T14:28:02-07:00
by fmw42
In Unix, variable must be enclosed by double quotes to be resolved. The problem is that without extra quotes, the spaces in your variable $irt are taken by draw as more arguments. So you need to put another set of double quotes in there.

For example, this work, but without the \"...\", I get your same error message.

convert logo: -font Arial -fill black -pointsize 20 -draw "text 50,100 \"$var\"" resultimage


You might like to switch to the use of -annotate, since it is more flexible and has less quoting issue.

convert logo: -font Arial -fill black -pointsize 20 -annotate +50+100 "$var" resultimage

see
http://www.imagemagick.org/Usage/text/#annotate

Re: Shell scripting with variables / convert text

Posted: 2013-08-04T16:53:51-07:00
by anthony
Basically the draw test syntax is
text X Y "text"
commas between arguments are optional, but some form of quoting (single or double) is NOT.

Draw also requires its whole argument string as a single argument so the shell needs quotes around that

As such the result is (with some extra spacing)

Code: Select all

  -draw  "    text 50,100 '$var'    "
HOWEVER, if the variable itself has quotes in it -- you have problems, as the shell expands the variabel, and draw sees the extra quotes!
See IM Exmaples, Draw, To Quote or Backslash?
http://www.imagemagick.org/Usage/draw/#quote

The better method is -annotate whcih removed the need for that extra level or quoting, though adds escape sequences instead.
IM Examples, Text to Image handling, Annotate
http://www.imagemagick.org/Usage/text/#annotate
fmw42 wrote:In Unix, variable must be enclosed by double quotes to be resolved.
Almost. The shell much expand the variable as it is what is holding the variables value.
If the variable contains spaces, and quotes, then you MUST wrap that variable in double quotes to preserve those characters (single quotes does not expand shell varables).

Because of this, always wrapping variable expansion in double quotes is a very good rule of thumb for almost all situations.

NOTE: the missing quotes (within the shell parsed quotes) is for the Draw operator's syntax. The quote could actually be included in the shell variable itself!!!

Code: Select all

my_text='"quoted text"'
convert -size 100x100 xc: -draw "text 10,50 $my_text"  show:
here the variable supplies draw with the quotes for its syntax. The quotes themselves are not 'drawn'
however here they are

Code: Select all

convert -size 100x100 xc: -draw "text 10,50 '$my_text' "  show:
But the draw 'MSVG string' parser will get confused in the variable contained single quotes!

To see what the draw parser sees, 'echo' the draw string

Code: Select all

echo  "text 10,50 '$my_text' "
text 10,50 '"quoted text"'
The shell will have stripped its quoting, and all spaces and other character (apart from a final newline added by echo) printed on screen is what is passed to ImageMagick for just that one argument.

Finally the IMv7 "magick" script passer was designed to parse quotes and backslashes in exactly the same way as the UNIX "bash" shell. Especially in regard to backslashing and quoting of newlines.

Re: Shell scripting with variables / convert text

Posted: 2013-08-05T10:00:03-07:00
by hildwin
@All, thanks a lot. It worked fine with annotate.

I use this in a Debian shell script which runs via crontab every five minutes.
In $pt I put the whole string: text plus time.

Code: Select all

/usr/bin/convert $impn -resize 1024 -font Kievit.ttf -fill white -pointsize 20 -annotate 0x0+20+720 "$pt" $impk