G'MIC exercises

Not sure what you want to do that, but one simple solution is to put an eval with the const definitions to test and use onfail to check for an error.

I do not understand

but maybe:

import gmic
number_three = 33 # your scalar value here
gmic.run("your gmic commands " + number_three + "rest of commands")

or

import gmic
import struct
number_three = 33
img = gmic.GmicImage(struct.pack('1f', number_three), 1,1)
gmic.run("somecommand", img)

or

import gmic
import numpy
number_three = 33
img = gmic.GmicImage.from_numpy(numpy.array([33]))
gmic.run("somecommand", img)

I made a thread about not being able to install gmic-py.


Wasn’t there a command that gives me the crop value point instead of cropping? I forgot about that.

My goal is to autocrop using value greater than 0.

Or in greater amount of details, crop images automatically to the value of alpha.

Maybe autocrop_coords?

1 Like

I believe we had this conversation before. autocrop_coords is neat. I hacked it to merge tiles for the purpose of connecting continuous defects within my clean photo filter.

How to escape a single slash within string?

cli_command_text="+rep_lavander_binary_map "$pw2size,$formula_mode,$bin_a,$bin_b,$6,"\\"

The above shows double slash in GUI.

Ah yes:

This works.

Yes, when stuck use entity numbers.

1 Like

What about this case?

rep_test_2:
test_str=string\"
('$test_str')
replace_str. \",{`92`}\"
echo {t} rm.

Why isn’t {`92`} working here?

It doesn’t look right but what were you expecting or trying to do?

Change quote mark into slash and quote mark. I fixed my script last night. Doesn’t do this, but it works.

I gave it a try but it seems to catch part of the echo message of the command, complaining about string of e[^-1] "Replace string '$1' by string '${2--1}' in image$?.". I looked further into it using the companion replace command and it seems to be caught at with from e[^-1] "Replace pixel values $1 with $2 in image$?." Strange.

How to solve this?

Basically, what I expect is 0 if ‘-’ is used as input.

$ rep_test -

rep_test:
m={v='$1'=='-';v?0:($1>=0?$1+1:$1);}

echo $m

Output:

