Advertisement
yclee126

Image rotation iterator

Jun 10th, 2024
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. # Image rotation iterator
  2.  
  3. # Original video & blog post by jk-keller
  4. # https://www.youtube.com/watch?v=1UuQCDgiqVg
  5. # https://jk-keller.com/o__o/rotational_growth/
  6.  
  7. # Python code by yclee126
  8.  
  9. import cv2
  10. import numpy as np
  11. import time
  12.  
  13.  
  14. ### User Settings ###
  15.  
  16. image_filename = 'image.png'
  17.  
  18. angle_increment = 10                # degrees, (+): counter clockwise, (-): clockwise
  19. rotation_count = 100
  20. interpolation = cv2.INTER_CUBIC     # see interpolation parameter table below
  21.  
  22. display_frames = 1                  # True(1) or False(0) -- (keys) q: quit, p: pause, s: skip
  23. frame_delay = 1                     # milliseconds, 0: frame-by-frame, 1: fastest
  24.  
  25. padding_color = (255, 255, 255)     # BGR order
  26. extra_padding = 10  # pixels
  27.  
  28. # interpolation params
  29. #
  30. # INTER_NEAREST  (nearest neighbor interpolation)
  31. # INTER_LINEAR   (bilinear interpolation)
  32. # INTER_CUBIC    (bicubic interpolation)
  33. # INTER_AREA     (resampling using pixel area relation)
  34. # INTER_LANCZOS4 (Lanczos interpolation over 8x8 neighborhood)
  35.  
  36.  
  37. ### Image processing codes ###
  38.  
  39. img = cv2.imread(image_filename)
  40.  
  41. h, w, *c = img.shape
  42. if len(c) == 0: # grayscale image
  43.     img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  44.  
  45. # make canvas that can fit the diagonal line of the image + extra padding
  46. final_size = int((h*h + w*w)**(1/2) + extra_padding*2)
  47. canvas = np.zeros((final_size, final_size, 3), dtype='uint8')
  48. canvas[:, :] = padding_color
  49.  
  50. # paste image in the center
  51. x_offset = int((final_size - w) / 2)
  52. y_offset = int((final_size - h) / 2)
  53. canvas[y_offset:y_offset+h, x_offset:x_offset+w] = img
  54.  
  55. # calc infos
  56. iterations = abs(round(360 / angle_increment * rotation_count))
  57.  
  58. print(f'Increment: {angle_increment} deg, Rotations: {rotation_count}, Iterations: {iterations}')
  59. print_progress = lambda i: print(f'Progress: {i/iterations*100:.2f}% (r:{int(abs(i*angle_increment) / 360)}, i:{i})', end='\r')
  60.  
  61. # main rotation loop
  62. rotated = canvas
  63. prev_print_time = time.time()
  64. for i in range(iterations):
  65.  
  66.     # print progress
  67.     if time.time() - prev_print_time > 0.1 or display_frames: # prevent print() slowing down calculation
  68.         print_progress(i)
  69.         prev_print_time = time.time()
  70.    
  71.     # rotate canvas
  72.     matrix = cv2.getRotationMatrix2D((final_size/2, final_size/2), angle_increment, 1.0)
  73.     rotated = cv2.warpAffine(
  74.         rotated,
  75.         matrix,
  76.         (final_size, final_size),
  77.         None,
  78.         interpolation,
  79.         cv2.BORDER_CONSTANT,
  80.         padding_color)
  81.  
  82.     if display_frames:
  83.         # display frame
  84.         cv2.imshow('win', rotated)
  85.         key = cv2.waitKey(frame_delay)
  86.  
  87.         # process pause input first
  88.         if key == ord('p'):
  89.             key = cv2.waitKey(0)
  90.  
  91.         # process other key inputs
  92.         if key == ord('q'):
  93.             exit()
  94.         elif key == ord('s'):
  95.             display_frames = False
  96.             cv2.destroyAllWindows()
  97.  
  98. # display final frame
  99. print_progress(iterations)
  100. cv2.imshow('win', rotated)
  101. cv2.waitKey(0)
  102.  
  103. # save image
  104. cv2.imwrite('output.png', rotated)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement