01-23-2018, 02:23 PM
(This post was last modified: 01-23-2018, 02:27 PM by mich_lloid.)
"get_pixel() is possibly somewhat faster than pdb.gimp_color_picker(img, layer, x, y, 0,0,0)"
Wow, drawable.get_pixel() is approximately 6,5 times faster than pdb.gimp_color_picker, thanks for pointing me to that.
the script looks like this now:
results as to speed with samples of 10, 5 and 1 pixels for a 1024x1024 image with 5 objects:
➤> objects_to_layers(img)
time taken: 2.11100006104 seconds.
➤> objects_to_layers(img, 5)
time taken: 7.57000017166 seconds.
➤> objects_to_layers(img, 1)
time taken: 181.577000141 seconds.
Also your script "Layer > Extract Objects > Extract objects to layers..." gives these results with holes as objects:
My script result:
I thought for a while that gimp crashed with samples of 1 pixels, that's how long it took.
I read that python's array module will get even faster results in Akkana Peck's blog: http://shallowsky.com/blog/gimp/pygimp-pixel-ops.html
How do I implement that into my script?
Wow, drawable.get_pixel() is approximately 6,5 times faster than pdb.gimp_color_picker, thanks for pointing me to that.
the script looks like this now:
Code:
import time
img = gimp.image_list()[0]
def objects_to_layers(image, smp=10):
time_start = time.time()
img = image
sample = smp
counter = 1
newlayer = pdb.gimp_layer_copy( img.active_layer, pdb.gimp_drawable_has_alpha( img.active_layer ) )
img.add_layer(newlayer)
for y in range(0, img.height , sample):
for x in range(0, img.width, sample):
#alphacheck = pdb.gimp_color_picker(img, img.active_layer, x, y, 0,0,0)
alphacheck = img.active_layer.get_pixel(x,y)
if alphacheck[-1] > 0:
pdb.gimp_fuzzy_select(img.active_layer, x, y, 254.9 , 2,0,0, 0,0)
src_layer = img.active_layer
newlayer = pdb.gimp_layer_copy( img.active_layer, pdb.gimp_drawable_has_alpha( img.active_layer ) )
img.add_layer(newlayer)
_, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img)
newlayer.name = "Layer %03d (%d, %d, %d, %d)" % (counter, x1, y1, x2 - x1, y2 - y1)
counter += 1
pdb.gimp_layer_resize(img.active_layer, x2 - x1, y2 - y1, -x1, -y1)
img.active_layer = src_layer
pdb.gimp_edit_clear(src_layer)
img.remove_layer(src_layer)
time_end = time.time()
print ('time taken: ' + str(time_end - time_start) + ' seconds.')
results as to speed with samples of 10, 5 and 1 pixels for a 1024x1024 image with 5 objects:
➤> objects_to_layers(img)
time taken: 2.11100006104 seconds.
➤> objects_to_layers(img, 5)
time taken: 7.57000017166 seconds.
➤> objects_to_layers(img, 1)
time taken: 181.577000141 seconds.
Also your script "Layer > Extract Objects > Extract objects to layers..." gives these results with holes as objects:
My script result:
I thought for a while that gimp crashed with samples of 1 pixels, that's how long it took.
I read that python's array module will get even faster results in Akkana Peck's blog: http://shallowsky.com/blog/gimp/pygimp-pixel-ops.html
How do I implement that into my script?