简体   繁体   中英

"Rotate" array ordering with numpy

I'm trying to figure out how to order in numpy this kind of situation:

First I created image blocks with two for loops (just for example):

cuts = 9 # This is for example, there could be much more blocks
for row in range(cuts):
    for column in range(cuts):
        block_min_x = (1 / cuts) * row
        block_max_x = (1 / cuts) * (row + 1)
        block_min_y = (1 / cuts) * column
        block_max_y = (1 / cuts) * (column + 1)

Then I add those "image blocks" to array with names like "img_1_1, img_1_2, img_1_3, img_2_1, img_2_2, img_2_3..." in that order. So it looks like this in array (example with numbers):

[6,7,8] [15,16,17] [24,25,26]
[3,4,5] [12,13,14] [21,22,23]
[0,1,2] [9,10,11]  [18,19,20]

Is there somekind of method to order those blocks to like this with numpy:

[0,1,2]     [3,4,5]     [6,7,8]
[9,10,11]   [12,13,14]  [15,16,17]
[18,19,20]  [21,22,23]  [24,25,26]

And here is image to show it better what I'm looking for:

订购示例图片

I'm not sure if there is somekind of term for this kind of problem, so apologize cannot use right tems. Problem seems to be like we need to "rotate clockwise" that ordering as you see in that blue line in example image. So the question is, how to order that in numpy? Also if it could changed in those for loops would be nice to know.

Usenumpy.rot90 :

In [11]: a = np.array([[6,7,8],[3,4,5], [0,1,2]])

In [12]: b = np.array([[15,16,17], [12,13,14], [9,10,11]])

In [13]: c = np.array([[24,25,26], [21,22,23], [18,19,20]])

In [14]: print(np.array([a, b, c]))
[[[ 6  7  8]
  [ 3  4  5]
  [ 0  1  2]]

 [[15 16 17]
  [12 13 14]
  [ 9 10 11]]

 [[24 25 26]
  [21 22 23]
  [18 19 20]]]

In [15]: print(np.rot90([a, b, c]))
[[[ 0  1  2]
  [ 9 10 11]
  [18 19 20]]

 [[ 3  4  5]
  [12 13 14]
  [21 22 23]]

 [[ 6  7  8]
  [15 16 17]
  [24 25 26]]]

Even though this is not done with a numpy, still can be useful for someone. I got the correct order by looping them like this. I'm sure that this could be done better in code but it seems this works now:

# Order rendered images (to 1, 2, 3, 4 from 3, 1, 2, 4)
rendered_images_ordered = []            
for y in range(parts_count, 0, -1):
    for x in range(parts_count):
        rendered_images_ordered.append(rendered_images[(y-1)+(x*parts_count)])

Output before :

['Fart_1_1.png', 'Fart_1_2.png', 'Fart_1_3.png', 'Fart_2_1.png', 'Fart_2_2.png', 'Fart_2_3.png', 'Fart_3_1.png', 'Fart_3_2.png', 'Fart_3_3.png']

前

Output after :

['Fart_1_3.png', 'Fart_2_3.png', 'Fart_3_3.png', 'Fart_1_2.png', 'Fart_2_2.png', 'Fart_3_2.png', 'Fart_1_1.png', 'Fart_2_1.png', 'Fart_3_1.png']

后

And if interested more about where I use this. I created this to blender add-on called RenderFarts . There I need this ordering in image merge function where I render parts and need to put those to the correct order. Merge process not work yet, but this ordering seems to work properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM