Broken GMIC Tutorial Code?

I’m going through the GMIC tutorials and having an issue with the “-repeat (+)” tutorial at: http://gmic.eu/tutorial/_repeat.shtml

This is the code I’m running (copied/pasted from the tutorial):

gmic -input ampersand_goudybl1911.jpg -negative[-1] -blur[-1] 3,1,0 100% 100% -append c -expand_x {{w}/2} 100%,100%,100%,100% -append[-2,-1] y 100%,100%,100%,100% -repeat {360/6} k={6*($>+1)} --rotate[0] {$k},2,1,255,255 -apply_channels[-1] "-add "$k,hsl_h -blend[-2,-1] negation,0.7 -done -rm[-2] -output twirl.png

I keep getting an error:

[gmic]-0./ Start G’MIC interpreter.
[gmic]-0./ Input file ‘ampersand_goudybl1911.jpg’ at position 0 (1 image 256x256x1x3).
[gmic]-1./ Compute negative of image [0].
[gmic]-1./ Blur image [0] with standard deviation 3, neumann boundary conditions and quasi-gaussian kernel.
[gmic]-1./ Input black image at position 1 (1 image 100x1x1x1).
[gmic]-2./ Append images [0,1] along the ‘c’-axis, with alignment 0.
[gmic]-1./ Expand image [0] along the x-axis with size 128 and boundary conditions 1.
[gmic]-1./ Input black image at position 1 (1 image 100x100x1x1).
[gmic]-2./ Append images [0,1] along the ‘y’-axis, with alignment 0.
[gmic]-1./ Input black image at position 1 (1 image 100x100x1x1).
[gmic]-2./repeat/ Set local variable k=’{6('. [gmic]-2./*repeat/ *** Error *** Item substitution '{{6*(}’: Unbalanced parentheses/brackets, in expression ‘{6*($’.

Either I’m making a very basic mistake or the tutorial coding is incorrect? I’m wondering if any of you would mind running the code and see if you get the same results?

I know next to nothing about GMIC, but reading the error, it seems like you need an extra }.

Sometimes when you copy and paste there might be hidden characters or other weirdness.

Try erasing and then typing out k={6*($>+1)} . Also, erase the and retype the spaces on either side.

Tried that and still no luck :frowning:

I tried retyping the whole thing from scratch instead of copy/pasting and I’m getting the same error unfortunately. Thanks for that advise though! It was well worth trying.

Are you running in a Windows cmd environment? If so, it looks like you may need to escape the ‘>’ symbol, which is a special character for redirection of cmd output. Try ^> i.e. k={6*($^>+1)}

Might also get something eaten by your shell since in seems that the G’MIC doesn’t get {6*($>+1)} but the G’MIC interpreter only gets something like k={6*($ which is indeed unbalanced, you might want to try something like

echo gmic [rest of command]  

to see where it goes wrong

On bash I needed to escape some stuff to get it to work (note the backslashes near --rotate and -apply_channels) although I suspect you are not using bash (goes wrong somewhere else)

gmic -input ampersand_goudybl1911.svg -negative[-1] \ 
 -blur[-1] 3,1,0 100% 100% -append c -expand_x {{w}/2} 100%,100%,100%,100% \
 -append[-2,-1] y 100%,100%,100%,100% -repeat {360/6} k={6*($>+1)} \ 
 --rotate[0] {\$k},2,1,255,255 -apply_channels[-1] \"-add \"\$k,hsl_h \
 -blend[-2,-1]  negation,0.7 -done -rm[-2] -output twirl.png

EDIT: for code readability

As far as Bash is concerned, everything that is for G’MIC to interpret and not Bash should go between single quotes. Escaping individual characters in a long and confusing parameter like that will give you a headache and look bad.

Hmmmm… blunt. Let’s go blunt for blunt and see where that goes?
Perhaps this is childish of me but… YAY! What’s life without opinions on the internet?

If escaping individual characters in a long and confusing parameter is the cause of looking bad (ie. I guess I look bad) and having headaches (yes suddenly I feel a migraine coming on - this code is killing me…) why is it being used exactly as such here?

http://gmic.eu/tutorial/_repeat.shtml5
(^ this being part of the official tutorials provided to learn and experiment with GMIC)

If you’ve recommendations I’d be interested in your take on the code and how it should look.
That being said, I’m in love with GMIC, what a fantastic tool!!

True although in this case it is not to long (in my opinion) and (more important) multiple backslashes are a bit more visible then single quote, especially since it is not possible to put the whole command between single quotes (e.g. gmic '[gmic commands]' doesn’t work)

(also for some reason replacing the \"-add \"\$k with '"-add "$k' doesn’t work here)

EDIT: for code

@james is probably correct. Usually by the time you’re using loops, the command line becomes a hindrance. Creating a command file for input would make sense, but the tutorial aims to focus on the subject in question only - introducing more advanced features at that stage may be confusing.

That said, perhaps struggling with command line escape characters can be considered a valuable tutorial for anyone wishing to use it :stuck_out_tongue:

Edit: I thought I’d better mention that although the tutorials can be considered “official”, they were very kindly created by a community member (Garry Osgood) and I assume tested under linux. Free time and patience to write documentation is a rare thing :slight_smile:

@dutch_wolf it improves legibility if you surround code here on discuss in single backticks if it’s inline, like this:

This example code `$code=here` does nothing.

which produces this: This example code $code=here does nothing.

or between three backticks before/after if it spans multiple lines, like this:

This example code spans several lines:
```
#!/usr/bin/env bash
repo=“HOME/repo-rt" if [[ ! -d "{repo}” ]]; then blabla
```

which produces this:

This example code spans several lines:

#!/usr/bin/env bash
repo="$HOME/repo-rt"
if [[ ! -d "${repo}" ]]; then blabla

Now, if I read your example code correctly:

(also for some reason replacing the "-add "$k with ‘"-add "$k’ doesn’t work here)

then you can’t do that. If the code relies on parameter expansion in Bash as opposed to by G’MIC, then characters with special meaning, such as $, may not be single-quoted because that removes their special meaning. But k={6*($>+1)} does not look like Bash, meaning G’MIC is supposed to solve that, in which case you can surround it by single quotes.

I can help you with Bash, but I am clueless about G’MIC syntax.

I was referring to the code. Excessive escaping makes the code look bad, not you. I’m sure you’re an irresistibly handsome man.

1 Like

I feel it is out of scope for the GMIC tutorial to try and include escape sequences for the shell, even if it is bash, the best shell :wink:

(thanks for the tutorial on posting code btw, will edit my previous posts)

Yes the "-add "$k part is G’MIC specific (the k={6*($<+1)} creates a variable that is used here) and if I echo the lines with backslashes and with single quotes the output is the same (so nothing get eaten or changed by bash as far as I can tell) but G’MIC refuses the line with single quotes.

Funny enough the earlier part ({$k} after rotate command) does work with single quotes

The following command seems to work, when typed on bash:


$ gmic http://www.particularart.com/static/media/uploads/command_reference/repeat/ampersand_goudybl1911.svg -negative[-1] -blur[-1] 3,1,0 100% 100% -append c -expand_x {{w}/2} 100%,100%,100%,100% -append[-2,-1] y 100%,100%,100%,100% -repeat {360/6} 'k={6*($>+1)}' --rotate[0] '{$k}',2,1,255,255 -apply_channels[-1] \"-add \"\$k,hsl_h -blend[-2,-1] negation,0.7 -done -rm[-2]

though I admit that for such long command lines, I would rather suggest using a custom command file instead, like putting all this in file foo.gmic:


# File `foo.gmic`
foo : 
  -input http://www.particularart.com/static/media/uploads/command_reference/repeat/ampersand_goudybl1911.svg
  -negative[-1]
  -blur[-1] 3,1,0 
  100% 100% 
  -append c 
  -expand_x {{w}/2} 
  100%,100%,100%,100% 
  -append[-2,-1] y 
  100%,100%,100%,100% 
  -repeat {360/6} 
    k={6*($>+1)}
    --rotate[0] {$k},2,1,255,255
    -apply_channels[-1] "-add "$k,hsl_h
    -blend[-2,-1] negation,0.7 
  -done
  -rm[-2]

Then invoke it like this:

$ gmic foo.gmic -foo

Otherwise, you will really have hard times figuring out what are the substitution patterns that will be used by the shell and what are those you have to keep for G’MIC, meaning usually a lot of escapes or single quotes to add to the command line.

By the way, has anyone got recent news from @grosgood ?
Last time I’ve tried to contact him, I didn’t get any answer. I hope he is going well!

I just compiled G’MIC which using @Carmelo_DrRaw’s CMake script is ridiculously easy. I did that to try and tackle the long command that you had trouble with, but while it was compiling David beat me to it :slight_smile:

@David_Tschumperle why do some commands, such as --rotate start with a double dash?
gmic --help makes no mention of double dashes.

In reference documentation:

  • A G’MIC command starting with a double hyphen ‘–’ (instead of a single hyphen ‘-’) does not act
    ‘in-place’ but inserts its result as one or several new images at the end of the image list.

Any contribution to the documentation to improve it is welcome by the way :slight_smile:

Is the documentation stored in a semantically structured form anywhere? i.e. where all code is marked as code, all explanations are marked as that, with each command and its explanation as a list item, etc. I ask because I noticed yesterday that the doc source code is just one long document with no structure, each element is discretely formatted, making it impossible to automatically parse, but also making me think that it was automatically generated by something…