【问题标题】:How do I save (export) all layers with gimp's script fu?如何使用 gimp script fu 保存(导出)所有图层?
【发布时间】:2014-11-07 14:44:46
【问题描述】:

通过gimp fu,我可以保存one层的内容(至少,我是这样解释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()

当我使用pdb.gimp_file_save(img, drawable, '/temp/tq84_write_text.png', '?') 保存图像时,它只会导出“文本”层。然而,如果我使用pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?'),它只会导出背景。那么,如何将两个图层导出到一张图像中(就像菜单 File -&gt; Export As 那样)。

【问题讨论】:

标签: python gimp python-fu


【解决方案1】:

内部完成的操作,即使是所有格式的 GIMP 文件导出器插件也是:复制图像,合并所有可见层,它们保存生成的可绘制对象。

这比听起来更容易,而且占用的资源更少。实际上你只需要替换你的保存线

pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?')

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)

(最后一次调用只是从程序内存中“删除”新图像,当然释放了资源)

【讨论】:

    【解决方案2】:

    我发现如果您将None 作为drawable 参数传递给gimp_xcf_save(),GIMP(至少2.8 版)会将图像的所有层保存到XCF 文件中:

    pdb.gimp_xcf_save(0, image, None, 'file.xcf', 'file.xcf')

    【讨论】:

      【解决方案3】:

      我发现最简单的方法是将图像展平,然后使用第一层保存:

      img.flatten()
      pdb.gimp_file_save(img, img.layers[0], 'image.jpg', '?')
      

      【讨论】:

        猜你喜欢
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多