for a reason. I'm trying my first
Gimp Perl script. However, it is
not really mine. It is someone
else's script that no longer works.
Apparently, Gimp Perl has changed
since this tutorial was written:
A Tutorial
for Perl Gimp Users
Updated for Gimp 1.2
The tutorial says that it was last
modified on March 28, 2003. Check
the above webpage to make sure that
this is still true. Everything I
write from here on will be based
on that date.
The script in the above tutorial is
called uni. You can access
the script in a good cut-and-paste
format by clicking on the word
uni which appears at the top
of the script.
The first thing I did was click on
uni and then I did a
cut-and-paste. The tutorial, which
is obviously outdated, recommended
that you copy the script to this
directory (folder):
$HOME/.gimp-1.2/plug-ins
I do not have a folder called
.gimp-1.2 for the obvious
reason that Gimp 1.2 is
outdated.
Instead, I have the following 2
folders under my home directory:
.gimp-2.2
.gimp-2.4
I'm guessing that the reason I have
2 folders instead of one is that I
must have upgraded Gimp from 2.2 to
2.4 and don't remember it.
In any case here's what happens when
I type the following command at the
command line to find out what version
of Gimp I'm running:
mylogin:~$ gimp -v GNU Image Manipulation Program version 2.4.7
Obviously, I'm running Gimp 2.4.
Therefore I need to put the script in the
following folder:
$HOME/.gimp-2.4/plug-ins
After I place the script in the above
folder, I find it will not run. This
is because it cannot find the Perl
interpreter.
Here's the first line of the script:
#!/usr/local/bin/perl -w
This is not where Perl is kept on
my Debian Linux system. To find out
where Perl is, I type the following
command:
which perl
Here's the answer that comes back:
/usr/bin/perl
I need to change the first line
of the script. Here's what it will
look like after I've removed the
word local:
#!/usr/bin/perl -w
OK. Now my script can find Perl.
Here's the error message that the
above change to line 1 corrected:
line 1: !/usr/local/bin/perl: No such file or directory
It is clear to me from the above
error message and from the error
messages that follow it, that without
the first line being correct, the
script called uni is assumed
to be a Unix shell script.
In other words, the above error message
is being generated by the Unix shell.
Next, I fire up Gimp and try to run the
script. The script is found at the
following menu location:
Xtns > Perl-Fu > Tutorial > Img Uni
When I run the script, I get the following
error message:
uni: Expected an INT32 but got 'BG_IMAGE_FILL'. Add '*1' if you really intend to pass in a string at /home/ined/.gimp-2.4/plug-ins/uni line 24 (ERROR)
Sounds like BG_IMAGE_FILL was not found. When
I go looking for BG_IMAGE_FILL, I find the
following on line 23 of the original unaltered version
of the script:
gimp_edit_fill($layer, BG_IMAGE_FILL);
Looks like gimp_edit_fill is a procedure.
I'll look it up in the Procedure Database,
also known as the PDB. I'll look at the
PDB via the procedure browser found here:
Xtns > Procedure Browser
In the procedure browser, I enter fill
as a search string. I find the procedure
gimp_edit_fill quite easily.
Note that the above error message objected to
the following parameter:
BG_IMAGE_FILL
The procedure browser indicates that there
is a similarly named parameter with a constant
value of 1:
BACKGROUND-FILL (1)
Did they change the name of the parameter on
us? It looks like they did.
The obvious thing to do is to
change the name. I'm going to
change BG_IMAGE_FILL to
BACKGROUND-FILL. Let's see
what happens.
OK. I've made the change in the
script now works --- sort of. It
works except that it now fills the
image with the color black instead
of orange.
Black is the foreground color and
orange is the background color. The
script is now malfunctioning!
It is filling the image with the wrong
color. It is choosing the foreground
color instead of the background color.
Let's see why this is.
I'll take a second look at gimp_edit_fill
in the PDB. Let's see if I can figure
this thing out.
I notice that gimp_edit_fill has
one parameter to represent the foreground
and one parameter to represent the background.
Here are the two parameters:
FOREGROUND-FILL (0), BACKGROUND-FILL (1)
The foreground parameter is a constant zero
and the background parameter is a constant one.
Next, I look for error messages that Gimp is
feeding back to the command line and I find
this:
Argument "FILL" isn't numeric in subtraction (-) at /home/ined/.gimp-2.4/plug-ins/uni line 24. Argument "BACKGROUND" isn't numeric in subtraction (-) at /home/ined/.gimp-2.4/plug-ins/uni line 24.
Of course! The minus sign in Perl is a subtraction
operator. Apparently BACKGROUND-FILL is
being interpreted is subtraction like this:
BACKGROUND - FILL
i'm guessing but I would imagine that the
above subtraction operation is being interpreted
as zero minus zero or one minus one. In either
case, the result will be zero. If the result is
zero, the constant parameter is interpreted as
zero and therefore the image is filled with
the foreground (which is zero) instead of the
background (which is one).
This explains why my image is being filled with
black instead of orange. It is because I'm passing
zero to gimp_edit_fill instead of one.
By now, the answer is apparent. I cannot use a
hypen in the name of a parameter as Perl will
interpret that hyphen as a minus sign.
Note that the parameter given in the Uni script
is BG_IMAGE_FILL. BG_IMAGE_FILL has underscores
instead of hyphens. OK. This must be the answer.
When I use the procedural browser to find gimp
procedures in the PDB, I should translate all
hyphens to underscores.
Let's try it. I'll now modify my script
so that BACKGROUND-FILL becomes
BACKGROUND_FILL.
Voila! Success! The image is filled with
the color orange. Also, no error messages
appear on the command line when I run the
script.
I realize now that I was not very observant.
The name of the function in the Perl script
is called gimp_edit_fill. The name
is the same in the PDB except that it is
called gimp-edit-fill. It's a case
of hypens versus underscores.
Apparently when writing Perl-Fu scripts,
the names of both procedures and procedure
parameters need to be changed from hypens
to underscores.
This one simple piece of information --
hyphens versus underscores is going to be
very very helpful I suspect.
I learned these things from this
experience:
- Mike sure the first line
of your Gimp Perl script contains
the correct path to the Perl
interpreter - Procedures in the PDB change
over time - Excecute Gimp from the command
line and then observe any error
messages that occur when you run
your perl script for the first
time - If a procedure produces error
messages, use the Gimp commmand line
to fully discover these errors. - Find the correct parameters for
a procedure in the PDB - When inserting parameters into
a procedure, translate hypens to
underscores to make them Perl
compatible
Mostly I learned that a little bit
of persistence pays off big.
Ed Abbott