Brain Teaser in G'MIC scripting

I was wondering today how to write a G’MIC script to do this simple stuff : Considering a list of named images (obtained for instance with sp tiger,cat,dog,bottles), how to duplicate a copy of these images at the end of the list while keeping exactly the same names?.
There are several ways to do it, but I was looking for the shorter code that does it.

Here is a solution:

foo :
  sp tiger,cat,dog,bottles
  [^] repeat $!/2 { =>[{$!/2+$>}] {$>,n} } # Duplicate + remove the copymark

Any better idea?

Hint: There is actually one better way to do this, with a command I’ve just introduced this morning :slight_smile:

Well here’s one way, inspired by something @afre pointed out recently:
gmic sp tiger,cat,dog,bottles +foreach { }

1 Like

Ahah, yes, indeed. Except that this has been fixed in 3.2.1, as it was actually a bug.
foreach and local have to behave the same way, so in 3.2.1, +foreach also adds the copymark to the image names.
But good point!

Argh, so I’m already out of date again… time to upgrade!

3.2.1 is still in pre-release state though :slight_smile:

1 Like

Found another even shorter one (still on 3.2.0):
gmic sp tiger,cat,dog,bottles +rm[]

Edit: and another
gmic sp tiger,cat,dog,bottles +mv[] 0

1 Like

Andy, you’re a genius :medal_sports: (you’re clearly smarter than me, I haven’t thought about this one :exploding_head: )
I doubt we can get a shorter solution!


Actually, with this post, what I wanted is to introduce a new command :

$ gmic h lof

  lof:
      feature

    Return the list of specified features (separated by commas) for each image of the selection.

It basically extends the use of {img,feature} to a whole selection of images.
For instance, to get all image names from a selection:

sp lena,bottles,david,lion,tiger
e ${"lof n"}

which means that one possible solution of the brain teaser would be:

foo :
  sp lena,eagle,cat,dog
  lof n [^] => ${}

(but yes, I admit it’s not the best one now :slight_smile: ).

Command lof is able to extract any kind of feature, so for instance:

lof[selection] I(w/2,h/2)

will return the list of center pixel values of all images in the selection.

I was happy with it when I added it. So I wanted to share the info !

This format looks very handy indeed. That’s going to shorten quite a few of my scripts!

As for being a genius, I think it’s just a sign I spent too much time on g’mic :slight_smile:

1 Like

I forgot to tell that of course, lof stands for list of features :slight_smile:

@garagecoder , Sorry to say that, because what you found was indeed brillant :slight_smile: But after thinking a bit about it, I find it more like a flaw in G’MIC. There are no reasons why +rm and +mv wouldn’t apply a copymark to the names of the images added at the end of the list.

So, I’ve change the code of the interpreter to make this happen!

Hmm, I guess I can use lof command to replace these two commands:

#@cli rep_cr: eq. to 'rep_cr'. : (+)
rep_cr: rep_channels_range
#@cli rep_channels_range:
#@cli : Return the minimal and maximum values per channels in a image.\n
#@cli : Author: Reptorian.
rep_channels_range:
if $!>1 error "Cannot be used on more than 1 images!" fi
num_chans={s}

repeat $num_chans { shared[0] $> }

u {expr('p=1+(x>>1);x&1?iM#p:im#p;',$num_chans<<1)}

rm[-$num_chans--1]
#@cli rep_ol: eq. to 'rep_orientation_list'. : (+)
#@cli : Return dimensions of selected images.
rep_ol: rep_orientation_list
#@cli rep_orientation_list:
#@cli : Return orientation of selected images.
#@cli : (eq. to rep_ol).
rep_orientation_list: u {expr('h#x>w#x',$!)}

Not sure for first one, but definitely for second one.

I was expecting that, makes sense to me too. I’ve never had a need to use that “feature” until now anyway, no harm done :slight_smile: