Processing many files - GreycLab/gmic-community GitHub Wiki

Introduction

Since G'mic loads files in memory before processing them, if you have a large number of files to process, you may not have enough memory to do it. In that case, the trick is to load an image, process it and remove it from memory before loading the next one. Here is how you can do it.

Let's say you have two directories called 'original' and 'processed'. The first contains JPEG files, that you want to turn gray and put in the second one.

  1. Use a shell script (linux)

The bash script below creates a grey version of all the files finishing with .jpg in the directory called 'original' and output them in the directory called 'processed':

cd original
for i in *jpg
do
gmic $i -to_gray -o ../processed/$i
done

The bash script below uses the thrid row to read custom command from a file named CommandFile. Custom command -custom_command specifed by the command file is applied to all tif files in current directory. Files are outputted with prefix tmp_. Fourth row reads intermediary tmp_ ouputs and saves the files in data type ushort as 16 bit tiff images with prefix Processed_. Fifth row removes intermediary tmp_ files.

for i in *tif
do
gmic -m [CommandFile] $i -[custom_command] -outputp tmp_
gmic tmp_$i -o Processed_$i,ushort
rm ./tmp_$i
done

for this to work the shell script should be run from working directory and working directory should contain both the custom command file, shell script and tiff tiles to process.

  1. Use command -apply_files (linux)

It exists a G'mic command that does it too, but it is a bit tricky.

The script below goes into the directory called 'processed', then, the G'mic command turns to gray the files finishing with .jpg in the directory called 'original' and output them in the current directory, adding the prefix 'gray_' to the name:

cd processed
gmic -apply_files \"-to_gray\",\" ../original/*.jpg \",gray_

There are two things to care about:

  1. you should put " before and after the command to execute, especially if it contains spaces;

  2. you should put " before and after the file list but also a space after the first " and before the second ". And filenames can't contain spaces.

  3. Use a batch file (windows)


The batch file below creates a grey version of all the files finishing with .jpg in the current directory and outputs them in the sub-directory called 'processed':

MD processed
FOR %%a in (*.jpg) DO gmic -i %%a -to_gray -o processed\%%a
PAUSE

If you want to also load the latest gmic update file you will need to make sure it's either in the same directory as you're running the batch file from or put the full path to it in the command. For example:

MD processed
FOR %%a in (*.jpg) DO gmic -m c:\gmic\update1585.gmic -i %%a -to_gray -o processed\%%a
PAUSE