PyGIMP Routines

license your software

You should get it over with as soon as possible, determine how you will license your scripts. This uses the python template. I suggest to you that you do not talk to anyone about it.



software_license="""
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""



register your plug-in

One of the more difficult tasks and it doesn't matter which script language you are using. This registry stuff is the point where the user interfaces with TheGIMP and it can be really picky.

This documentation is in progress....


register(
        "python_fu_plugin_name",
        "simple description",
        "description and instructions",
        "Your Name",
        "Copyright Holder",
        "Copyright Date",
        "<GimpGuiPath>/Menu/SubMenu/Title",
        "Image Types",
        [
	(PF_COLOR, "color_name", "red, green, blue", (255,255,255),
	(PF_COLOUR, "colour_name", "red, green, blue", (000,000,000),
	(PF_PALETTE, "palette_name", "Palette selector", "Default"),
        (PF_FONT, "font", "Font selector", "Sans"),
        (PF_BRUSH, "brush", "Brush selector", "Untitled"),
        (PF_PATTERN, "pattern", "Pattern selector", "Pine"),
        (PF_GRADIENT, "gradient", "Gradient selector", "Default"),
        (PF_FILE, "location_name", "A directory or file on a computer.", "/tmp"),
        (PF_BOOL, "simple_answer", "Ask a yes or no question:", False),
        (PF_TOGGLE, "simple_answer", "Ask a yes or no question:", False),
	(PF_RADIO, "choice_variable", "Multiple Choices:", ".value_name_2", (
                  ("readible name 0", "value_name_0"),
                  ("readible name 1", "value_name_1"),  
                  ("readible name 2", "value_name_2"))),
        (PF_SPINNER, "numerical_value", "Numerical value:", 33, (1,100,1)),
        (PF_SLIDER, "numerical_value", "Numerical value:", 77, (1,100,1)),
        (PF_STRING, "text_ui",  "Pass a string through the interface:", "text string from ui")
        ],
        [],
        plugin_name)

### your plug-in here ###

main()

name
blurb
help
author
copyright name
copyright date
menu path
image types
params
results
function
end of script

saving and formating image files

Saving images is different depending on the format.

save jpeg

This example uses the default settings and accepts a comment string.


def save_jpeg(image, new_filelocation, comment):
    layer = pdb.gimp_image_flatten(image)
    quality = 85.0
    smoothing, optimize, progressive = 0.0, True, False
    subsmp, baseline, restart, dct = 1, False, 0, 1
    pdb.file_jpeg_save(image, layer, new_filelocation, new_filelocation, quality, smoothing, optimize, progressive, comment, subsmp, baseline, restart, dct)


save png

This example uses the default settings and tells it to save any comments that are attached to it..


def save_png(image, drawable, new_filelocation, use_comment):
    compression = 9
    interlace, bkgd = False, False
    gama, offs, phys = False, False, False
    time, svtrans = True, False
    pdb.file_png_save2(image, drawable, new_filelocation, new_filelocation, interlace, compression, bkgd, gama, offs, phys, time, use_comment, svtrans) 


index image colors

GIMP uses and should use RGB images while the work is in progress. Converting to indexed should happen right before saving into a file format that needs or accepts this.


def index_image(image, drawable):
    # from the Indexed Color Conversion Defaults
    if pdb.gimp-drawable-has-alpha(drawable):number_colors = 255
    else:number_colors = 256
    dither_type, palette_type = NO_DITHER, MAKE_PALETTE
    alpha_dither, remove_unused, palette = False, True, ''
    pdb.gimp_image_convert_indexed(image, dither_type, palette_type, number_cols, alpha_dither, remove_unused, palette)
    return image

read files from a directory

The secret to batch processing via GIMP for me.



def get_images(original_type, original_location):
    images = []
    for filename in os.listdir(original_location):
        if filename.endswith(original_type):
            basename, ext = filename.split('.')
            imagefile = os.path.join(original_location, filename)
            original_image = {'base_name':basename,
                              'image_file':imagefile}
            images.append(original_image)
    return images


pygimp via script-fu server via shell script

It is disturbing, but it works. You can use the script-fu server to run a python plug-in from the command line. At least I can with gimp on linux.

This example uses the batch resize plug-in

gimp-2.3 -i -b '(python_fu_batch_resize RUN-NONINTERACTIVE ".jpg", "/tmp/images/", 100, "-browser", ".png", True, True, "/tmp/web-images")' -b '(gimp-quit 0)'