Feature request 'Reorder List'

Perhaps I have missed something in the reference, but I was playing and I wanted to reorder a large image list painstakingly invoking move. I made a custom command to reorder the list based on a string input. The string is the index numbers in the images in the new list order. For example:

reorder_list 0,2,4,6,8,1,3,5,7

moves every second image to the end of the list.

I thought this would make a useful addition to GMIC but I am not familiar enough with the cli and dealing with strings to make a robust and elegant command.

Any takers?

I think I have cracked it, though I should have used something other than the name to store the order., but I ran out of time.

gmic sp tiger,apples,david,wall,chick,colorful foo_reorder_ 0,2,4,1,3,5

afre_reorder:
  ($*) repeat $!-1 nm[$>] {-1,i($>)} done rm.
  repeat 2 repeat $! mv[$>] {$>,b} done done

PPS Couldn’t sleep until I fixed the problem. :nerd_face: Order is now stored along the z-axis. Submitted a PR for afre_reorder.

1 Like

Sorry to keep you up.

I took your idea about naming images and came up with this.

#@gui reorder_list: reorder_list, reorder_list
#@gui : order = text (0,"1,2,3,4,8,7,6,5,0)

reorder_list:

#generate sample images
-rm
-repeat 9
sample $>
-text_outline[$>] $>,0,0,50%,1,1,255 # make it obvious which image is which
-done

($1) #enter new order as image

-repeat {$!-1}  # name images as letters so sorting alphabetcally will give the new order
-name[{-1,i($>)}] {`[{97+$>}]`}
-done

-rm[-1] #remove image of new order values

-repeat {$!}  #move 'a'...'N' to end of list until 'a' becomes the first image
image_name={`[{97+$>}]`}
-move[$image_name] {$!}
-done

Learnt some things doing that. :slight_smile:

This suits my needs but to be useful for everybody a command like this would need to handle issues like having repted values in the new order etc.

Correct: my current implementation in the PR doesn’t account for repeated values yet (e.g., 0,3,5,4,3,1), but it can take any number of values (doesn’t have to match the number of images) and can handle skipped values in the counting sequence (e.g., 0,3,5,4,7,1).

Why not using mv[1--1:2] $! in this case ?
Also command sort_list may be useful for custom sorting.

Incorporated sort_list in afre_reorder (submitted a PR). It deals with duplicate values but removes the possibility of accepting a numbered list of any length, so I had to reintroduce the feature.

PS My laptop just crashed; it has been like this for the past several years. Fortunately, my changes were committed beforehand. :sweat_smile:  (If anyone has a laptop to spare…)

I think I have solved this problem (Slow though):

$ tic sp cat,lena,dog rep_reorder 2,0,1 toc
rep_reorder:

list_names={0,n}

repeat $!-1 {
 p={$>+1}
 
 list_names.=,{$p,n}
}

new_order=${rep_inverse_permutation\ $*}

name[0--1] $new_order

sort_list +,n

($*)

repeat $!-1 {
 name[$>] ${arg\ {i(#-1,$>)}+1,$list_names}
}

rm.

Remarks: I am liking the permutation, combination, ncr things I made. Comes in handy.

Also, someone did point to me to a Python method of doing this (which doesn’t apply to G’MIC, but the method is what should be paid attention to):

def f(a):
    seq = [i for i in range(len(a))]

    seq_copy = [i for i in range(len(a))]

    for i, n in enumerate(a):
        seq_ = [s for j, s in enumerate(seq_copy[i:]) if j != n]

        seq = seq[:i] + [n] + seq_

    return seq