C:\Windows\System32>gmic rep_test -
[gmic]-0./ Start G'MIC interpreter.
[gmic] *** Error in ./rep_test_2/ (file 'C:\Users\User\AppData\Roaming\user.gmic', line #1449) *** Item substitution '{v='-'=='-';v?0:(->=0?(int(-)%3)+1:-(int(abs(-))%3+1));}': Unrecognized item '-' in expression 'v?0:(->=0'.

What I’m looking is for a one-liner solution.

Solution after some time:

{v='$1'=='-';n=isint($1)?$1:0;v?0:(n>=0?n+1:n);}

It’s because:

replace_str. \",{`92`}\"

is replaced by

replace_str. \",\\"

which is already something wrong, as the string \\" is not really valid (the double quotes are not escaped). You’d like to have \\\" instead.

The correct way is:

replace_str. \",\\\"

Now your image contains the ascii codes: 115,116,114,105,110,103,92,34
(the two last ascii codes correspond to characters \ and ").

But beware, when you use command echo, the string is first “unbackslashed”, meaning e.g. that \n is replaced by a LINEFEED (ascii code 10).
Here In particular, it means the ascii sequence \" (given by {t}) will display only the character " .
So you see string" as a result, and think something is wrong.

If you use : echo {/{t}} instead (which means : show me the backslashed version of {t}), then you get what you are looking for:

rep_test_2 :
  test_str=string\"
  ('$test_str')
  replace_str. \",\\\"
  echo {/{t}} rm.

then:

$ gmic rep_test_2
[gmic]-0./ Start G'MIC interpreter.
[gmic]-1./rep_test_2/ string\"
[gmic]-0./ End G'MIC interpreter.

Note that it is definitely not easy to get all these steps right when we output or parse text data with G’MIC. A few rules to keep in mind:

  • Command echo and substitution expression {'string'} always do a first pass to “convert” the input string, meaning backslash characters are converted to their ascii code counterpart (\n10, \\\ , etc.).
  • When you use replace_str , do not hesitate to see what happens before/after by displaying the corresponding image that contained the string.
1 Like

Is there a way to make fill use vector-based calculation here and speed it up?

This is slight faster than current version of rep_bitplane_shuffle. It doesn’t end in vector, but scalar.

Note: You can use ${rep_extract_permutation_order\ 8,23459} as $2 as this will result in permutation of integer number spaced by one starting from 0.

#@cli new_rep_bitplane_shuffle: direction={ 0=backward | 1=forward },index_0...index_inf
#@cli : Bit Plane shuffle according to the order of index specified by user.
new_rep_bitplane_shuffle:
num_of_ind,direction={$#-1},{$1&1}
tv=[{expr('x',$num_of_ind)}]
order=[${2--1}]

if $#==2 error insuf_args fi
if sort($order)!=$tv error inv_ind_args fi
if $order==$tv return fi

if !$direction order=[${rep_inverse_permutation\ ${2--1}}]
fi

f ":begin(
  const number_of_bits=$num_of_ind;
  order="$order";
  tv=expr('x',number_of_bits);
 );
 sum(((i>>tv)&1)<<order);"

The current version already runs quick for me. Have you tried a simple lookup table approach?

You could then pre-calculate all swapped patterns for all numbers and store in an image or vector. Then your input i pixel would just be a direct index into that lookup image, no find required. It should be fast for very large images then.

Edit: in fact that way you could just warp the lookup…

1 Like

Different subject… is there already a fast command to get a convolution kernel back from source/target comparison?

I had a go with gcd_solve_kernel which I just added, but get the feeling there must be an easier way.
gmic sp wall +laplacian +gcd_solve_kernel.. .
solvekernel

Your solution works really good:

C:\Windows\System32>gmic sp dog tic rep_bitplane_shuffle 8,${rep_extract_permutation_order\ 8,23459} toc
[gmic]-0./ Start G'MIC interpreter.
[gmic]-1./ Input sample image 'dog' (1 image 1024x685x1x3).
[gmic]-1./ Initialize timer.
[gmic]-1./ Elapsed time: 0.035 s.
[gmic]-1./ Display image [0] = 'dog'.
[0] = 'dog':
  size = (1024,685,1,3) [8 Mio of float32].
  data = (208,216,208,216,208,216,216,208,208,216,216,208,(...),220,85,85,85,220,220,73,73,73,220,220,220).
  min = 1, max = 254, mean = 124.926, std = 70.271, coords_min = (405,181,0,1), coords_max = (496,88,0,2).
[gmic]-1./ End G'MIC interpreter.

C:\Windows\System32>gmic sp dog tic new_rep_bitplane_shuffle 8,${rep_extract_permutation_order\ 8,23459} toc
[gmic]-0./ Start G'MIC interpreter.
[gmic]-1./ Input sample image 'dog' (1 image 1024x685x1x3).
[gmic]-1./ Initialize timer.
[gmic]-1./ Elapsed time: 0.01 s.
[gmic]-1./ Display image [0] = 'dog'.
[0] = 'dog':
  size = (1024,685,1,3) [8 Mio of float32].
  data = (208,216,208,216,208,216,216,208,208,216,216,208,(...),220,85,85,85,220,220,73,73,73,220,220,220).
  min = 1, max = 254, mean = 124.926, std = 70.271, coords_min = (405,181,0,1), coords_max = (496,88,0,2).
[gmic]-1./ End G'MIC interpreter.

Though, I don’t get about the warp thing.

It’s just another way of getting a value from another image via an index. For example:
gmic 10,1,1,1,u +f[0] x f. "i(#0,i)"
is the same as
gmic 10,1,1,1,u +f[0] x warp. [0]

warp gets me this result:

#@cli new_rep_bitplane_shuffle: direction={ 0=backward | 1=forward },index_0...index_inf
#@cli : Bit Plane shuffle according to the order of index specified by user.
new_rep_bitplane_shuffle:
num_of_ind,direction={$#-1},{$1&1}
whole_numbers=[{expr('x',$num_of_ind)}]
order=[${2--1}]

if $#==2 error insuf_args fi
if sort($order)!=$whole_numbers error inv_ind_args fi
if $order==$whole_numbers return fi

if !$direction order=[${rep_inverse_permutation\ ${2--1}}] fi

{2^$num_of_ind},1,1,1,sum((x>>$whole_numbers&1)<<$order);

warp[^-1] [-1]

rm.

It will require some tweaks; in default mode warp treats each pixel as a vector index [x,y,z] of where to pull a value from. Then it will also take the entire vector (e.g. [R,G,B]) from that point.

Example: create an image with a coloured point, create a second image with an [x,y,z] index to that point, then use warp to retrieve the [R,G,B] from it:
gmic 10,10,1,3 = 345,3,3,0,0 = 456,3,3,0,2 "(3^3^0)" +warp.. .

The command may be called warp, but I often use it for fast pixel retrieval via an index.