01-24-2018, 04:58 PM
Thanks Ofnuts!
Wow, looping over tiles is ridiculously faster than standard looping with python, 5 seconds versus 3 minutes with a 1024x1024 image with 5 objects sampling every pixel.
The script now looks like this, any suggestions to make script better would be really welcome:
I tested with a large 4096 x 4096 image with 194 objects on them, took 24 seconds to get them to seperate layers, I won't test with old script, will probably take at least 10 minutes...
I tried to update tiles directly with active layer in image, but the tiles wouldn't update, that's why i used a temp_layer copy of active_image.
Can you tell me why this is or what I probably did wrong?
Wow, looping over tiles is ridiculously faster than standard looping with python, 5 seconds versus 3 minutes with a 1024x1024 image with 5 objects sampling every pixel.
The script now looks like this, any suggestions to make script better would be really welcome:
Code:
import time
def objects_to_layers(img, sample = 1):
time_start = time.time()
#samples = 2
#img = gimp.image_list()[0]
src_layer = img.active_layer
layer = src_layer.copy()
img.add_layer(layer)
counter = 1
temp_layer = layer.copy()
tile_rows = int(layer.width / 64)
if(layer.width % 64 > 0):
tile_rows += 1
tile_cols = int(layer.height / 64)
if(layer.width % 64 > 0):
tile_cols += 1
for tile_row in range(tile_rows):
for tile_col in range(tile_cols):
#print(tile_row, tile_col)
#srcTile = layer.get_tile(False, tile_row, tile_col)
srcTile = temp_layer.get_tile(False, tile_row, tile_col)
if srcTile != None:
for tile_x in range(0, srcTile.ewidth, sample):
for tile_y in range(0, srcTile.eheight, sample):
pixel = srcTile[tile_x, tile_y]
# if pixel is not completely transparent
if ord( pixel[3] ) > 0:
layer_pixel_pos_x = tile_col * 64 + tile_x
layer_pixel_pos_y = tile_row * 64 + tile_y
pdb.gimp_fuzzy_select(img.active_layer, layer_pixel_pos_x, layer_pixel_pos_y, 254.9 , 2,0,0,0,0)
newlayer = layer.copy()
img.add_layer(newlayer)
x1, y1, x2, y2 = layer.mask_bounds
newlayer.name = "Layer %03d (%d, %d, %d, %d)" % (counter, x1, y1, x2 - x1, y2 - y1)
counter += 1
newlayer.resize(x2 - x1, y2 - y1, -x1, -y1)
img.active_layer = layer
pdb.gimp_edit_clear(layer)
#update tile by updating temp_layer
temp_layer = layer.copy()
srcTile = temp_layer.get_tile(False, tile_row, tile_col)
img.remove_layer(layer)
time_end = time.time()
print ('time taken: ' + str(time_end - time_start) + ' seconds.')
img = gimp.image_list()[0]
objects_to_layers(img)
I tested with a large 4096 x 4096 image with 194 objects on them, took 24 seconds to get them to seperate layers, I won't test with old script, will probably take at least 10 minutes...
I tried to update tiles directly with active layer in image, but the tiles wouldn't update, that's why i used a temp_layer copy of active_image.
Can you tell me why this is or what I probably did wrong?