Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rotate(img,angle):
- cos=math.cos(angle/180*math.pi)
- sin=math.sin(angle/180*math.pi)
- h,w=img.shape[0], img.shape[1]
- print(cos,sin)
- new=np.zeros((math.ceil(abs(w*sin)+abs(h*cos)),math.ceil(abs(w*cos)+abs(h*sin)),3))
- offset_i=(w*sin if sin>0 else 0)+(-h*cos if cos<0 else 0)
- offset_j=(-w*cos if cos<0 else 0)+(-h*sin if sin<0 else 0)
- for i in range(h):
- for j in range(w):
- if i==j and i==0:
- continue
- l=math.sqrt(i*i+j*j)
- cosa=j/l
- sina=i/l
- cosb=cosa*cos+sina*sin
- sinb=sina*cos-cosa*sin
- newi=int(l*sinb+offset_i)
- newj=int(l*cosb+offset_j)
- new[newi if 0<=newi<new.shape[0] else new.shape[0]-1][newj if 0<=newj<new.shape[1] else new.shape[1]-1]=img[i][j]
- return new
Add Comment
Please, Sign In to add comment