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:36
Post: #4
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: #5
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: #6
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:58 (This post was last modified: Mar 7, 2012 20:58 by borko1980.)
Post: #7
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: #8
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:48
Post: #9
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: #10
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: #11
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, 22:05 (This post was last modified: Mar 7, 2012 22:06 by borko1980.)
Post: #12
RE: Python, get number of layers
Aha, so that means i have to have same number of 'space' characters before 'try' as in before 'except'?
Find all posts by this user
Quote this message in a reply
Mar 7, 2012, 23:03
Post: #13
RE: Python, get number of layers
Thanks for your help.
Find all posts by this user
Quote this message in a reply
Mar 8, 2012, 10:21
Post: #14
RE: Python, get number of layers
Ok. I have added horizontal/vertical orientation to be possible to choose in witch way layers will be copied, and moved menu as 'Layer' submenu. The problem now is i don't know how to detect (in python script) if input image is present so i can grey/ungrey option if it is or not?

Current code:
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)
    if orientation == "horizontal":
        width  = timg.width * numLayers
        height = timg.height
    else:
        width  = timg.width
        height = timg.height * numLayers
    
    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)
        if orientation == "horizontal":
            xoffset   = timg.width * i
            my_layer.translate(xoffset, 0)
        else:
            yoffset   = timg.height * i
            my_layer.translate(0, yoffset)

    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 new image stacked",
    "Copy layers to new image stacked",
    "borko1980",
    "borko1980",
    "2012",
    "<Image>/Layer/_CopyLayersStacked",
    "",
    [
        (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 8, 2012, 12:53
Post: #15
RE: Python, get number of layers
That's it. Thanks.

Code:
"*"

Is this is regex string?
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 362 Jul 11, 2017 07:26
Last Post: ythgilb
  Setting text markup from python script Burbly 1 481 Sep 16, 2014 12:29
Last Post: Burbly
  Python-Fu Script Not Registering image_artist 1 411 Sep 14, 2014 13:24
Last Post: image_artist
  Bashing my head - Batch Python Script nmiltner 1 489 Sep 5, 2014 00:58
Last Post: nmiltner
  Help with python script and drawables nmiltner 3 525 Aug 28, 2014 16:09
Last Post: nmiltner

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS