Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # date: 2024.06.22
- # [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)
- import cv2
- import numpy as np
- import random
- # --- constants ---
- WIDTH = 1000
- HEIGHT = 1000
- FRAMES = 6000
- FPS = 60
- # --- functions ---
- def modify_positions(x1, y1, x2, y2):
- while True:
- mx1 = x1 + random.randint(-5, 5)
- my1 = y1 + random.randint(-5, 5)
- mx2 = x2 + random.randint(-5, 5)
- my2 = y2 + random.randint(-5, 5)
- if 0 <= mx1 < WIDTH and 0 <= my1 < HEIGHT and 0 <= mx2 < WIDTH and 0 <= my2 < HEIGHT:
- x1 = mx1
- y1 = my1
- x2 = mx2
- y2 = my2
- break
- return x1, y1, x2, y2
- # --- main ---
- x1 = random.randint(0, WIDTH - 1)
- y1 = random.randint(0, HEIGHT - 1)
- x2 = random.randint(0, WIDTH - 1)
- y2 = random.randint(0, HEIGHT - 1)
- frame = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
- video_name = 'output_video.avi'
- fourcc = cv2.VideoWriter_fourcc(*'XVID')
- video_writer = cv2.VideoWriter(video_name, fourcc, FPS, (WIDTH, HEIGHT))
- video_writer.write(frame)
- for frame_num in range(1, FRAMES):
- # display some progress
- if frame_num % 10 == 0:
- #print('.', end='', flush=True)
- print(f'{frame_num:05}', end='\r', flush=True)
- current_frame = np.copy(frame)
- x1, y1, x2, y2 = modify_positions(x1, y1, x2, y2)
- affected_pixels = []
- dx = abs(x2 - x1)
- dy = abs(y2 - y1)
- sx = 1 if x1 < x2 else -1
- sy = 1 if y1 < y2 else -1
- err = dx - dy
- temp_x = x1
- temp_y = y1
- while True:
- affected_pixels.append((temp_x, temp_y))
- if temp_x == x2 and temp_y == y2:
- break
- e2 = err * 2
- if e2 > -dy:
- err -= dy
- temp_x += sx
- if e2 < dx:
- err += dx
- temp_y += sy
- # invert_affected_pixels
- for x, y in affected_pixels:
- current_frame[y, x] = 255 - current_frame[y, x] # Invert pixel color
- video_writer.write(current_frame)
- frame = current_frame
- video_writer.release()
- print(f"Video saved as {video_name}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement