Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching for python script islands to layers
#18
Ok, so I've been looking at sample scripts of jfmdev at "http://registry.gimp.org/node/28124"
very insteresting are scripts "test-discolour-layer-v3.py" which loops over pixels with arrays and "test-discolour-layer-v4.py" which loops over pixels with tiles.

Looping over pixels with array module:


Code:
import time
import array

def loop_array():
   time_start = time.time()
   img = gimp.image_list()[0]
   layer = img.active_layer
   srcRgn = layer.get_pixel_rgn(0, 0, layer.width, layer.height, False, False)
   pixelSize = len(srcRgn[0,0])
   srcArray = array.array("B", srcRgn[0:layer.width, 0:layer.height])
   for y in range(layer.height):
       for x in range(layer.width):
           # Get the pixel
           pos = (x * layer.height + y) * pixelSize
           pixel = srcArray[pos], srcArray[pos+1], srcArray[pos+2], srcArray[pos+3]
   time_end = time.time()
   print ('time taken: ' + str(time_end - time_start) + ' seconds.')

loop_array()

➤> loop_array()
time taken: 20.2019999027 seconds.
20 seconds is pretty bad for looping over 1024x1024 image.

looping over pixels with tiles:


Code:
import time

def loop_tiles():
   time_start = time.time()
   img = gimp.image_list()[0]
   layer = img.active_layer
   tn = int(layer.width / 64)
   if(layer.width % 64 > 0):
       tn += 1
   tm = int(layer.height / 64)
   if(layer.height % 64 > 0):
       tm += 1
   for i in range(tn):
       for j in range(tm):
           srcTile = layer.get_tile(False, j, i)
           for x in range(srcTile.ewidth):
               for y in range(srcTile.eheight):
                   # Get the pixel
                   pixel = srcTile[x,y]
   print( ord( pixel[-1] ))
   time_end = time.time()
   print ('time taken: ' + str(time_end - time_start) + ' seconds.')

loop_tiles()
➤>time taken: 0.218999862671 seconds.


Wow, 0.21 seconds, looping over pixels with tiles is definitely the way to go.

Ok, so now my questions are:

1) How do I translate a tile's pixel position to it's layer's global pixel position so I can select it with "pdb.gimp_fuzzy_select(img.active_layer, <x_position_of_pixel>, <y_position_of_pixel>, 254.9 , 2,0,0, 0,0)" ?

2) How do I exit the loop, update the layer by clearing selection, update layer, and resume the loop from last tile?

f.e. I want to accomplishthese steps for a 1024x1024 image with 16x16 tiles:
-I find a non-transparent pixel at tile(3,4)
-I exit the tile loop
-select that pixel and neighboring non-transparent pixels with 'pdb.gimp_fuzzy_select'
-do some stuff
-update the layer
-and then resume tile loop at tile(3,4) until tile(16,16).
-script hopefully ends succesfully.
Reply


Messages In This Thread
RE: Searching for python script islands to layers - by mich_lloid - 01-23-2018, 10:13 PM

Forum Jump: