OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does my python script not access global variables properly?

  • Thread starter Thread starter a rand0m dude
  • Start date Start date
A

a rand0m dude

Guest
My python script is supposed to create a video, but while it works correctly for everything else, the first frame provides a desired output but is seperated from the rest.

Online, I viewd a youtube video explaining an interesting optical illusion. A line would move across the screen, inverting the pixels instead of coloring them. This resulted in a human being able to recognise the moving line, but upon pausing, it would be seen as just static. In an attempt to recreate this effect, I created this script. However, the first frame creates a line as desired, but the rest of the code creates a brand new line, that is way too short but the rest continues to be modifications on that line as desired. The code attached shows my entire script.

My code:

Code:
import cv2
import numpy as np
import random

WIDTH = 1000
HEIGHT = 1000
FRAMES = 6000
FPS = 60
x1, y1 = random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1)
x2, y2 = random.randint(0, WIDTH - 1), 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)

def modify_positions():
    global x1, y1, x2, y2
    while True:
        mx1, my1 = x1 + random.randint(-5, 5), y1 + random.randint(-5, 5)
        mx2, my2 = x2 + random.randint(-5, 5), y2 + random.randint(-5, 5)
        if 0 <= mx1 < WIDTH and 0 <= my1 < HEIGHT and 0 <= mx2 < WIDTH and 0 <= my2 < HEIGHT:
            x1, y1 = mx1, my1
            x2, y2 = mx2, my2
            break

def invert_affected_pixels(frame, affected_pixels):
    for pixel in affected_pixels:
        x, y = pixel
        frame[y, x] = 255 - frame[y, x]  # Invert pixel color

for frame_num in range(1, FRAMES):e
    current_frame = np.copy(frame)

    modify_positions()

    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

    while True:
        affected_pixels.append((x1, y1))
        if x1 == x2 and y1 == y2:
            break
        e2 = err * 2
        if e2 > -dy:
            err -= dy
            x1 += sx
        if e2 < dx:
            err += dx
            y1 += sy

    invert_affected_pixels(current_frame, affected_pixels)

    video_writer.write(current_frame)
    frame = current_frame

video_writer.release()
cv2.destroyAllWindows()

print(f"Video saved as {video_name}")
<p>My python script is supposed to create a video, but while it works correctly for everything else, the first frame provides a desired output but is seperated from the rest.</p>
<p>Online, I viewd a youtube video explaining an interesting optical illusion. A line would move across the screen, inverting the pixels instead of coloring them. This resulted in a human being able to recognise the moving line, but upon pausing, it would be seen as just static. In an attempt to recreate this effect, I created this script. However, the first frame creates a line as desired, but the rest of the code creates a brand new line, that is way too short but the rest continues to be modifications on that line as desired. The code attached shows my entire script.</p>
<p>My code:</p>
<pre><code>import cv2
import numpy as np
import random

WIDTH = 1000
HEIGHT = 1000
FRAMES = 6000
FPS = 60
x1, y1 = random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1)
x2, y2 = random.randint(0, WIDTH - 1), 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)

def modify_positions():
global x1, y1, x2, y2
while True:
mx1, my1 = x1 + random.randint(-5, 5), y1 + random.randint(-5, 5)
mx2, my2 = x2 + random.randint(-5, 5), y2 + random.randint(-5, 5)
if 0 <= mx1 < WIDTH and 0 <= my1 < HEIGHT and 0 <= mx2 < WIDTH and 0 <= my2 < HEIGHT:
x1, y1 = mx1, my1
x2, y2 = mx2, my2
break

def invert_affected_pixels(frame, affected_pixels):
for pixel in affected_pixels:
x, y = pixel
frame[y, x] = 255 - frame[y, x] # Invert pixel color

for frame_num in range(1, FRAMES):e
current_frame = np.copy(frame)

modify_positions()

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

while True:
affected_pixels.append((x1, y1))
if x1 == x2 and y1 == y2:
break
e2 = err * 2
if e2 > -dy:
err -= dy
x1 += sx
if e2 < dx:
err += dx
y1 += sy

invert_affected_pixels(current_frame, affected_pixels)

video_writer.write(current_frame)
frame = current_frame

video_writer.release()
cv2.destroyAllWindows()

print(f"Video saved as {video_name}")

</code></pre>
 

Latest posts

Top