pythongimppython-fu

How to save multiple layer modifications in one save


I have a python script that finds and updates multiple text layers in an existing GIMP file. I want to save the whole file out as a copy. However the only way I can seem to find that documented using pdb.gimp_xcf_save requires I pass in a drawable (for which I'm using the current text layer) to save the file as a xcf. So I'm repeating this call for each layer as I loop through them. This seems wasteful and surely there must be a way around this to just do it in one method but I've not be able to find it.

def dynamic_text_replace_plugin(timg, tdrawable, pathToXcf, textToReplace, saveAsJpg):
    texts = textToReplace.split('~')
    myImage = pdb.gimp_xcf_load(1, pathToXcf, pathToXcf)

    textCount = 0
    for newText in texts:
        textCount = textCount + 1
        textLabel = "TEXT" + str(textCount)
        myLayer = pdb.gimp_image_get_layer_by_name(myImage, textLabel)
        sourceText = pdb.gimp_text_layer_get_text(myLayer)
        textToReplace = sourceText.replace(textLabel, newText)
        pdb.gimp_text_layer_set_text(myLayer, textToReplace)
        # Not sure if I can do this once or if it has to be for every layer?
        saveAsPath = pathToXcf.replace(".xcf", "_replaced.xcf")
        pdb.gimp_xcf_save(1, myImage, myLayer, saveAsPath, saveAsPath)

    if (saveAsJpg):
        pathToJpg = pathToXcf.replace(".xcf", ".jpg")
        myImage.flatten()
        pdb.gimp_file_save(myImage, myImage.layers[0], pathToJpg, '?')

    # Clean up memory
    pdb.gimp_image_delete(myImage)

Solution

  • AFAIK the "drawable" parameter is ignored when you save to multi-layer formats such as XCF. So give it any image layer or None and save just once:

    pdb.gimp_xcf_save(0, image, None,'/tmp/foo.xcf','/tmp/foo.xcf')