1def rotate(image,a):
2 #image : your image
3 #a : angle to rotate your image
4 angle = - ( a )
5 angle=math.radians(angle)
6 cosine=math.cos(angle)
7 sine=math.sin(angle)
8 height=image.shape[0]
9 width=image.shape[1]
10 # height and width of the new image
11 new_height = round(abs(image.shape[0]*cosine)+abs(image.shape[1]*sine))+1
12 new_width = round(abs(image.shape[1]*cosine)+abs(image.shape[0]*sine))+1
13 #image variable of dimensions of new_height and new _column filled with zeros
14 output=np.zeros((new_height,new_width,image.shape[2]))
15 image_copy=output.copy()
16 #Find the centre of the image about which we have to rotate the image
17 original_centre_height = round(((image.shape[0]+1)/2)-1)
18 original_centre_width = round(((image.shape[1]+1)/2)-1)
19 # Find the centre of the new image that will be obtained
20 new_centre_height= round(((new_height+1)/2)-1)
21 new_centre_width= round(((new_width+1)/2)-1)
22
23 for i in range(height):
24 for j in range(width):
25 #co-ordinates of pixel with respect to the centre of original image
26 y=image.shape[0]-1-i-original_centre_height
27 x=image.shape[1]-1-j-original_centre_width
28 '''
29 #co-ordinate of pixel with respect to the rotated image
30
31 new_y=round(-x*sine+y*cosine)
32 new_x=round(x*cosine+y*sine)
33 '''
34 new_y,new_x=shear(angle,x,y)
35 new_y=new_centre_height-new_y
36 new_x=new_centre_width-new_x
37
38 if 0 <= new_x < new_width and 0 <= new_y < new_height and new_x>=0 and new_y>=0:
39
40 output[new_y,new_x,:]=image[i,j,:]
41 pil_img=Image.fromarray((output).astype(np.uint8))
42
43 pil_img.save("rotated_image.png")
44 img=cv.imread("rotated_image.png")
45 img_2=cv.imread("img.jpg")
46 cv.imshow("Befor Rotate",img_2)
47 cv.imshow("After Rotate",img)
48 cv.waitKey(0)
49
50def shear(angle,x,y):
51 # shear 1
52 tangent=math.tan(angle/2)
53 new_x=round(x-y*tangent)
54 new_y=y
55 #shear 2
56 new_y=round(new_x*math.sin(angle)+new_y) #since there is no change in new_x according to the shear matrix
57 #shear 3
58 new_x=round(new_x-new_y*tangent) #since there is no change in new_y according to the shear matrix
59 return new_y,new_x
60
61
1import skimage
2import skimage.transform
3rotated_img=skimage.transform.rotate(img,-60, resize=True)
1def rotate_image(img, degrees, output_scale="crop"):
2
3 assert output_scale in ["crop", "full"], "output_scale should be either 'crop' or 'full'"
4 # convert rotation amount to radian
5 rot_rad = degrees * np.pi / 180.0
6 rotate_m = np.array([[np.cos(rot_rad), np.sin(rot_rad)],
7 [- np.sin(rot_rad), np.cos(rot_rad)]])
8
9 # If output_scale = "full", the image must be inserted into a bigger frame, so the coordinates would be translated
10 # appropriately.
11 gray_scale = False
12 if len(img.shape) < 3:
13 img = img.reshape(*img.shape, 1)
14 gray_scale = True
15
16 h, w, c = img.shape
17 if output_scale == "full":
18 diagonal = int(np.sqrt(h * h + w * w)) # Pytagoras theorm - the diagonal is the longest line in the rectangle
19 im_padded = np.zeros((diagonal, diagonal, c))
20 center_h = int((diagonal - h) // 2)
21 center_w = int((diagonal - w) // 2)
22 im_padded[center_h:-center_h, center_w:-center_w, :] = img
23 img = im_padded
24 rotated_image = np.zeros((diagonal, diagonal, c))
25 h, w, c = img.shape
26 else:
27 rotated_image = np.zeros((h, w, c))
28
29 # Rotate and shift the indices, from PICTURE to SOURCE (and NOT the intuitive way)
30 indices_org = np.array(np.meshgrid(np.arange(h), np.arange(w))).reshape(2, -1)
31 indices_new = indices_org.copy()
32 indices_new = np.dot(rotate_m, indices_new).astype(int) # Apply the affineWrap
33 mu1 = np.mean(indices_new, axis=1).astype(int).reshape((-1, 1))
34 mu2 = np.mean(indices_org, axis=1).astype(int).reshape((-1, 1))
35 indices_new += (mu2-mu1) # Shift the image back to the center
36
37 # Remove the pixels in the rotated image, that are now out of the bounds of the result image
38 # (Note that the result image is a rectangle of shape (h,w,c) that the rotated image is inserted into, so in the
39 # case of a "full" output_scale, these are just black pixels from the padded image...).
40 t0, t1 = indices_new
41 t0 = (0 <= t0) & (t0 < h)
42 t1 = (0 <= t1) & (t1 < w)
43 valid = t0 & t1
44 indices_new = indices_new.T[valid].T
45 indices_org = indices_org.T[valid].T
46
47 #
48 xind, yind = indices_new
49 xi, yi = indices_org
50 rotated_image[xi, yi, :] = img[xind, yind, :]
51
52 if gray_scale:
53 rotated_image = rotated_image.reshape((h, w))
54
55 return rotated_image.astype(np.uint8)
56
57img = cv2.imread(image_path)
58rotated = rotate_image(img, 45)
59cv2.imshow("Rotated_Image", rotated)
60cv2.waitkey(0)