Archive for September, 2010

G’mic: The command line behind the Gimp plug-in

0-Expendable introduction

The G’mic plug-in for Gimp is based on the G’mic language. Each filter proposed in the plug-in corresponds to a G’mic command line, each setting is just a parameter of that command line. And command lines are great because :

  1. it is a way to remember settings and filter sequences,
  2. it allows to (almost) effortlessly apply on a large set of images what you just did graphically.

Anything said below supposes that you run some kind of linux, but the spirit of it apply under other OS too.

1-How to get the command lines?

To have the plug-in tells you what you do graphically in the command line language, you just have to start Gimp from a terminal and to set it to “verbose mode”.

To start from a terminal, just open a terminal and type :

gimp

To set it to “verbose mode”, once you have open the plug-in, set the “ouput messages…” menu on the left to “Verbose”.

From now on, you’ll see text appearing in the terminal each time G’mic adapt the preview to your new setting and each time you apply a filter on your image.


Image from PhotoComiX, CC-by-nc

2-What it looks like?

Two kind of lines appear in the terminal. The ones telling what command is used to create the preview (containing the /preview/ sequence) and the ones telling what command is really applied on the picture (containing the /apply/ sequence). You are probably only interested in the last kind.

For example, the line:

[gmic_gimp]./apply/ -v -99 -gimp_mix_rgb 1,0,0,1,0,0,1,0,0,0,2,0

tells that the command called was gimp_mix_rgb and all the numbers behind correspond to the settings.

3-What to do with it?

You could just be happy to know what happens and to be able to keep everything in a text file with a simple copy/paste, but you could also want to apply it in a terminal. Just go to the directory where your file (say image.jpg) is and type:

gmic image.jpg -gimp_mix_rgb 1,0,0,1,0,0,1,0,8.5,0,2,0

Your processed image will be displayed, but not saved.
To save it instead of displaying it:

gmic image.jpg -gimp_mix_rgb 1,0,0,1,0,0,1,0,8.5,0,2,0 -o processed_image.jpg

If you have many images (whose names finish by jpg) that you want to be processed, a script like that should do it:

for i in *jpg
do
gmic $i -gimp_mix_rgb 1,0,0,1,0,0,1,0,8.5,0,2,0 -o processed_$i
done

Image Wikimedia

4-By the way

If you only want the “interested” lines to appear in your terminal, you should run Gimp with this command instead:

gimp |grep apply
Advertisements

G’mic Command Line: First steps

0-Unnecessary introduction

Before being a popular Gimp plugin, G’mic is a command line tool devoted to image manipulation. This tutorial assumes that commands are given on a Unix shell command line. Windows user will have to adapt, and might want to install MinGW to be more comfortable with command line use. It is also possible to test notions explained here below in a script built for the plug-in.

Every G’mic command line formally looks that way:

gmic -instruction1 arguments -instruction2 arguments etc...

Everything is explained in the official reference page. But anyone human would need some more developed explanations. Here are some.

1-A basic example

Before applying any treatment on an image, G’mic has to load it with the -input instruction. After any treatment being applied, you might want to unload the final result on your hard disk with the -output instruction.

Let’s use a basic example. To rotate 90° an image called image.jpg and get the result as a PNG file called image.png, you’d use the following command:

gmic -input image.jpg -rotate 90 -output image.png

You’ll notice that G’mic returns in your terminal the details of all the operations that occurred. For the previous command line, you should have had something like:

[gmic]-0./ Start G'MIC instance.
[gmic]-0./ Input file 'image.jpg' at position [0] (1 image 512x384x1x3).
[gmic]-1./ Rotate image [0] of 90 deg., black borders and linear interpolation.
[gmic]-1./ Output image [0] as file 'image.png'.
[gmic]-1./ End G'MIC instance.

So you won’t be able to say that you didn’t know what happened.

 

From Rantz, CC-by-nc-sa

2-Aliases

In order to save typing time, the most common instructions have aliases. For example, -input can be replaced by -i and -output by -o. So, our basic example can be rewritten as:

gmic -i image.jpg -rotate 90 -o image.png

In fact, the -input instruction is that common that an even faster way to invoke it has been implemented in G’mic: just type the image name directly without any instruction before. Indeed, the next command line is the exact equivalent to the previous one:

gmic image.jpg -rotate 90 -o image.png

Don’t expect more, this is as fast as it can get.

3-Details about -output

3.1-Formats

You can get an output anytime in the command line (before or after an operation) and the name of the output file determine the format. For instance, with the following command line, you get 3 images in 3 different formats, the BMP one has not undergone any rotation, the other ones have.

gmic image.jpg -o image.bmp -rotate 90 -o image.png -o image.tga

