Some Python-Based script to make G'MIC scripting easier

I have decided to learn Python a bit to aid in speeding up coding in the G’MIC language, but I want to share these script.

These scripts utilize your clipboard, so all you need to do is copy your g’mic code snip and then execute script, and finally you will see the output.

Find Unique Arguments in dynamic gui arguments
import pyperclip as pc
import re
import numpy as np

# Copy u lines used for dynamic gui before executing!
# This file is to output all unique end arguments in u {$1}\.... series

def listToString(s):
    str1=""
    
    for ele in s:
        str1+=ele
    return str1

gmic_str_paste=pc.paste()
gmic_str=str(gmic_str_paste)
Lines=gmic_str.splitlines()

string_element=[]

for t in Lines:
    current_string=t.split('"')
    if len(current_string)==3:
        bracket_str=current_string[2]
        if len(bracket_str)>2:
            extracted_str=re.findall(r'\{.*?\}', bracket_str)
            string_element.append(str(extracted_str))

uniq_set=set([element for element in string_element if string_element.count(element)>1])
arr_uniq_set=list(uniq_set)

ordered_element=[]
position=[]

for i in arr_uniq_set:
    set_str=str(i)
    m=0
    for j in string_element:
        m=m+1
        if i==j:
            break
    position.append(m)
    
position.sort()

for i in position:
    ordered_element.append(string_element[i])
    
result=str(ordered_element)

result=re.sub('\[','',result)
result=re.sub(']','',result)
result=re.sub('\"','',result)
result=re.sub('\'','',result)
result=re.sub(' ','',result)
result=re.sub(',','',result)

print(result)
Increment all numbers next to '$'
import pyperclip as pc

#Copy codes contain $number before executing!

a = pc.paste()
sa=str(a)
b=""
tn=''

temp_num=0
increment_num=int(input("Insert Number: "))
max_num=int(input("Increment Number Equal or Greater than: "))

print('')

skip_digit=bool(True)

p=0
q=1
for c in sa:
    if c=='$':
        b=b+c
        skip_digit=bool(True)
        if a[p+q].isdigit():
            while a[p+q].isdigit():
                tn=tn+a[p+q]
                q=q+1
            if int(tn) >= max_num:
                temp_num=int(tn)+increment_num
            else:
                temp_num=int(tn)
            b=b+str(temp_num)
            tn=""
            q=1
    else:
        if not c.isdigit():
            skip_digit=bool(False)
        if c.isdigit():
            if not skip_digit:
                b=b+c
        else:
            b=b+c
    p=p+1
print(b)
Replace if,elif statement with arg-style code
import pyperclip as pc

#Copy just the if elif portion of your code before executing!

a = pc.paste()
sa=str(a)
vs=sa.splitlines()
b=[]
m=bool(False)

for c in vs:
    init_pos=c.find("if ")+2
    if init_pos!=-1 or test_end!=-1:
        end_pos=len(c)
        cut_after_if=c[init_pos:end_pos:1]
        start_eq_pos=cut_after_if.find("==")
        if start_eq_pos != -1:
            eq_string=cut_after_if[start_eq_pos:len(cut_after_if):1]
            space_string_pos=eq_string.find(" ")
            if space_string_pos != -1:
                after_init_space=eq_string[space_string_pos:len(eq_string):1]
                append_str=after_init_space.lstrip()
                b.append(append_str)
        else:
            m=bool(True)
            print("Invalid!")
            break            
    else:
        m=bool(True)
        print("Invalid!")
        break
    
if not m:
    init_end_str=""
    for i in b:
        init_end_str=init_end_str+i+","
    init_end_str=init_end_str[0:len(init_end_str)-1:1]
    print("Replace $existing_variable with a existing variable to make it work.\n")
    print("${arg\ $existing_variable,"+init_end_str+"}")

I had improve 'Increment all numbers next to ‘$’. Now, you can define which numbers equal to or greater than to increment. This is much easier than manual edit. :slight_smile:

When you write a command with a lot of arguments, a good practice is to name them before everything else, then use only the named arguments later in the code, so you are sure you won’t miss something. And you finally add a new argument in your command, it’s not a problem, you’ll be able to insert it at any place you want.

For instance:

foo :  skip "${1=0},${2=10},${3=xy}"
  smoothness,sharpness,axes=${1-3}
  blur $smoothness
  sharpen $sharpness
  mirror $axes

Now imagine I want to insert a new argument angle just before axes, then I only had to complete the two first lines of code and eventually add a few new lines, but I don’t have to “shift” the argument numbers in all lines of code that would need it:

foo : skip "${1=0},${2=10},${3=0},${4=xy}"
  smoothness,sharpness,angle,axes=${1-4}
  blur $smoothness
  sharpen $sharpness
  rotate $angle
  mirror $axes
1 Like