如何在一次保存中保存多层修改

时间:2019-10-02 21:15:58

标签: python gimp python-fu

我有一个python脚本,可以在现有GIMP文件中查找和更新多个文本层。我想将整个文件另存为副本。但是,我似乎只能找到使用pdb.gimp_xcf_save记录的唯一方法,要求我传入一个drawable(为此我正在使用当前文本层)将文件另存为xcf。因此,当我遍历它们时,我在每一层都重复此调用。这似乎很浪费,而且肯定有一种解决方法可以只用一种方法来完成它,但我找不到它。

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)

1 个答案:

答案 0 :(得分:0)

AFAIK当保存为XCF等多层格式时,将忽略“ drawable”参数。因此,给它任何图像层或None并保存一次:

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