On Saturday 15 July 2006 02:39 pm, Manuel QuiƱones wrote:
> Hello
> For a while I was doing this to merge two layers: I iterate through
> the entire stack of layers, to make them visible, and the rest
> unvisible, and using the merge visible layers to join them.
>     for L in img.layers:
>         if L == layer1 or L == layer2:
>             pdb.gimp_drawable_set_visible(L, True)
>         else:
>             pdb.gimp_drawable_set_visible(L, False)
>     pdb.gimp_image_merge_visible_layers(img, 0)
> Is there a better way to do this?
No, there is none.
Due to the way the pdb is developed, its functions tend to mimic what 
is available in the UI, instead of providing raw access to the GIMP 
internals. (Like a merge( *layers)  function would do)

So, unless there is a merge method in the api for the layer object, 
and (me looks), there is not, you are limited to either "merge down" 
- which merges 2 adjacent visible layers on the layer stack or "merge 
visible layers".

There are small tweaks that can make the code cleaner by making use of 
the properties of the Layer object itself, instead of using the PDB. 
But the main function here is "merge", and for that there is only the 

In terms of processing time, the time one takes to search the layers 
like you are doing is negligible next to the one taken by combining 
the pixels itself - thats is why python is great at managing gimp 
high-level objects. The problem here is actually the one that python 
tries to avoid - developer time. You have to work around the lack of 
a merge( *layers)  function with constructs like this.

Actually, there is a problem yet with your code - you change the 
visibility of all layers on the image and do not restore them later. 
That makes such a script an inconvenience to the user.

The work around to "remember" the layer visibility will be even uglier 
- in this loop, you have to create and fill a data structure, 
associating an unique attribute from each layer and its original 
visibility.  After performing your merge, you have to loop through 
all layers again, setting their visibility back.

That means, to be nice for the user, and withthe minro tweaks I 
suggested above, your code could be like:

     vis_dict = {}
     for L in img.layers:
        vis_dict[L.name] = L.visible
        if L in (layer1, layer2):
            L.visible = False
            L.visible = True
    pdb.gimp_image_merge_visible_layers(img, 0)
    for L in img.layers:
        L.visible = vis_dict[L.name]

As you can see, using the object API, and a python trick for checking 
if L is one of the elements of the tuple (L1, L2), the code becomes 
shorter and more readable. This can even becomeitself a merge 
(*layers)  function like I mentioned before - you just place it on 
the begining of your code. Probably python fu should include soem 
utility functions like this to the API beyond those provided by the 
gimp core, but who knows?

> manuelq
> _______________________________________________
> Gimp-user mailing list
> Gimp-user@lists.XCF.Berkeley.EDU
> https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-user
Gimp-user mailing list

Reply via email to