Discussion:
Command substitution into argument to convert
(too old to reply)
Michael F. Stemper
2022-06-13 19:28:31 UTC
Permalink
I'm trying to time-stamp image files as they're downloaded. The
following works fine:

$ convert -pointsize 20 -fill black -draw 'text 10,20 "06/13 14:13"' RadarMap.png Stamped.png

However, hard-coding the time stamp would make it worthless. What
I really want to use is the output of:

$ date +"%m/%d %H:%M"

But no matter how I try to incorporate that into the command, I
get errors.

For instance,

$ convert -pointsize 20 -fill black -draw 'text 10,20 $(date +"%m/%d %H:%M")' RadarMap.png Stamped.png
convert: non-conforming drawing primitive definition `(' @ error/draw.c/DrawImage/3190.

or:

$ convert -pointsize 20 -fill black -draw "text 10,20 $(date +"%m/%d %H:%M")" RadarMap.png Stamped.png
convert: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3190.

I've even tried two steps, initializing a variable and using it:

$ ts=$(date +"%m/%d %H:%M")
$ echo $ts
06/13 14:24
$ convert -pointsize 20 -fill black -draw "text 10,20 $ts" RadarMap.png Stamped.png
convert: non-conforming drawing primitive definition `text' @ error/draw.c/DrawImage/3190.
$ convert -pointsize 20 -fill black -draw 'text 10,20 $ts' RadarMap.png Stamped.png
convert: non-conforming drawing primitive definition `ts' @ error/draw.c/DrawImage/3190.

I'm sure that what I need to do to get a dynamic argument into
convert will be obvious to everybody except me. What am I missing?

Thanks,
--
Michael F. Stemper
Deuteronomy 24:17
Michael F. Stemper
2022-06-13 20:29:45 UTC
Permalink
Post by Michael F. Stemper
I'm trying to time-stamp image files as they're downloaded. The
$ convert -pointsize 20 -fill black -draw 'text 10,20 "06/13 14:13"' RadarMap.png Stamped.png
However, hard-coding the time stamp would make it worthless. What
$ date +"%m/%d %H:%M"
But no matter how I try to incorporate that into the command, I
get errors.
One other data point. If I try using back-quotes for the
command substitution:

$ convert -pointsize 20 -fill black -draw 'text 10,20 `date +"%m/%d %H:%M"`' RadarMap.png Stamped.png

no error message is issued, but the text that's placed on
the image is:

date +"%m/%d %H:%M"
--
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.
Grant Taylor
2022-06-13 22:32:43 UTC
Permalink
Post by Michael F. Stemper
I'm sure that what I need to do to get a dynamic argument into
convert will be obvious to everybody except me. What am I missing?
I wonder if you are running into -- what I consider to be -- order of
operations issues related to quoting. I use the single quote around awk
statements so that the $ isn't interpreted. So there's a chance that
the $(...) inside of '...' isn't being interpreted either.

When I've run into these type of problems I usually punt to an eval
"$(date "...")" type thing and have date generate the entire command
string that is passed to eval.
--
Grant. . . .
unix || die
Lewis
2022-06-14 00:21:04 UTC
Permalink
Post by Michael F. Stemper
I'm trying to time-stamp image files as they're downloaded. The
$ convert -pointsize 20 -fill black -draw 'text 10,20 "06/13 14:13"' RadarMap.png Stamped.png
However, hard-coding the time stamp would make it worthless. What
$ date +"%m/%d %H:%M"
So do that.

$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw 'text 10,20 "$MYDATE"' RadarMap.png Stamped.png
Post by Michael F. Stemper
For instance,
$ convert -pointsize 20 -fill black -draw 'text 10,20 $(date +"%m/%d %H:%M")' RadarMap.png Stamped.png
Notice that you have changed the quotes since you have "" inside '' in
your original. You would need to figure out the escaping to make this
right. Setting a variable avoids this.
Post by Michael F. Stemper
$ convert -pointsize 20 -fill black -draw "text 10,20 $(date +"%m/%d %H:%M")" RadarMap.png Stamped.png
same issue, you are not quoting the way the original command is quoted.
Post by Michael F. Stemper
$ ts=$(date +"%m/%d %H:%M")
$ echo $ts
06/13 14:24
$ convert -pointsize 20 -fill black -draw "text 10,20 $ts" RadarMap.png Stamped.png
Your original line has 'text 10,20 "the date is here"'

Note the quotes.
--
I presume you're mortal, and may err.
Michael F. Stemper
2022-06-14 12:59:18 UTC
Permalink
Post by Lewis
Post by Michael F. Stemper
I'm trying to time-stamp image files as they're downloaded. The
$ convert -pointsize 20 -fill black -draw 'text 10,20 "06/13 14:13"' RadarMap.png Stamped.png
However, hard-coding the time stamp would make it worthless. What
$ date +"%m/%d %H:%M"
So do that.
$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw 'text 10,20 "$MYDATE"' RadarMap.png Stamped.png
No error message is given. The text drawn on the image is:
$MYDATE
Post by Lewis
Post by Michael F. Stemper
For instance,
$ convert -pointsize 20 -fill black -draw 'text 10,20 $(date +"%m/%d %H:%M")' RadarMap.png Stamped.png
Notice that you have changed the quotes since you have "" inside '' in
your original. You would need to figure out the escaping to make this
right. Setting a variable avoids this.
Post by Michael F. Stemper
$ convert -pointsize 20 -fill black -draw "text 10,20 $(date +"%m/%d %H:%M")" RadarMap.png Stamped.png
same issue, you are not quoting the way the original command is quoted.
This is true. I have tried several different ways of quoting. None
of them have worked for me.
Post by Lewis
Post by Michael F. Stemper
$ ts=$(date +"%m/%d %H:%M")
$ echo $ts
06/13 14:24
$ convert -pointsize 20 -fill black -draw "text 10,20 $ts" RadarMap.png Stamped.png
Your original line has 'text 10,20 "the date is here"'
Note the quotes.
And when I put single quotes around the text directive, I get:

$ ts=$(date +"%m/%d %H:%M")
$ convert -pointsize 20 -fill black -draw 'text 10,20 $ts' RadarMap.png Stamped.png
convert: non-conforming drawing primitive definition `ts' @ error/draw.c/DrawImage/3190.
$
--
Michael F. Stemper
A preposition is something you should never end a sentence with.
Lewis
2022-06-14 20:26:17 UTC
Permalink
Post by Lewis
Post by Lewis
Post by Michael F. Stemper
I'm trying to time-stamp image files as they're downloaded. The
$ convert -pointsize 20 -fill black -draw 'text 10,20 "06/13 14:13"' RadarMap.png Stamped.png
However, hard-coding the time stamp would make it worthless. What
$ date +"%m/%d %H:%M"
So do that.
$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw 'text 10,20 "$MYDATE"' RadarMap.png Stamped.png
$MYDATE
Then it is still a quoting issue, probably beaus you hav single quotes
around the outside. I do not use the convert tool, so I can’t tell you
specifically what the issue is, but the first thing I wold do is
eliminate the single quotes, assuming they are not required by convert.


