Advertisement
furas

Python - animated line - (Stackoverflow)

Jun 21st, 2024
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. # date: 2024.06.22
  2. # [Why does my python script not access global variables properly? - Stack Overflow](https://stackoverflow.com/questions/78654048/why-does-my-python-script-not-access-global-variables-properly)
  3.  
  4. import cv2
  5. import numpy as np
  6. import random
  7.  
  8. # --- constants ---
  9.  
  10. WIDTH = 1000
  11. HEIGHT = 1000
  12. FRAMES = 6000
  13. FPS = 60
  14.  
  15. # --- functions ---
  16.  
  17. def modify_positions(x1, y1, x2, y2):
  18.     while True:
  19.         mx1 = x1 + random.randint(-5, 5)
  20.         my1 = y1 + random.randint(-5, 5)
  21.         mx2 = x2 + random.randint(-5, 5)
  22.         my2 = y2 + random.randint(-5, 5)
  23.  
  24.         if 0 <= mx1 < WIDTH and 0 <= my1 < HEIGHT and 0 <= mx2 < WIDTH and 0 <= my2 < HEIGHT:
  25.             x1 = mx1
  26.             y1 = my1
  27.             x2 = mx2
  28.             y2 = my2
  29.             break
  30.  
  31.     return x1, y1, x2, y2
  32.  
  33. # --- main ---
  34.  
  35. x1 = random.randint(0, WIDTH - 1)
  36. y1 = random.randint(0, HEIGHT - 1)
  37. x2 = random.randint(0, WIDTH - 1)
  38. y2 = random.randint(0, HEIGHT - 1)
  39.  
  40. frame = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
  41.  
  42. video_name = 'output_video.avi'
  43. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  44. video_writer = cv2.VideoWriter(video_name, fourcc, FPS, (WIDTH, HEIGHT))
  45.  
  46. video_writer.write(frame)
  47.  
  48. for frame_num in range(1, FRAMES):
  49.  
  50.     # display some progress
  51.     if frame_num % 10 == 0:
  52.         #print('.', end='', flush=True)
  53.         print(f'{frame_num:05}', end='\r', flush=True)
  54.        
  55.     current_frame = np.copy(frame)
  56.  
  57.     x1, y1, x2, y2 = modify_positions(x1, y1, x2, y2)
  58.  
  59.     affected_pixels = []
  60.     dx = abs(x2 - x1)
  61.     dy = abs(y2 - y1)
  62.     sx = 1 if x1 < x2 else -1
  63.     sy = 1 if y1 < y2 else -1
  64.     err = dx - dy
  65.  
  66.     temp_x = x1
  67.     temp_y = y1
  68.     while True:
  69.         affected_pixels.append((temp_x, temp_y))
  70.        
  71.         if temp_x == x2 and temp_y == y2:
  72.             break
  73.            
  74.         e2 = err * 2
  75.        
  76.         if e2 > -dy:
  77.             err -= dy
  78.             temp_x += sx
  79.            
  80.         if e2 < dx:
  81.             err += dx
  82.             temp_y += sy
  83.  
  84.     # invert_affected_pixels
  85.     for x, y in affected_pixels:
  86.         current_frame[y, x] = 255 - current_frame[y, x]  # Invert pixel color
  87.  
  88.     video_writer.write(current_frame)
  89.     frame = current_frame
  90.  
  91. video_writer.release()
  92.  
  93. print(f"Video saved as {video_name}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement