-1

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)
Poojan
  • 3,366
  • 2
  • 17
  • 33
Pete Duncanson
  • 3,208
  • 2
  • 25
  • 35
  • Don't you just overwrite the same file each time, so only the last save will be in effect anyway? – mkrieger1 Oct 02 '19 at 21:32
  • Also please fix the indentation of the code in the post. It's unclear which parts belong to the `def dynamic_text_replace_plugin` statement. – mkrieger1 Oct 02 '19 at 21:33
  • Well yes it does just that but seems a bit wasteful. I mean it works but seemed excessive. In my mind it should be able to keep the layers in memory and then just write them all down when I call gimp_xcf_save but that needs a layer to save so I've been doing it for every layer I touch. It works but feels wrong. – Pete Duncanson Oct 02 '19 at 21:54

1 Answers1

0

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')
xenoid
  • 8,396
  • 3
  • 23
  • 49