A Little Help on the Tutorial Pages

Grüezi from Switzerland. I try to familiar myself with gmic, just the basic stuff such as resizing a batch of images in one go and so on. I found the tutorial but some sections seem to be broken, sich as https://gmic.eu/tutorial/command_items_and_selections.html Can anyone please point out how I can iterate over a number of images provided on the command line to a filter…

you’re probably looking for foreach

sample colorful,earth,duck,dog foreach { +blur 10 sub normalize 0,255 }

It will apply the blur; etc commands to all images in the list.

… or you can just load a bunch of images and apply a command to all:

gmic *.jpg blur 10

And here’s the link to the missing page :


1 Like

Thanks and… yes and no. I want to understand how to use it when I write an own filter/new command, which processes a set of images and I want to access the individual images that have been handed over as arguments (plus I need to know how I add a suffix to a filename (as handed over a argument) before the extension.

On a more general basis, I have browsed the documentation and would have loved some more information on how to handle arguments in new commands. But perhaps the documentation did not yet catch my attention.

Are you talking about this ?

The execution of a G’MIC command may be restricted only to a subset of the image list, by appending [selection] to the command name. Examples of valid syntaxes for selection are:

command[-2]: Apply command only on the penultimate image [-2] of the list.

command[0,1,3]: Apply command only on images [0], [1] and [3].

command[3-6]: Apply command only on images [3] to [6] (i.e, [3], [4], [5] and [6]).

command[50%-100%]: Apply command only on the second half of the image list.

command[0,-4–1]: Apply command only on the first image and the last four images.

command[0-9:3]: Apply command only on images [0] to [9], with a step of 3 (i.e. on images [0], [3], [6] and [9]).

command[0–1:2]: Apply command only on images of the list with even indices.

command[0,2-4,50%–1]: Apply command on images [0], [2], [3], [4] and on the second half of the image list.

command[^0,1]: Apply command on all images except the first two.

command[name1,name2]: Apply command on named images name1 and name2.

That I have seen. Let’s make it easier to explain. I want to write a new command helmutscommand and I would like to see a foreach or oder loop over the arguments of the command. The only thing within the loop should be to output the arguments (which are names for images) and output the name of the image without the extension. I think if I have that, I can take it from there.
And, @prawnsushi, thanks so much!

something like this?
(NB : you can use foreach ... done or foreach { ... } . Just be prepared for the “escaping” nightmare in the command line if you use { ... } :smiley:

gmic -v 0 *.png run 'foreach echo {b} done'


foreach {
echo {b}

If you save helmutscommand in a .gmic file you can call it like this :
gmic ./helmut.gmic *.png helmutscommand p

(p means : do not display images)

gmic -v 0 *.png run 'foreach echo {b} done' 

Here are a few variables (Substitution rules) :

{image,feature[:delimiter]} is substituted by a specific feature of the image [image]. image can be either an image number or an image name. It can be also eluded, in which case, the last image [-1] of the list is considered for the requested feature. Specified feature can be one of:

b: The image basename (i.e. filename without the folder path nor extension).

f: The image folder name.

n: The image name or filename (if the image has been read from a file).

t: The text string from the image values regarded as character codes.

x: The image extension (i.e the characters after the last . in the image name).

And you can do this in bash with basename :
basename -as *.png

Yes, thank you, @prawnsushi !

This is a work in progress in Tutorial Land. Maybe something on that topic around late September. Terse notes for now.

@David_Tschumperle wrote the Bouncing Ball Tutorial as an intuitive example for developing a custom command. It has a companion video. You get to watch him compose the command in real time, furnishing some insights that are hard to teach.

At its absolute essence, a custom command is:

commandname : <pipeline>…

The commandname becomes a key to select the <pipeline> from an internal, dynamically maintained table. command (m) makes entries to that table; uncommand (um) removes them.

During command line itemization, G’MIC tries to match commandname to built-ins. Failing that, it tries to retrieve from its internal table a pipeline identified by the commandname key. Should that succeed, it assumes it has a custom command. This custom command needs to know (1) The images it is to work on, and (2) the arguments it has been given.

  1. As @prawnsushi noted, Selection Decorators furnish the command with a subset “need-to-know” itemization of the image list. So far as the custom command knows, these are all the images in the universe, packaged on its own “private” image list. G’MIC furnishes the $! substitution, the size of this private image list. In repeat {$!} … [$>] … [$<] … done iterators, $> successively assumes increasing indices from 0, counting from the start of the list; $< successively assumes decreasing indices from $!–1, counting from the end of the list.
  2. G’MIC provides $-expressions to furnish information about the comma-separated (no intervening spaces) argument list. $-expressions are substitutions available in particular to custom commands and represent the argument list in various ways to the custom command. Generally, $0 represents the name of your custom command, $1 represents your first argument, ${$#–1} represents your last argument, where $# is the custom command substitution representing the length of your argument list. Observe that since $-expressions give your arguments numerical names, you can mix-in math expressions to dynamically compute retrievals from the argument list, as in how I notated the last item on the argument list, above. Those of us interested in script readability typically remap $-expressions to more meaningful keys:

where ${<m>-<n>} hyphenations retrieves consecutive arguments as a comma-separated list. Notice that $1 and $enie are initially aliases, but enie may be re-assigned to another value later on. $-expressions are effectively read-only; it is a futile exercise to re-assign the value of the second argument:

2=`This is silly`

will not remap $2 to "This is silly."

That should give you enough rabbit holes into which you may tumble. Watch your first step; have fun.

Sorry for the noise. Struck out notation does not work.


Thank you, @grosgood. I appreciate your reply and it helped.