By the way, by default, G’mic saves JPEG files with quality 100, which is probably more than what you’d want. To save at quality 95, use -o image.jpg,95

3.2-Display

If you don’t specify any -output (or -o) instruction or if you explicitly give the -display instruction, then G’mic will display your image on the screen. The 2 next command lines do the same thing: they display the image after a 45° rotation:

gmic image.jpg -rotate 45
gmic image.jpg -rotate 45 -display

press ‘q’ to quit the display screen.

There is actually a lot to say about the display screen, but it won’t be said now.

4-The arguments of the instruction

Since the beginning, the image is rotated, just rotated, but like most G’mic instructions, the -rotate instruction possesses many arguments to rotate in a specific way. If you look at the official reference page, you’ll find the lines explaining how -rotate works. It begins by:

-rotate angle,_borders,_interpolation,_cx[%],_cy[%],_zoom

This means that -rotate may have until 6 arguments. The first one (angle) has to be specified. The other ones begin by an underscore, it means that they are optional. Just make some tests and you’ll understand how it works. For example, try these lines:

gmic image.jpg -rotate 30,1
gmic image.jpg -rotate 30,0,0,50%,50%
gmic image.jpg -rotate 30,0,0,0%,0%,0.5

You can also get that off-line with man gmic or gmic -h.

5-dealing with several images

5.1-Many just like one

You can deal with many images at a time, just load them all. For example, the following command lines load the images and display them:

gmic image1.jpg image2.jpg image3.jpg
gmic image*.jpg

If you write your command lines as in the previous chapter, then G’mic instructions are applied on every loaded images. By using the next command line:

gmic image*.jpg -rotate 90 -o image.png

you tell G’mic to rotate all the image*.jpg files and to save them as a png file. But it won’t save several images in one file called image.png, it will name them image_000001.png, image_000002.png, image_000003.png, etc.

5.2-Restricting an instruction to only some images

You can specify on what images applying an instruction by adding the image number, starting by 0 under brackets:

gmic image*.jpg -rotate[0] 90

only rotates the first loaded image.

gmic image*.jpg -o[1] image.png

saves only the second one as image.png.

If you use negative numbers, you start by the last one:

gmic image*.jpg -rotate[-1] 90

only rotates the last loaded image.

gmic image*.jpg -rotate[-2] 90

only rotates the second last one.
You can also select a set of images to manipulate:

gmic image*.jpg -rotate[0,2,-1] 90 rotates the first, the third and the last one.
gmic image*.jpg -rotate[0-9] 90 rotates the 10 first images.
gmic image*.jpg -rotate[0-9:3] 90 rotates every 3 images starting from the first one and finishing at the tenth one. It means the first, the fourth, the seventh and the tenth one.
gmic image*.jpg -rotate[0,-4--1] 90 rotates the first and the 4 last ones.
gmic image*.jpg -rotate[50%-100%] 90 rotates the second half of the image list.
gmic image*.jpg -rotate[0,50%--1] 90 rotates the first image and the second half of the image list.

Modified from Rantz, CC-by-nc-sa

5.3-Keeping the original with double dash

By default, an instruction replace the original image by the processed one. There is a way to keep the original: use a double dash (- -). Ex:

gmic image.jpg --rotate 90

produces 2 images: the untouched image.jpg and the rotated image.jpg. More details are avalaible.

5.4-Manipulating the image list

As you have seen in the previous chapter, the image order is important. You can change that order at any time by using -move or -reverse:

gmic image*.jpg -move[50%-100%] 0

moves the second half of the image list at the beginning

gmic image*.jpg -reverse

reverses the order of all the images.
You can also keep only some of the images in your list with -keep or get rid of some with -remove. If you feel lost sometime, check what G’mic returns in your terminal, it should tell you how many images are left.

6-Custom commands

Once you begin to play around with G’mic, command lines can become pretty long. A solution is to write things in a custom command file. Many custom commands already exist, you can study and learn from them. It would probably help to get some more explanation on the art of writing custom commands but you won’t find them here. All that will be said is that if you need to load your custom command file before using it.

This is done with the instruction -command or -m or even without any instruction if your file finishes by .gmic. So, the three followings lines do the same thing:

gmic -command my_custom_command_file.gmic image.jpg -my_command
gmic -m my_custom_command_file.gmic image.jpg -my_command
gmic my_custom_command_file.gmic image.jpg -my_command

7-Going further

Logically following this tutorial is that article. Then, to increase your G’mic culture, you should probably click on gallery’s images and to master the technique, don’t forget the custom command file.
You are also invited to share your problems and solutions at the forum.

From Rantz, CC-by-nc-sa