Guest User

Untitled

a guest
Nov 21st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. def rotate(img,angle):
  2. cos=math.cos(angle/180*math.pi)
  3. sin=math.sin(angle/180*math.pi)
  4. h,w=img.shape[0], img.shape[1]
  5. print(cos,sin)
  6. new=np.zeros((math.ceil(abs(w*sin)+abs(h*cos)),math.ceil(abs(w*cos)+abs(h*sin)),3))
  7. offset_i=(w*sin if sin>0 else 0)+(-h*cos if cos<0 else 0)
  8. offset_j=(-w*cos if cos<0 else 0)+(-h*sin if sin<0 else 0)
  9. for i in range(h):
  10. for j in range(w):
  11. if i==j and i==0:
  12. continue
  13. l=math.sqrt(i*i+j*j)
  14. cosa=j/l
  15. sina=i/l
  16. cosb=cosa*cos+sina*sin
  17. sinb=sina*cos-cosa*sin
  18.  
  19. newi=int(l*sinb+offset_i)
  20. newj=int(l*cosb+offset_j)
  21. 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]
  22. return new
Add Comment
Please, Sign In to add comment