1

Here's an Imagemagick command for retrieving pixel values from an image foo.tiff:

convert foo.tiff [1x1+40+30] -format %[fx:int(65535*r)],%[fx:int(65535*g)],%[fx:int(65535*b)] info:-

How do I format this to make it acceptable to the Tcl 'exec' command? I've tried a variety of escaping the various characters, enclosing in braces, and so on. I appreciate the advice...

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

1

Tcl will do the right thing if you split that string into a list:

set cmd {convert foo.tiff [1x1+40+30] -format %[fx:int(65535*r)],%[fx:int(65535*g)],%[fx:int(65535*b)] info:-}
exec {*}[split $cmd]
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 2
    That string is actually a well-formed list already. `exec {*}$cmd` would work. – Donal Fellows Apr 29 '15 at 08:45
  • That works! But what is this {*} thing? That's totally new to me...Thanks for the help. – Peter Hiscocks Apr 30 '15 at 01:00
  • It is one of the very few syntax rules of Tcl: it expands a list into its elements. Suppose `set var [list foo bar baz]`. When you call `acommand $var` then "acommand" receives only one argument, the list {foo bar baz}. When you call `acommand {*}$var` then "acommand" receives *three* arguments, the strings "foo", "bar" and "baz". See http://www.tcl.tk/man/tcl8.6/TclCmd/Tcl.htm -- in Tcl 8.4 and earlier, we had to resort to `eval` to achieve the same effect. – glenn jackman Apr 30 '15 at 02:15
1

Personally, I'd just quote the bits that must not be substituted with {}:

exec convert foo.tiff {[1x1+40+30]} -format {%[fx:int(65535*r)],%[fx:int(65535*g)],%[fx:int(65535*b)]} info:-
slebetman
  • 109,858
  • 19
  • 140
  • 171