Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching for python script islands to layers
#12
(01-22-2018, 10:52 PM)Ofnuts Wrote: 1) No, I don't know what you mean. Are you taking in account the fact that the Y coordinates go down and not up?

2) The layer variable is actually a Python object(*) with  a writable "name" attribute, so you can do something like:
Code:
layer.name="Layer %03d" % some_sequence_number

or even
Code:
layer.name="Object @(%3d,%3d)" % (i*sample,j*sample)

Speaking of "i*sample", a seasoned Pythonista would loop like this:

Code:
for x in range(0,img.width,sample):
   for y in range(0,img.height,sample):
        # and use x,y instead of i*sample,j*sample
[code]

(*) Most things you manipulate are: image, drawable, layer (subclass of drawable), channel... and they  have methods that wrap the most used pdb.* calls. See [url=http://www.gimp.org/docs/python/index.html]this not so complete doc[/url] and if you are curious, do a "dir(image)" or "dir(layer)" or "dir(gimp)" in your Python console.

[/code]
Thanks for the tips
1) The selection went vertically instead of horizontally, i had to switch image.height loop with image.width, very counter intuative.
I also have to apply the mask layer for this script to work, thankfully the color data of the transparent pixels is preserved.
The code now looks like this:

Code:
img = gimp.image_list()[0]
layer = img.active_layer
#sample every 10 pixels
sample = 10
counter = 1
for y in range(0, img.height , sample):
    for x in range(0, img.width, sample):
        alphacheck = pdb.gimp_color_picker(img, layer, x, y, 0,0,0)
        if alphacheck[-1] > 0:
            pdb.gimp_fuzzy_select(layer, x, y, 254.9 , 2,0,0, 0,0)
            prev_layer = img.active_layer
            newlayer = pdb.gimp_layer_copy( img.active_layer, pdb.gimp_drawable_has_alpha( img.active_layer ) )
            pdb.gimp_image_insert_layer(img, newlayer, None, -1)
            bounds = pdb.gimp_selection_bounds(img)
            newlayer.name = "Layer %03d (%d, %d, %d, %d)" % (counter, bounds[1], bounds[2], bounds[3] - bounds[1], bounds[4] - bounds[2])
            counter += 1
            pdb.gimp_layer_resize(img.active_layer, bounds[3] - bounds[1], bounds[4] - bounds[2], -bounds[1], -bounds[2])
            img.active_layer = prev_layer
            pdb.gimp_edit_clear(prev_layer)

Could you make it go faster? Wink
Reply


Messages In This Thread
RE: Searching for python script islands to layers - by mich_lloid - 01-22-2018, 11:30 PM

Forum Jump: