01-23-2018, 12:43 AM
(01-22-2018, 11:30 PM)mich_lloid Wrote: 1) The selection went vertically instead of horizontally, i had to switch image.height loop with image.width, very counter intuitive.
Well, *you* wrote the inner loop on Y so the computer abides There is nothing "intuitive" in programming, at least not at the beginning.
(01-22-2018, 11:30 PM)mich_lloid Wrote: 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?
get_pixel() is possibly somewhat faster than pdb.gimp_color_picker(img, layer, x, y, 0,0,0)
Set a variable to the initial active layer and stop wondering what layer is the active one after that.
"bounds = pdb.gimp_selection_bounds(img)" is better written "_, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)" and then use x1,x2,y1,y2... instead of indexing bounds[]. Not much faster but easier to read. "_" is by convention the nale of a variable you won't use.
You are playing with fire, what if you sample in the hole inside an object?