G'MIC exercises

What does this mean?

You can also prepend a hyphen - to a feature (that supports it) to flush the corresponding event immediately after reading its state (works for keys, mouse and window events).

Source: G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing - Substitution Rules

Another question:

Let’s say I want a string without a substring in it. How do I do it?

var1=part_test
var2=??? # Return _test using $var1

Right now, I have this code:

_$0_list_of_blending_modes=_$0_add,_$0_alpha,_$0_and,_$0_average,_$0_burn,_$0_darken,_$0_difference,_$0_divide,_$0_dodge,_$0_exclusion,_$0_freeze,_$0_grainextract,_$0_grainmerge,_$0_hardlight,_$0_hardmix,_$0_interpolation,_$0_lighten,_$0_linearburn,_$0_linearlight,_$0_multiply,_$0_normal,_$0_negation,_$0_or,_$0_overlay,_$0_pinlight,_$0_reflect,_$0_screen,_$0_shapeaverage,_$0_softburn,_$0_softdodge,_$0_softlight,_$0_stamp,_$0_subtract,_$0_vividlight,_$0_xor
num_of_blending_modes={narg(${_$0_list_of_blending_modes})}
${_$0_list_of_blending_modes}={expr('x',$num_of_blending_modes)}

And I want to remove _$0_ within all variables of _$0_list_of_blending_modes without changing the above code.

EDIT:

My answer seem to be adding these two lines:

('${_$0_list_of_blending_modes}')
replace_str. _$0_,"" _$0_list_of_blending_modes={t} rm.

How to preview an animation of 3D image without exporting to .mp4?

Can g’mic do real time animations?
Maybe because I think there are some in the demo command. Maybe you can check the code?

I saw this command in the scripting tutorial/ fun part… G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing - Funny Oneliners

torus3d 100,80,36,24 s3d f.. 1+int(y/3) a y repeat 20 800,800 +r3d[0] 0,0,1,$> pose3d. 0.88,-0.08,-0.46,0,0.36,0.75,0.56,0,0.3,-0.66,0.7,0 j3d.. .,50%,50%,-500,1,2 rm. f. "boundary=1;i==j(-1)&&i==j(0,-1)?0:255" +b. 6 equalize. 256 *. 1.7 max[-2,-1] done rm[0] r2dx 50% n 0,255

…but not sure if it does animation from the command line (?)

Here, try reading this thread: How to export animation via gmic cli [SOLVED] - #8 by grosgood

The closest to real time animation is to use window command. You did it before, I think.

Ditto.

See “Simple particle system” in @David_Tschumperle 's Post 171, Big Bad Thread of Silly Questions. Look at the code after the #Render animation comment.

Well, I sometimes use window to check my loops and it sometimes does animation.
But anyway, wasn’t that what @Reptorian was asking here? :man_shrugging:

I think I have it.

For now, it’ll do for me, but it still needs way, way, way more work

Here it is:

$ 100,100,100,4,v=norm(x-49.5,y-49.5,z-49.5)/50;v<1?lerp([0,1,1,255],[360,1,1,255],v); hsv2rgb rep_preview_3d_animation
#@cli rep_preview_3d_animation: '_fps>0'
#@cli : Previews 3D images as animations via windows.
rep_preview_3d_animation:
skip ${1=30}
check "$!<10&&$1>0&&isint($1)"

delay_time={int(1000/$1)}

num_of_windows,screen_w,screen_h=$!,{*,u},{*,v}
+store temp

scaled_screen_w,scaled_screen_h={int([$screen_w,$screen_h]*.9)}

foreach { 
 if w>$scaled_screen_w||h>$scaled_screen_h 
  r2din $scaled_screen_w,$scaled_screen_h,3 
 fi
 +crop 0,0,0,0,100%,100%,0,100%
 drgba.
 w$>. -1,-1,0,0,50%,50%  rm.
}

$!,1,1,2,[1,d#x] => frames_data

do
 repeat $num_of_windows {
  current_frame,number_of_frames:=I[#$frames_data,$>]
  +crop[$>] 0,0,$current_frame,0,100%,100%,$current_frame,100%
  drgba.
  w$>. -1,-1,0,0 rm.
  set. {($current_frame+1)%$number_of_frames},$>
 }
 wait $delay_time
while {*}

rm $temp

Why not using animate3d ?

Didn’t really know that’s a thing. I keep getting errors with it though, so I don’t know if that command is suitable for my needs.

C:\Windows\System32>gmic 100,100,100,4,v=norm(x-49.5,y-49.5,z-49.5)/50;v"<"1?lerp([0,1,1,255],[360,1,1,255],v); hsv2rgb animate3d 1,1,1,1,1,1
[gmic]./ Start G'MIC interpreter (v.3.3.4).
[gmic]./ Input image at position 0, with values 'v=norm(x-49.5,y-49.5,z-49.5)/50;v<1?lerp([0,1,1,255],[360,1,1,255],v);' (1 image 100x100x100x4).
[gmic]./ Convert color representation of image [0] from HSV to RGB.
[gmic]./ Generate 3D animation frames from 3D object [0], with 1 frames, angle steps (1,1,1), zoom factor 1 and 1% fake shadow.
[gmic] *** Error in ./animate3d/*repeat/*local/ *** Command 'check': Expression '0' is false.

animate3d can be used for 3D-vector objects, not 3D volumetric images.
Also, it does generate frames of an animation (e.g. to save a video), this is not what you are looking for.

In your case, it seems you are talking about visualizing a 3D volumetric image in 3D.
There is multiple ways to do that, and all depend on the content of your volumetric image.

For your example, command pointcloud3d may be useful.
But remember, as your volumetric image is dense, you’ll see probably mostly the outer surface of your image, not the interior, unless you make your 3D object semi-opaque (opacity3d).

How can I get this to work?

$ m foo:continue repeat 1000 { echo 1 foo echo 2 }

I know why it won’t though.

And yes, I know of the single variable trick. Hmmm…

Right now, I’m suspecting that +pal code can be shortened with base642uint8, but without the feature idea I had in mind, it’s not possible. Or maybe I will create a new code copying from base642uint8 to find a workaround.

This won’t work because when you call foo , you’re entering a new command, and in this new command the continue has no sense (you’re not inside a loop in this new command).
That’s actually the difference between a command and a macro (macro functions is the things you can define in math expressions, but you cannot define such macros for a regular G’MIC pipeline).
And I’d say, fortunately so: If such a continue would have an effect on the “parent” loop, I imagine the nightmare for debugging things.

Yeah, I know that. Now, I tried testing a new approach to see if it would save time (only slightly), but all it saved number of characters from 128000 to 41000. Never mind.

How does one check if a command exist? I think I have a way to make cli_end, which is the opposite of cli_start.

If cli_end exists, then _display will execute it at the very start. This will very easily allow me to separate my user.gmic files entirely.

I wonder if parse_cli can find it?

Probably something as:

if ['$$cli_end']!=0 ... fi

(but works only for custom commands).

Thanks, that works. I inserted this under _display code at the very beginning:

if ['$$cli_end']!=0 cli_end fi

And added this to user.gmic

cli_end: blur 100   

And running $ sp cat will show me a blurred image. This definitely will allow me to just simply import .gmic script, and execute __main__ without having to type __main__ on the CLI if it exists on the imported .gmic script.

I’m not getting the same result here:

Python:

def compute_fft_simple(image):

    # Load the image and convert to grayscale
    img = image.convert('L')
    
    # Convert image to numpy array
    img_array = np.array(img)

    # Compute the 2-dimensional FFT
    fft_result = np.fft.fft2(img_array)
    magnitude_spectrum = np.abs(fft_result)

G’MIC

to_gray
+fft 
a[-2,-1] c 
100%,100%,100%,1,cabs(I#-1) 
rm.. log.

And yet, I think these output are the same with smaller images:

>>> import numpy
>>> a=numpy.mgrid[:5,:5][0]
>>> a
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4]])
>>> fft_result=numpy.fft.fft2(a)
>>> fft_result
array([[ 50.  +0.j        ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5+17.20477401j,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5 +4.0614962j ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5 -4.0614962j ,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ],
       [-12.5-17.20477401j,   0.  +0.j        ,   0.  +0.j        ,
          0.  +0.j        ,   0.  +0.j        ]])
>>> magnitude_spectrum=numpy.abs(fft_result)
>>> magnitude_spectrum
array([[50.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [21.26627021,  0.        ,  0.        ,  0.        ,  0.        ],
       [13.1432778 ,  0.        ,  0.        ,  0.        ,  0.        ],
       [13.1432778 ,  0.        ,  0.        ,  0.        ,  0.        ],
       [21.26627021,  0.        ,  0.        ,  0.        ,  0.        ]])
$ 5,5,1,1,y +fft a[-2,-1] c 100%,100%,100%,1,cabs(I#-1) rm..

What’s going on?