0

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:

Ordering Example Image

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.

1
  • 2
    rot90 is what you should use since it's more readable, but it's worth realizing that you can do a rotation with a[::-1].T, ie "flip vertically then flip diagonally". Commented Feb 9, 2020 at 13:03

2 Answers 2

1

Use numpy.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]]]
Sign up to request clarification or add additional context in comments.

Comments

0

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']

Before

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']

After

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.