gimppython-fu

Gimp Python scripts work separately but not when combined


I have a multilayer Gimp XCF template and my goal is to automate inserting JPGs into it and export them from the command line.

I have a first working python-fu plugin for inserting the image, and a second working python-fu plugin for flattening/saving the image (one line of executed code), but I want to combine the two plugins to make them easier to call from the command line. Eventually I also want to automate the opening of the XCF file. For now though I'm just trying to combine the two functions. Both plugins receive "image" and "layer" as input parameters.

My function parameters for the combined plugin are image, layer, the JPG to insert (file), X and Y offsets for placing the image in the XCF template (x_offset, y_offset), and a place for the export (outputFolder).

When I add the save command (pdb.file_jpeg_save shown near the bottom of my code) to the first working script, it fails. Why would this work on its own but fail here?

My code is shown below.

#!/usr/bin/env python

from gimpfu import *

def add_flatten_save(image, layer, file, x_offset, y_offset, outputFolder):
    ''' Add image to new layer, flatten, then saveSave the current layer into a PNG file, a JPEG file and a BMP file. '''

    # Indicates that the process has started.
    gimp.progress_init("Opening '" + file + "'...")

    try:
        # Open file.
        fileImage = None

        if(file.lower().endswith(('.jpeg', '.jpg'))):
            fileImage = pdb.file_jpeg_load(file, file)

            # Create new layer.
            newLayer = gimp.Layer(image, "New Layer Name", layer.width, layer.height, layer.type, layer.opacity, layer.mode)
            # the +1 adds it behind the top layer
            image.add_layer(newLayer, +1)

            # Put image into the new layer.
            fileLayer = fileImage.layers[0]
            pdb.gimp_edit_copy(fileLayer)
            floating = pdb.gimp_edit_paste(newLayer, True)

            # Update the new layer.
            newLayer.flush()
            newLayer.merge_shadow(True)
            newLayer.update(0, 0, newLayer.width, newLayer.height)

            # Flatten + offset floating layer, then flatten image
            pdb.gimp_floating_sel_to_layer(floating)
            pdb.gimp_layer_set_offsets(floating, x_offset, y_offset)
            pdb.gimp_image_flatten(image)

            # Export JPG of flattened image
            pdb.file_jpeg_save(image, layer, outputFolder + "/" + layer.name + ".jpg", "raw_filename", 0.9, 0, 0, 0, "Creating with GIMP", 0, 0, 0, 0)

        else:
            gimp.message("The image could not be opened since it is not an image file.")

    except Exception as err:
        gimp.message("Unexpected error: " + str(err))
    
register(
    "python_fu_add_flatten_save",
    "Add image to layer",
    "Add image to layer and flatten.",
    "Tim B.",
    "Tim B.",
    "2021",
    "<Image>/Filters/Tim/Add, flatten, save",
    "*",
    [
        (PF_FILE, "file", "File to open", ""),
        (PF_INT, "x_offset", "X offset", ""),
        (PF_INT, "y_offset", "Y offset", ""),
        (PF_DIRNAME, "outputFolder", "Output directory", ""),
    ],
    [],
    add_flatten_save)

main()

Solution

  • The fundamental problem is that when you flatten the image, the layer "layer" doesn't exist anymore.

    Try adding layer = pdb.gimp_image_get_active_layer(image) before you save.