Motivation

While it is easy to do, opening a Jpeg file and saving it in the native
GIMP *.xcf format is tedious and time consuming. You may also want to
perform the same operation, like setting the grid spacing as an example,
on all of the images that you import as part of the process. Automating
the parts of the editing process that are the same for each image saves
time, produces consistent results, and allows you to focus your attention
on the part of the image editing process that really needs your attention.

This tutorial is a scripting example that is intended to illustrate automating
part of the editing process. The script opens all of the Jpeg images in
the source directory, and saves them as xcf files in the target directory.
This is not a basic scripting tutorial, a basic understanding of writing
GIMP scripts is assumed.

Requirements that are different than most scripts.

The script needs to be available and run when there is no image open.

The script has to be able to provide a GUI to identify the Source and
Target directories, identify all of jpg files in the source directory
and convert them.

The script needs to be aware of the type of operating system of the
host machine, or more specifically whether the filename separator is
a "\" or a "/".

The script has to be smart enough to not overwrite work in progress,
(Xcf files with the same name in the target directory).

Let's look at the first requirement, to be able to run the script
without an open image. We are working on a group of several images not
just one, and since we want to start our script from a know point, we
don't want to have an image open for edit, just the GIMP framework or
application.

The availability of a script from within the GIMP application is
established and controlled within the script-fu-register block. After the
function name are an ordered set of strings of required information
followed by an optional number of GUI widget declarations that may feed
selected information to the script. The required strings are:

Function Name - Matches the name of the function in the opening
'define' statement.

Menu Label - The label identifying the function in the menus and
the way to invoke the script.

Description - The description that will be displayed about the
script in the Procedure Browser.

Author - Name of the Author

Copyright - Copyright information.

Creation Date - Date you wrote the script.

Valid Image Type - The type of image the script is
permitted to work upon.

The content of the Valid Image Type is the way that we specify
whether we can run the script without an image open. You might be tempted
to use an image type of "*" to specify running all of the time, but that
is not quite what it does. The "Valid Image Type" is a way to restrict the
script from running in an unintended context, "*" is the way to specify
"any open image". What we want instead is an empty string - "" -
to specify running without an image open. The registration block for
the script-fu version of the program would look like (the Python
registration block follows a similar pattern):

Registration Block

( script-fu-register "script-fu-example-jpg-to-xcf" ; Function Name
"1 ) Import JPG to XCF (Directory)" ; Menu Label
"This script is an interactive script to convert all of the jpegs
in a source directory into GIMP xcf format files in a target
directory. The script is designed to be run WITHOUT ANY IMAGE
LOADED. Runs from GIMP shell in Linux and Windows."
"Stephen Kiel" ; Author
"2013, Stephen Kiel" ; Copyright
"July 2013" ; Creation Date
"" ; Valid Image Type - No Image required
; We actually don't want any images open when we run this
; script, so it must be available from the menu when an
; image is not loaded. This script will determine the IDs
; of the Image and Drawable itself rather than having them
; passed as parameters.
; Interactive widgets
SF-DIRNAME "JPG Originals (source) Directory" ""
SF-DIRNAME "XCF Working (target) Directory" ""
) ; End script-fu-register
( script-fu-menu-register
"script-fu-example-jpg-to-xcf" "<Image>/Example-Scm")

Function Menus

Our second requirement, to be able to use the GUI to browse
to our Jpeg source file directory and our Xcf target file directory
is handled first in the registration block by using the
"SF-DIRNAME" widgets to label the GUI selection fields and
pass the collected results as inputs to the script (above). Once
we have the path names to our source and target directories we will
use the "file-glob" (os.listdir in python-fu) function
within the main program to create a list of all of the image files
in those directories.

We address the third requirement of determining the operating
system type within the function itself. We want the function to be
portable, so it can run on either the Linux desktop unit you have in
your home, or the Windows laptop you carry out on the road. We need
to know the OS type in order to determine the pathname separator,
either a "\" or "/" and deal with it in the script.

Fortunately once we have the pathname to the source directory it is
easy to determine which operating system generated it by using the
script-fu "strbreakup" function. We can compare the number
of pieces by breaking on the "/" and "\" character and use the
result with the greater number of pieces to determine the platform's
operating system. If we are using the python-fu scripting language
we can check the value of os.name directly.

Our fourth requirement to not overwrite existing *.xcf
files which may have ongoing work in progress is solved by
building a list of files in the target directory and checking
that list for existence before writing each new file.

Conclusion

The working example script is pretty straightforward; building
a list of file names, running through that list in a loop,
opening each source file and exporting it in turn. An equivalent
python-fu version is included as well. You should be able to use
this example as a model to build similar functions, examples of
similar functions might be to convert a directory of xcf files
to jpg files, to scale original jpg files to a smaller size, etc.

When using the GIMP widgets to Browse through the file system,
you will probably need to select "Other" to get to navigate where
you really want. When using an automated script it is
ALWAYS a good idea to run on a COPY of your original
images.

The Scripts

The following scripts are the script-fu and python-fu
versions of the Jpg to Xcf function discussed above.