Introduction to Graphics Tools

From CSE330 Wiki

Jump to: navigation, search

Contents

[edit] GIMP

In this module, we will concentrate on GIMP (GNU Image Manipulation Program) [1]. As its name suggests, GIMP is a open source image manipulation tool. In addition to powerful internal functions, it comes with its own scripting language which lets the users to write the extentions. The scripting language is called Script-Fu which has a very similar syntax to Scheme [2]. In addition to Script-Fu, there are Perl and Python plug-ins to call internal Script-Fu procedures in the those languages.

[edit] Installing GIMP

You can download GIMP for several platforms at http://www.gimp.org. In debian, you can install it using

apt-get install gimp

Although 2.4 has more functionality, most of the available resources on the internet are for 2.2 or lower versions (there are slight changes in the functions between the versions). For this reason, you may want to download 2.2.

[edit] Script-Fu

Script-Fu is a procedural language developed for GIMP. As its a procedural language, Script-Fu requires most of the functions use lists to send and return data. The function calls are implemented as function-name followed by arguments.

(function-name arg1 arg2 ...)

for example

(+ 3 5) 

returns (8). You can set a variable to a value by set! instruction

(set! variable value)

value could be a constant, another variable, or a function return.

(set! result (+ 3 5))

The most recent version, 2.4 forbids the global variables and forces the local scoping through let* instruction.

This is done by

(let*   (
        (variable1 value)
        (variable2 value)
        )
       .....
       .....
)


Functions are defined using define instruction.

(define (function-name argument1 argument2 ...)
)

combined with let local scoping, usually the functions have the following structure

(define  (function-name argument1 argument2 ...)
 (let*
        (variable1 value)
        (variable2 value)
       .....
       .....
 )
)

[edit] GIMP Procedures

GIMP has built-in procedures (either built-in functions or Script-Fu programs). These can be browsed using Procedure Browser (it can be accessed through Xtns->Procedure Browser menu path). You can search for the procedures you are interested.

GIMP also provides as Script-Fu console so that you can call internal procedures through the console. This console is accessible through Xtns->Script-Fu->Script-Fu Console.

For example,

(gimp-image-new 100 100 RGB)

will create a 100x100 RGB image. This function returns the ID of the new image. later you can display this image with

(gimp-display-new 1) ;assuming 1 is the image ID

The initial image won't have any layer, you will need to create a new layer and add to your image. As typing from the console is not practical, GIMP provides you mechanisms to write your own programs and execute them.

[edit] Writing Script-Fu Programs

You can write your own Script-Fu programs. This is done by creating a text file with .scm extension in the scripts directory for GIMP. In Linux, this directory is located as .gimp-version/scripts where version is major and minor release number (2.2, 2.4 etc.).

In .scm file, you need to write your own functions and register at least one of them to the GIMP. The registered function then will be available in GIMP's procedure browser.

The registration is done by calling script-fu-register function. These function takes several arguments such as the name of the function to be registered, short cut key and short explanation, long help text, the creator, copyright, date, arguments to be passed to the the registered function etc..

For example, following Script-Fu creates an empty image

(define (script-fu-hello-world width height )

   (let* (
	   (img (car (gimp-image-new width height RGB))))
           (gimp-display-new  img)
    )
 )

(script-fu-register 
   "script-fu-hello-world"
   _"_simple hello world"
   "Creates a hello world text"
   "Burchan"
   "(c) 2007"
   "28/11/2007"
   ""
   SF-VALUE "Width:" "200"
   SF-VALUE "Height" "200"
)

You can call this function from the script-fu console using the function name and its parameters.

 (script-fu-hello-world 100 100)

Instead of using the console, you can also register your function to GIMP menus and call your function from the menu


(script-fu-menu-register "script-fu-hello-world"
			 _"<Toolbox>/Xtns/CSE-330")

The above code (placed inside the same file as your other functions), creates a new sub menu (CSE-330) under Xtns where your function will be displayed. When you access your new function, it will also display a window for you fill out the arguments. In the above case the arguments were

 SF-VALUE "Width:" "200"
 SF-VALUE "Height" "200"

They are just the integers with default values 200. When GIMP asks user to type the values, it will show the descriptions width and height. Other data types include SF-String,SF-Font, SF-Image etc. You can find the other types at http://wiki.osphoto.org/index.php/Script-fu_Dialog_box

Within your function definition, you can also call other gimp functions. For example, the following code creates a new image with a text typed by the user


(define (script-fu-hello-world width height txt)
   (let* ( 
	   (img (car (gimp-image-new width height RGB)));create image
 	   (layer (car (gimp-layer-new img width height RGB  "background-layer" 100 0))) ;create layer
	   )
        (gimp-image-add-layer img layer 0) ;add layer to image 
	(gimp-edit-fill layer 2) ;fill the layer with white color
	(gimp-palette-set-foreground '(255 0 0));set foreground
	(gimp-text-fontname img -1 10 100 txt -1 1 16  0 "Sans") ;type the user specified text at 10,100, add text a new layer, use anti-alliasing, use 16 px size and Sans font
	(gimp-image-flatten img) ;merge the layer and the text
        (gimp-display-new  img) ;display the image
        (gimp-file-save 1 img (car (gimp-image-get-active-drawable img)) "test.png" "") ;save the image
   )

 )

(script-fu-register 
   "script-fu-hello-world"
   _"_simple hello world"
   "Creates a hello world text"
   "Burchan"
   "(c) 2007"
   "28/11/2007"
   ""
   SF-VALUE "Width:" "200"
   SF-VALUE "Height" "200"
   SF-STRING "Message:" "Hello"
)
(script-fu-menu-register "script-fu-hello-world"
			 _"<Toolbox>/Xtns/CSE-330")


Note the '(car' function. As gimp procedures return lists in the form of (list-item1 list-item2 ....) even though the return list contains only one item, you need to access that item using car function which returns the head of the list.

[edit] Perl-Fu

Perl-fu is a GIMP plug-in that lets you use perl to access GIMP procedures. In order to use it, you need to install GIMP module for perl. The easiest way for debian is just install libgimp-perl

apt-get install libgimp-perl

Once you have Gimp module, you can then write your perl script for GIMP and call them from Gimp.

In order to use your scripts, you need to place them under GIMP's plug-ins directory. This directory is located at ~/gimp-version/plug-ins. It must be executable otherwise GIMP won't load it.

Similar to Script-Fu programs, you need to register your program. Perl-fu registration handles both function and menu registration.


Following Perl-Fu create a new image and writes hello

#!/usr/bin/perl
use Gimp ":auto";
use Gimp::Fu;


sub cse330_hello{
   $img=gimp_image_new(100,100,0);
   $layer=gimp_layer_new($img,40,40,0, "background",100,0);
   gimp_image_add_layer($img,$layer,0);
   gimp_palette_set_foreground([255,0,0]);
   gimp_text_fontname($img,-1,40,40,"hello",0,1,16,0,"Sans");
   gimp_image_flatten($img);

   return $img;
}
register 
   "cse300_hello",
   "say hello",
   "description goes here",
   "Burchan",
   "(c) Burchan",
   "11/28/2007",
   "<Toolbox>/Xtns/CSE330/hello",
   "*",
   [
   ],
   \&cse330_hello;

exit &main();


Note that GIMP functions are now accessed as if they are Perl functions. The names however are changed, i.e., the dashes (-) are replaced with underscores (_) to avoid syntax errors.

You need to return the images so that GIMP can display it. Th registration is done by register function call. This is very similar to script-fu-register. You need to give the function name, the extra information and parameters. The main difference is you need to give the address of the function so that perl-fu can call the correct function.

You can also pass the arguments to your perl function. For example,

 sub menu_item {
   ($txt)=@_;
   .....
 }
 register
   "menu_item",                 # fill in name
   "Create a menu item",  # a small description
   "Menu creator",       # a help text
   "Burchan",            # Your name
   "Burchan (c)",        # Your copyright
   "2007-11-28",              # Date
   "<Toolbox>/Xtns/CSE330/Make Menu",   # menu path
   "*",                       # Image types
   [
   [PF_STRING, "txt", "txt", "Hello world!"]
   ],
   \&menu_item;
exit main();


In this case, GIMP will ask about a text string, and that string is going to be passed to your function.

[edit] Useful Links

Script-Fu tutorial http://www.klaban.torun.pl/help/script-fu-tutorial/

Perl-Fu tutorial http://imagic.weizmann.ac.il/~dov/gimp/perl-tut.html (note that this tutorial is for 1.2, although the overall framework is similar, the function parameters are changed).

[edit] Teams for this module

Team 0: Thomas Jerry Moore, Simon Tam. Please use zoo.cse.wustl.edu

Team 1: Aaron Edward Jacobs, Adam Glenn Berger. Please use zoo.cse.wustl.edu

Team 2: Garrett F Eardley, Richard Zack Speyer. Please use zoo.cse.wustl.edu

Team 3: Zachary Robert Dwiel, Gurnish Sahni. Please use zoo.cse.wustl.edu

Team 4: Timothy Felix Trinidad, Jeffery Tyrone Nelson. Please use zoo.cse.wustl.edu

Team 5: Justin Alante McClain, David Cassey Schainker. Please use zoo.cse.wustl.edu

Team 6: Katherine T. Maschmeyer, David Kaminsky. Please use zoo.cse.wustl.edu

Team 7: Jeremy Neil Friedman, Weston Joseph Haught. Please use oz.cse.wustl.edu

Team 8: Ryan Allen Johns, James Leslie Grady. Please use oz.cse.wustl.edu

Team 9: Lauren Miranda Jackson, Micah Scott Herstand. Please use oz.cse.wustl.edu

Team 10: Daniel Edward Brewster, Alan Jonathan Lundeen. Please use oz.cse.wustl.edu

Team 11: Jeremy Wayne Williams, Mark Avram Lilien. Please use oz.cse.wustl.edu

Team 12: Scott Zachary Bressler, Helena Marie Wotring. Please use oz.cse.wustl.edu

Team 13: Mamta Jaikumar Datwani, Douglas James Hyde, Daniel Everett Harter. Please use oz.cse.wustl.edu

Team 14: Steven D Broner. Please use oz.cse.wustl.edu

Note that team 13 has three students and team 14 has one.

Personal tools