如何使用gimp的脚本fu保存(导出)所有图层?

Ren*_*ger 9 python gimp python-fu

随着GIMP福,我能救的内容一个层(至少,这就是我interprete的定义,gimp_file_save因为它需要的参数drawable).

现在,我有以下脚本:

from gimpfu import *

def write_text():

    width  = 400
    height = 100

    img = gimp.Image(width, height, RGB)
    img.disable_undo()


    gimp.set_foreground( (255, 100, 20) )
    gimp.set_background( (  0,  15, 40) )

    background_layer = gimp.Layer(
                           img,
                           'Background',
                           width,
                           height,
                           RGB_IMAGE,
                           100,
                           NORMAL_MODE)

    img.add_layer(background_layer, 0)
    background_layer.fill(BACKGROUND_FILL)

    text_layer = pdb.gimp_text_fontname(
                    img,
                    None,
                    60,
                    40,
                    'Here is some text',
                    0,
                    True,
                    30,
                    PIXELS,
                    'Courier New'
                )

    drawable = pdb.gimp_image_active_drawable(img)

#   Either export text layer ...
#   pdb.gimp_file_save(img, drawable, '/temp/tq84_write_text.png', '?')

#   .... or background layer:
    pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?')

register(
  proc_name     = 'tq84_write_text',
  blurb         = 'tq84_write_text',
  help          = 'Create some text',
  author        = 'Rene Nyffenegger',
  copyright     = 'Rene Nyffenegger',
  date          = '2014',
  label         = '<Toolbox>/Xtns/Languages/Python-Fu/_TQ84/_Text',
  imagetypes    = '',
  params        = [],
  results       = [],
  function      = write_text
)

main()
Run Code Online (Sandbox Code Playgroud)

当我pdb.gimp_file_save(img, drawable, '/temp/tq84_write_text.png', '?')用来保存图像时,它只会导出"文本"图层.然而,如果我使用pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?')它只会导出背景.那么,如何将两个图层导出到一个图像中(如菜单所示File -> Export As).

jsb*_*eno 12

即使是所有formnats的GIMP file-expoerter插件,内部完成的工作是:复制图像,合并所有可见图层,保存生成的drawable.

这比听起来更容易,占用的资源更少.实际上,您只需要更换保存线

pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?')
Run Code Online (Sandbox Code Playgroud)

通过

new_image = pdb.gimp_image_duplicate(img)
layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
pdb.gimp_file_save(new_img, layer, '/temp/tq84_write_text.png', '?')
pdb.gimp_image_delete(new_image)
Run Code Online (Sandbox Code Playgroud)

(最后一次调用只是从程序存储器"删除"新图像,当然释放资源)