6. Painting areas with selections
In the uni script the function gimp_edit_fill was called
to fill the whole image. Looking at the info for gimp_edit_fill in
the DB browser we find the following:
A D V E R T I S E M E N T
Name: |
gimp_edit_fill |
Blurb: |
Fill selected area of drawable |
In: |
DRAWABLE |
drawable |
The drawable to fill from |
|
INT32 |
fill_type |
The type of fill: FG_IMAGE_FILL (0),
BG_IMAGE_FILL (1), WHITE_IMAGE_FILL (2), TRANS_IMAGE_FILL
(3), NO_IMAGE_FILL (4) |
Help: |
This procedure fills the specified drawable
with the fill mode. If the fill mode is foreground, the
current foreground color is used. If the fill mode is
background, the current background color is used. Other fill
modes should not be used. This procedure only affects
regions within a selection if there is a selection active. |
|
Thus, if a selection is active when gimp_edit_fill is called
only the selection is painted. There are lots of ways of choosing a
selection as can be seen when searching for a ``select'' in the PDB. The
example below uses gimp_rect_select, whose entry in the PDB looks
as follows:
Name: |
gimp_rect_select |
Blurb: |
Create a rectangular selection
over the specified image |
In: |
IMAGE |
image |
The image |
|
FLOAT |
x |
x coordinate of upper-left corner of
rectangle |
|
FLOAT |
y |
y coordinate of upper-left corner of
rectangle |
|
FLOAT |
width |
the width of the rectangle: width > 0 |
|
FLOAT |
height |
the height of the rectangle: width > 0 |
|
INT32 |
operation |
the selection operation: {ADD (0), SUB(1),
REPLACE (2), INTERSECT (3) } |
|
INT32 |
feather |
feather option for selections |
|
FLOAT |
feather_radius |
radius for feather operation |
Help: |
This tool creates a rectangular selection
over the specified image. The rectangular region can be
either added to, subtracted from, or replace the contents of
the previous selection mask. If the feather option is
enabled, the resulting selection is blurred before
combining. The blur is a gaussian blur with the specified
feather radius. |
|
A simple use of this function which selects a rectangle in the middle of an
image and paints that rectangle with a user defined color. This example also
introduces a couple of new features we haven't seen before:
- The script is associated with an image since its menu path starts
with "<Image>/...". Note that as a result of this the callback sub in
line 13 receives two additional parameters, the active image and the
seleced drawable.
- The use of a subroutine without a name as a parameter to
register
- The use of the PDB functions gimp_undo_push_group_start and
gimp_undo_push_group_end. These functions declare an undo
group. When an undo is done on the image, instead of having the
individual operators undo, all the actions between the undo start and
the undo group calls will be undone at once.
- The return type of the register function defines what new images
should be displayed by gimp. In this case we don't want to display any
new images and therefore return an empty array.
paint-select |
1: #!/usr/local/bin/perl -w
2:
3: use Gimp ":auto";
4: use Gimp::Fu;
5:
6: register
7: "img_paint_select",
8: "Paints the selection", "Paints the selection",
9: "Dov Grobgeld", "Dov Grobgeld", "1999-05-14",
10: "<Image>/Perl-Fu/Tutorial/Paint Select",
11: "*",
12: [
13: [PF_COLOR, "color", "Rectangle color", [0,0,255]] ],
14: sub {
15: my($img, $layer, $color) = @_;
16: my($width, $height) = (gimp_image_width($img),
17: gimp_image_height($img));
18:
19: # Select a rectangle inside the image and paint it with color
20: gimp_undo_push_group_start($img);
21: gimp_rect_select($img,
22: $width/4, $height/4, $width/2, $height/2,
23: REPLACE, 0,0);
24: gimp_palette_set_background($color);
25: gimp_edit_fill($layer, BG_IMAGE_FILL);
26: gimp_selection_none($img);
27: gimp_displays_flush();
28: gimp_undo_push_group_end($img);
29:
30: # Tell gimp not to display a new image
31: return ();
32: };
33:
34: exit main();
|
The result when run on our previous image:
6.1. Complex selections
Besides rectangular selections elliptical selections may also be created
through the PDB functions gimp_ellipse_select() and
gimp_free_select() which allows the selection of ellipses and polygons.
More complex selections may be created through the channel mechanism. The
PDB gimp_channel_new() creates a new channel. The channel is a
drawable that may be painted into, just like any other drawable, but with
the difference that it is always a grey level image. Once the channel is
finished, the channel may be loaded into the selection through the PDB
function gimp_selection_load().
Search for ``select'' in the DB Browser to see a list of all the
selection related functions.
6.2. Loops
In perl it is trivial to write loops that together with the various selecton
tools gives powerful creative possibilities. Here is an example that mixes
colors in circles. There is nothing really new here, but it shows the power
of the what we have described above.
circles |
1: #!/usr/local/bin/perl
2:
3: use Gimp ":auto";
4: use Gimp::Fu;
5:
6: sub circles {
7: my ($size, $bgcolor, $radius) = @_;
8:
9: # Create the background
10: $img = gimp_image_new($size, $size, RGB);
11: $layer = gimp_layer_new($img, $size, $size, RGB,
12: "Layer 1", 100, NORMAL_MODE);
13: gimp_image_add_layer($layer, -1);
14: gimp_palette_set_background($bgcolor);
15: gimp_edit_fill($layer, BG_IMAGE_FILL);
16:
17: my $ncircles = int($size/$radius/2);
18:
19: for ($i=0; $i<$ncircles; $i++) {
20: for ($j=0; $j<$ncircles; $j++) {
21: # Be creative and mix colors
22: $color = [$i*30, ($ncircles-$j)*25, ($i+$j)*15];
23:
24: # Select a circle
25: gimp_ellipse_select($img,
26: $i*$radius*2, $j*$radius*2,
27: $radius*2, $radius*2,
28: REPLACE, 1, 0, 0);
29:
30: # Paint the color in the circle
31: gimp_palette_set_background($color);
32: gimp_edit_fill($layer, BG_IMAGE_FILL);
33: gimp_selection_none($img);
34: }
35: }
36:
37: return $img;
38: }
39:
40: # register the script
41: register "circles", "a loop", "a loop", "Dov", "Dov", "1999-05-14",
42: "<Toolbox>/Xtns/Perl-Fu/Tutorial/Circles",
43: "*",
44: [
45: [PF_INT32, "size", "Img size", 100],
46: [PF_COLOR, "bg", "Background color", [40,180,60]],
47: [PF_INT32, "radius", "Circle radius", 10]
48: ],
49: \&circles;
50:
51: exit main();
|
The result:
|