Post Reply 

Python, get number of layers

Mar 7, 2012, 18:14 (This post was last modified: Mar 7, 2012 18:15 by borko1980.)
Post: #1
Python, get number of layers
How to obtain a number of layers of input image?
I need to create new image where:
Code:
width = timg.number_of_layers * timg.width
height = timg.height

and then copy each layer of input image to new one so it looks like this:
input image (say 100x100):
Code:
$ - layer1
@ - layer2
% - layer3
& - layer4
output image (400x100):
Code:
$@%& - layer1

Also if you can help me find (if exists) a proper documentation for Gimp-Python function/members list explained in detail, because i can't find it with google, except this witch is partial/incomplete.

Thanks for your time.
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 19:08 (This post was last modified: Mar 7, 2012 19:41 by borko1980.)
Post: #2
RE: Python, get number of layers
I tried this but Gimp fails to load, and i don't see whats wrong
Code:
#!/usr/bin/env python

import math
from gimpfu import *

def layers_to_image(timg, tdrawable, orientation):

    imgLayers =  timg.layers
    numLayers = len(imgLayers)
    
    width  = timg.width * numLayers
    height = timg.height
    
    image = gimp.Image(width, height, RGB)
    image.disable_undo()
    
    gimp.set_foreground(255, 255, 255)
    gimp.set_background(0, 0, 0)
    
    for i in range(0, numLayers):
        currLayer = timg.layers()[i]
        my_layer  = currLayer.copy()
        image.add_layer(my_layer, 0)
        xoffset   = width * i
        my_layer.translate(xoffset, 0)
        
    
    pdb.gimp_image_resize_to_layers(image)
    pdb.gimp_image_flatten(image)
    
    image.enable_undo()

register(
    "layers_to_image",
    "Copy layers to image",
    "Copy layers to image",
    "borko1980",
    "borko1980",
    "2012",
    "<Image>/File/Create/_CopyLayers",
    "",
    [
        (PF_RADIO, "orientation", "Orientation", "horizontal",
            (("_horizontal", "horizontal"), ("_vertical", "vertical"))
        )
    ],
    [],
    layers_to_image)

main()
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 19:41
Post: #3
RE: Python, get number of layers
EDIT: Where can i see debug information (error messages) when script isn't loaded. I want to know why.
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:21 (This post was last modified: Mar 7, 2012 20:31 by ofnuts.)
Post: #4
RE: Python, get number of layers
You can find a lot using "dir(your_object)". This lists all attributes and members.

And image has a "layers" attribute which is a list of the layers it contains, so you can iterate it:
Code:
for layer in image.layers:
  if layer.visible:  # for instance

You can use the python console to use dir() on various objects to find attributes and methods. Most methods you find that way usually are direct equivalents of the PDB functions (but make for much more readable code). Usually, where PDB methods take an ID, these method take the object (but not always Smile )

What you want to do is quite easy: if "source" and "target" are your images, copying the layers from one to the other goes something like
Code:
for layer in reverse(source.layers):  # list from top, but we want to add from bottom
  target.add_layer(layer.copy(),0)
yes, that's it... Note that you can also duplicate the image ("source.duplicate()"), this is likely to be faster.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:36
Post: #5
RE: Python, get number of layers
But i need integer index to be able to calculate offset when copying to new image:
Code:
for i in range(0, numLayers):
        layer     = timg.layers()[i]
        my_layer  = layer.copy()
        image.add_layer(my_layer, 0)
        xoffset   = width * i
        my_layer.translate(xoffset, 0)
Since i don't know where i make mistake, nor i know a sytaks.

I am not a coder, and have lot of trouble to get this because i use this operations frequently and i am tiered of doing it by hand, so i deicided to learn Gimp python , but documentation sucks (escuse my language).
Please, if you find time, help me to do what i want.
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:37
Post: #6
RE: Python, get number of layers
Does this code:
Code:
layer     = timg.layers()[i]
grabs layer at 'i' index?
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:51
Post: #7
RE: Python, get number of layers
I need this:

input image:
[Image: inputimage.jpg]

output image:
[Image: output.jpg]
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:52
Post: #8
RE: Python, get number of layers
(Mar 7, 2012 20:37)borko1980 Wrote:  Does this code:
Code:
layer     = timg.layers()[i]
grabs layer at 'i' index?
No, because "layers" is a list, not a method returning a list. So that would be
Code:
layer=timg.layers[i]

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 20:58 (This post was last modified: Mar 7, 2012 20:58 by borko1980.)
Post: #9
RE: Python, get number of layers
Gimp still want load my script:
Code:
#!/usr/bin/env python

import math
from gimpfu import *

def layers_to_image(timg, tdrawable, orientation):

    imgLayers =  timg.layers
    numLayers = len(imgLayers)
    
    width  = timg.width * numLayers
    height = timg.height
    
    image = gimp.Image(width, height, RGB)
    
    for i in range(0, numLayers):
        layer     = timg.layers[i]
        my_layer  = layer.copy()
        image.add_layer(my_layer, 0)
        xoffset   = width * i
        my_layer.translate(xoffset, 0)
    
    gimp.Display(image)

register(
    "layers_to_image",
    "Copy layers to image",
    "Copy layers to image",
    "Belphegor",
    "Belphegor",
    "2012",
    "<Image>/File/Create/_CopyLayers",
    "",
    [
        (PF_RADIO, "orientation", "Orientation", "horizontal",
            (("_horizontal", "horizontal"), ("_vertical", "vertical"))
        )
    ],
    [],
    layers_to_image)