$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw "text 10,20 $MYDATE" RadarMap.png Stamped.png

or maybe

$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw "text 10,20 \"$MYDATE\"" RadarMap.png Stamped.png

Remember that ' and " ae very different, and ' do not usually allow
expansion of variables contained inside, but sometimes it is hard to
know when a variable is being expanded by the shell and when it is being
passed to the command.

Either way, sometimes you just have to play with the quotes and the
escapes.
--
Were it not for frustration and humiliation I suppose the human race
would get ideas above its station. -Ogden Nash
Lewis
2022-06-14 23:38:34 UTC
Permalink
Post by Lewis
$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw "text 10,20 \"$MYDATE\"" RadarMap.png Stamped.png
This works:

MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill white -draw "text 10,20 \"$MYDATE\"" image.jpg image-date.jpg

I expect this also works, but did not try:
convert -pointsize 20 -fill white -draw "text 10,20 \"$(date +"%m/%d %H:%M")\"" image.jpg image-date.jpg

This SHOULD work because the $() should protect the "" inside...

(of course, this draws white text no matter what, which is fine if the
image is dark, but invisible if the top left is white).
--
The "H" in Jesus H Christ comes from "Harold be Thy name" in the
Lord's Prayer.
Michael F. Stemper
2022-06-15 12:55:18 UTC
Permalink
Post by Lewis
Post by Lewis
$ MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill black -draw "text 10,20 \"$MYDATE\"" RadarMap.png Stamped.png
MYDATE=$(date +"%m/%d %H:%M");convert -pointsize 20 -fill white -draw "text 10,20 \"$MYDATE\"" image.jpg image-date.jpg
convert -pointsize 20 -fill white -draw "text 10,20 \"$(date +"%m/%d %H:%M")\"" image.jpg image-date.jpg
Those both work. Thanks.

I had tried various permutations of single and double quotes, but
never thought of escaping them.
--
Michael F. Stemper
This email is to be read by its intended recipient only. Any other party
reading is required by the EULA to send me $500.00.
Loading...