How do I save (export) all layers with gimp's script fu?

醉酒当歌 提交于 2019-12-20 20:00:56

问题


With gimp fu, I can save the content of one layer (at least, that is how I interprete the definition of gimp_file_save because it takes the parameter drawable).

Now, I have the following script:

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()

When I use pdb.gimp_file_save(img, drawable, '/temp/tq84_write_text.png', '?') to save the image, It will only export the "text" layer. Yet, If I use pdb.gimp_file_save(img, background_layer, '/temp/tq84_write_text.png', '?') it will only export the background. So, how can I export both layers into one image (as the menu File -> Export As would do).


回答1:


What is done internally, even by GIMP file-expoerter plug-ins for all formnats is: duplicate the image, merge all visible layers, them save the resulting drawable.

This is easier, and take less resources than it sounds. Effectively you just have to replace your save line

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

by

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)

(The last call just "deletes" the new image from program memory, freeing up the resources, of course)




回答2:


I have found out that if you pass None as the drawable argument to gimp_xcf_save(), GIMP (at least version 2.8) will save all layers of the image to the XCF file:

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




回答3:


What I found easiest was to flatten the image then just save using the first layer:

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


来源:https://stackoverflow.com/questions/26803732/how-do-i-save-export-all-layers-with-gimps-script-fu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!