This commit is contained in:
Hannes
2026-07-20 20:51:09 +02:00
parent fe9072221b
commit 4503f99135
7 changed files with 677 additions and 86 deletions
+36 -34
View File
@@ -1,12 +1,21 @@
import random
import os
import cv2
import numpy as np
from PIL import Image
width = 1280
height = 720
width = 200
height = 400
fps = 30 # Frames per second
total_frames = 240 # Increased to actually see the animation move
img = Image.new("RGB", (width, height))
# Ensure output directory exists
os.makedirs("output", exist_ok=True)
pixels = img.load()
# Define the VideoWriter
# 'mp4v' is a widely supported MP4 codec
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_path = "output/rainbow.mp4"
video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
colors = (
(0xFF, 0x00, 0x00),
@@ -16,37 +25,30 @@ colors = (
(0x00, 0xFF, 0xFF),
(0x00, 0x00, 0xFF),
(0x8B, 0x00, 0xFF),
(0xFF, 0x00, 0xFF)
)
colors2 = (
(0xFF,0x00,0x00),
(0xFF,0x40,0x00),
(0xFF,0x80,0x00),
(0xFF,0xBF,0x00),
(0xFF,0xFF,0x00),
(0xBF,0xFF,0x00),
(0x80,0xFF,0x00),
(0x00,0xFF,0x00),
(0x00,0xFF,0x80),
(0x00,0xFF,0xFF),
(0x00,0x80,0xFF),
(0x00,0x00,0xFF),
(0x40,0x00,0xFF),
(0x80,0x00,0xFF),
(0xFF,0x00,0xFF),
(0xFF,0x00,0x80)
(0xFF, 0x00, 0xFF),
)
for y in range(height):
for x in range(width):
pixels[x, y] = colors[((x+y)>>4)&7]
img = Image.new("RGB", (width, height))
pixels = img.load()
img.save("output/rainbow.png")
img.show()
frame_num = 0
while frame_num < total_frames:
for y in range(height):
for x in range(width):
# Adjusted the bit-shift so the animation moves visibly frame-by-frame
pixels[x, y] = colors[((x + y + (frame_num >> 1)) >> 4) & 7]
for y in range(height):
for x in range(width):
pixels[x, y] = colors2[((x+y)>>4)&15]
# 1. Convert PIL Image to a NumPy array
frame_np = np.array(img)
img.save("output/rainbow2.png")
img.show()
# 2. Convert RGB (PIL format) to BGR (OpenCV format)
frame_bgr = cv2.cvtColor(frame_np, cv2.COLOR_RGB2BGR)
# 3. Write the frame to the video file
video.write(frame_bgr)
frame_num += 1
# Crucial: Release the video writer to finalize and save the file
video.release()
print(f"Video saved successfully to {video_path}")