Skip to content
Advertisement

How to rotate square numpy array of 2 dimensional by 45 degree in python?

I have a numpy array of images. The dimension is 2 and the shape is (100,100). I want to augment more data as I have only 52 set of numpy array. I want to rotate the given array by 45 degree. What should I do for that??

Suppose the array be like

a=[[ 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]]

Please rotate the given array by 45 degree.

Advertisement

Answer

You can use scipy.ndimage.rotate

import numpy as np
from scipy.ndimage import rotate
x = np.arange(25).reshape(5, -1)
rotate(x, angle=45)

Output

array([[ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  4,  9, 14,  0,  0],
       [ 0,  3,  8, 12, 16, 21,  0],
       [ 0,  0, 10, 15, 20,  0,  0],
       [ 0,  0,  0, 18,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0]])
Advertisement