Processing batches of images

First, thanks to all of you, who helped getting me started so far. It is an interesting journey go write the first scripts.
I need help with the following, I would like to write a function that takes a number of images as an input. It then iterates over one image after the other and appends them. When certain conditions are met, I would like the function to scale the assembled image that we have so far to a bounding box and output the assembled image. It then processes the next images in the same mater. So, if we have 10 inout images and the condition are met after images 3 and 7, we would get 3 new images, the first containing images 1-2, the second containing images 3-6 and the last image containing 7-10. Note that I don’t know in advance when the conditions are met at the beginning. I only know in the course of reading the images.
In a traditional programming language, it would not be difficult at all but how to do this effectively with a pipeline?

And one thing to add: There might be a couple of 100 images that need to be processed.

precondition: You have -input n images. For sake of discussion, let image variance iv==0 be the “certain condition” that distinguishes the images that partition your image list into separate groups. An image with no variance at all among constituent pixels is likely a black image. Perhaps these are your third and seventh images mentioned In your preamble. See Mathematical expressions for the various per-image metrics that G’MIC maintains. Familiarize yourself with the name command; it is the principle means to gather together images into various groups or “collections”; all images in a particular group have a common name — and perhaps a common condition, if the scriptwriter arranges this as so. Then something like:

groupid=0
foreach {
   if iv==0
      groupid+=1
   fi
   name. "Group_"$groupid
}

That is, whenever an image with the distinguishing characteristic is noticed in the image stream, it advances the groupid count. The image with the distinguishing characteristic is the first to be enrolled in the new group. That image, and all others up to (but not including) the next image with the distinguishing characteristic, are enrolled in this group. One use case for this could be the frames of a video, with all the scenes separated by blank “sentry images.”

Often what we loosely term as “naming images” is more closely modeled as putting a single image in a named group. Usually, the loose terminology is harmless, but can lead to soft mental bugs.

Haven’t tested the proposed code and it is time to walk the puppy dog. Good luck!

@Helmut_Kaufmann , as it seems you want to process a lot of images, a quite good approach would be to pass the input images image list as a regular expression to your command, rather than loading all images in memory before running the command. If you have hundreds of images with good resolutions to load, that would be painful for your computer.
What I can propose right now is something like this:

foo :
  $=arg repeat $# { files 3,${arg{1+$>}} files.=$sep${} sep=, } # Retrieve list of image files

  # Loop over all image files.
  repeat narg($files) {
    arg0 $>,$files file=${}
    e " - File: "$file
    i $file # Input new image

    do_append:=u<0.1 # <- Criterion for determining if images must be appended or not

    if $do_append
      a x
      filename out.jpg,$> filename=${}
      o $filename
      rm
    fi
  }

Which can be called from the command line like this:

$ gmic foo \*.jpg

Here, the criterion I chose is simply a threshold on a random value, but feel free to change it to something more meaningful :slight_smile:

1 Like

@Helmut_Kaufmann I don’t know your level with G’mic (I’m a beginner too, and not even a dev!) but it may seem a bit cryptic to you, so here’s a little “translation” for a few shortcuts used here :

:= launches the math parser. You could also use do_append={ u<0.1 }
e shortcut for echo
a short for append
u here it is a variable : random float between 0 and 1. there is also a command u(n) which gives you a float between 0 and n. Or u(n,N), which I think you can guess.
o a.k.a. output
i short for input, but you can also use nothing.

EDIT: forgot these ones below…
$> Is a variable that increments with each loop, starting from 0. $< does the same backwards.
${} is the output/status of the last command? (not so sure about this one if someone can confirm)

Also, the best command is h, short for help : gmic h a. Using autocompletion will give you a list of all commands starting with a.

Let’s hope I’m not wrong here :slight_smile:

1 Like

Yes. ${} returns whatever status the previous command set (via status).
So let:

foo :
   status "I am a great command!"

Then:

 $ gmic -command foo.gmic run 'foo echo ${}'
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Import commands from file 'foo.gmic', with debug info (1 new, total: 4661).
I am a great command!
[gmic]-0./ End G'MIC interpreter.
1 Like

Good afternoon @prawnsushi, @David_Tschumperle and @grosgood . Thanks for your replies. I will give it a try shortly. And perhaps will try and write some 101 with my experiences.

Good morning… Works! Will make some adjustments and then come back to you. But now, taking one of my kids to the fencing tournament.

1 Like