main()

Where is the error?

Thanks for your time
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:23
Post: #10
RE: Python, get number of layers
Just tried it without loop and it runs, so i made mistake in a loop but dont see where???

Code:
for i in range(0, numLayers):
        layer     = timg.layers[i]
        my_layer  = layer.copy()
        image.add_layer(my_layer, 0)
        xoffset   = width * i
        my_layer.translate(xoffset, 0)
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:34
Post: #11
RE: Python, get number of layers
Actually you code is pretty good. Here is one that sort of work (sort ofn because you still have a bug, but I'll let you find it out Smile
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
from gimpfu import *

def layers_to_image(timg, tdrawable, orientation):

    imgLayers =  timg.layers
    numLayers = len(imgLayers)
    
    width  = timg.width * numLayers
    height = timg.height
    
    image = gimp.Image(width, height, RGB)
    image.disable_undo()
    
    gimp.set_foreground(255, 255, 255)
    gimp.set_background(0, 0, 0)
    
    for i in range(0, numLayers):
        currLayer = timg.layers[i]
        my_layer  = pdb.gimp_layer_new_from_drawable(currLayer, image)
        image.add_layer(my_layer, 0)
        xoffset   = width * i
        my_layer.translate(xoffset, 0)
        
    
    pdb.gimp_image_resize_to_layers(image)
    pdb.gimp_image_flatten(image)
    
    image.enable_undo()
    gimp.Display(image)

register(
    "layers_to_image",
    "Copy layers to image",
    "Copy layers to image",
    "borko1980",
    "borko1980",
    "2012",
    "<Image>/File/Create/_CopyLayers",
    "",
    [
        (PF_RADIO, "orientation", "Orientation", "horizontal",
            (("_horizontal", "horizontal"), ("_vertical", "vertical"))
        )
    ],
    [],
    layers_to_image)

main()
The changes from yours:
  • used layers[i] instead of layers()[i] as above
  • replaced "layer.copy()" by "pdb.gimp_layer_new_from_drawable()". Layer.copy() creates layers that can only be used in the same image.
  • added "gimp.Display(image)" to get a window displaying your image (you don't need that if you want to save the image directly to file).
So on the whole you were very close... if this is your first python program you deserve well-earned congratulations.

Tricks of the trade:

To catch the bulk of the syntax errors, in a command line prompt/terminal session, run your python code outside of Gimp:
Code:
>>python CreateFromLayers.py
Traceback (most recent call last):
  File "CreateFromLayers.py", line 5, in <module>
    from gimpfu import *
  File "/home/bd/.gimp-2.6/plug-ins/gimpfu.py", line 75, in <module>
    import gimp
ImportError: No module named gimp
If it gets that far, you have no obvious syntax errors such as "inconsistent indenting" which is fairly frequent in Python (in Windows, Python comes with IDLE which is also pretty good for this). You can then proceed to the next step, running you python as a plugin.

To catch startup errors (if your plugin doesn't appear in the menu) start Gimp in a shell session (Linux) or use gimp-console (Windows). This should display errors if they happen. This will also display the output of any "print" instruction you have in your python if you need this to debug.

Start an "Error console" window in Gimp. You can them use "pdb.gimp_message()" without having a dialog for each message.

The body of your main function (the one your register) should look like:
Code:
    try:
        your_real_code_gos_here()
        except Exception as e:
        print e.args[0]
        pdb.gimp_message(e.args[0])
So all execution errors get displayed.

My python productivity has made a great leap when I moved to Linux Smile

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:48
Post: #12
RE: Python, get number of layers
Wohoho... thanks.

I found a bug as soon as i get this running, thanks to you:
Code:
xoffset   = width * i    #WRONG
xoffset   = timg.width * i #RIGHT

Quote:So on the whole you were very close... if this is your first python program you deserve well-earned congratulations.

Well, i looked some examples, copy/paste/edit play with them but still don't get the syntaks right.

THANKS A LOT. KISSES YOUR FEET.
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:53
Post: #13
RE: Python, get number of layers
...And about exceptions:
Code:
def my_func(bla, bla, bla)
    try:
        ....my whole code
        except Exception as e:
        print e.args[0]
        pdb.gimp_message(e.args[0])

Right?
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:58 (This post was last modified: Mar 7, 2012 21:59 by borko1980.)
Post: #14
RE: Python, get number of layers
Hm, I don't get nothing helpful from console (i am on win7):

[Image: console.jpg]

??? What this mean?
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 21:59
Post: #15
RE: Python, get number of layers
(Mar 7, 2012 21:53)borko1980 Wrote:  ...And about exceptions:
Code:
def my_func(bla, bla, bla)
    try:
        ....my whole code
        except Exception as e:
        print e.args[0]
        pdb.gimp_message(e.args[0])

Right?

except/try should have the same indent. And you are missing ":" at the end of the "def" line. Otherwise it looks ok.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Loading PythonFu Plugin: ImportError for .../python/gimp.so (GIMP 2.8.22/OSX/Py2.7.9) joroek 1 256 Jul 11, 2017 07:26
Last Post: ythgilb
  Setting text markup from python script Burbly 1 392 Sep 16, 2014 12:29
Last Post: Burbly
  Python-Fu Script Not Registering image_artist 1 353 Sep 14, 2014 13:24
Last Post: image_artist
  Bashing my head - Batch Python Script nmiltner 1 410 Sep 5, 2014 00:58
Last Post: nmiltner
  Help with python script and drawables nmiltner 3 461 Aug 28, 2014 16:09
Last Post: nmiltner

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS