import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
# Load the uploaded image
image_path = "/mnt/data/bg_home.png"
bg_image = Image.open(image_path)
# Convert image to numpy array
bg_array
= np
.array(bg_image
)
# Create figure and axis for animation
fig, ax = plt.subplots(figsize=(6, 10))
ax.set_xlim(0, bg_array.shape[1])
ax.set_ylim(bg_array.shape[0], 0)
ax.axis("off")
# Display the background image
img_display = ax.imshow(bg_array, animated=True)
# Generate bubble positions
num_bubbles = 15
bubble_positions = np.column_stack((
np.random.randint(50, bg_array.shape[1] - 50, size=num_bubbles),
np.random.randint(0, bg_array.shape[0], size=num_bubbles)
))
bubble_scatters = ax.scatter(bubble_positions[:, 0], bubble_positions[:, 1], c='white', s=20, alpha=0.5)
# Animation function
def animate(frame):
# Move bubbles upwards
bubble_positions[:, 1] -= 3
bubble_positions[:, 1] = np.where(bubble_positions[:, 1] < 0, bg_array.shape[0], bubble_positions[:, 1])
bubble_scatters.set_offsets(bubble_positions)
return img_display, bubble_scatters
# Create the animation (10 seconds loop, 20 fps)
ani = animation.FuncAnimation(fig, animate, frames=200, interval=50, blit=True)
# Save as MP4
output_mp4_path = "/mnt/data/animated_water.mp4"
ani.save(output_mp4_path, writer="ffmpeg", fps=20)
# Provide the MP4 file path
output_mp4_path
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHQKaW1wb3J0IG1hdHBsb3RsaWIuYW5pbWF0aW9uIGFzIGFuaW1hdGlvbgpmcm9tIFBJTCBpbXBvcnQgSW1hZ2UKCiMgTG9hZCB0aGUgdXBsb2FkZWQgaW1hZ2UKaW1hZ2VfcGF0aCA9ICIvbW50L2RhdGEvYmdfaG9tZS5wbmciCmJnX2ltYWdlID0gSW1hZ2Uub3BlbihpbWFnZV9wYXRoKQoKIyBDb252ZXJ0IGltYWdlIHRvIG51bXB5IGFycmF5CmJnX2FycmF5ID0gbnAuYXJyYXkoYmdfaW1hZ2UpCg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GVfcG9zaXRpb25zKQoKICAgIHJldHVybiBpbWdfZGlzcGxheSwgYnViYmxlX3NjYXR0ZXJzCgojIENyZWF0ZSB0aGUgYW5pbWF0aW9uICgxMCBzZWNvbmRzIGxvb3AsIDIwIGZwcykKYW5pID0gYW5pbWF0aW9uLkZ1bmNBbmltYXRpb24oZmlnLCBhbmltYXRlLCBmcmFtZXM9MjAwLCBpbnRlcnZhbD01MCwgYmxpdD1UcnVlKQoKIyBTYXZlIGFzIE1QNApvdXRwdXRfbXA0X3BhdGggPSAiL21udC9kYXRhL2FuaW1hdGVkX3dhdGVyLm1wNCIKYW5pLnNhdmUob3V0cHV0X21wNF9wYXRoLCB3cml0ZXI9ImZmbXBlZyIsIGZwcz0yMCkKCiMgUHJvdmlkZSB0aGUgTVA0IGZpbGUgcGF0aApvdXRwdXRfbXA0X3BhdGgK
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
# Load the uploaded image
image_path = "/mnt/data/bg_home.png"
bg_image = Image.open(image_path)
# Convert image to numpy array
bg_array = np.array(bg_image)
# Create figure and axis for animation
fig, ax = plt.subplots(figsize=(6, 10))
ax.set_xlim(0, bg_array.shape[1])
ax.set_ylim(bg_array.shape[0], 0)
ax.axis("off")
# Display the background image
img_display = ax.imshow(bg_array, animated=True)
# Generate bubble positions
num_bubbles = 15
bubble_positions = np.column_stack((
np.random.randint(50, bg_array.shape[1] - 50, size=num_bubbles),
np.random.randint(0, bg_array.shape[0], size=num_bubbles)
))
bubble_scatters = ax.scatter(bubble_positions[:, 0], bubble_positions[:, 1], c='white', s=20, alpha=0.5)
# Animation function
def animate(frame):
# Move bubbles upwards
bubble_positions[:, 1] -= 3
bubble_positions[:, 1] = np.where(bubble_positions[:, 1] < 0, bg_array.shape[0], bubble_positions[:, 1])
bubble_scatters.set_offsets(bubble_positions)
return img_display, bubble_scatters
# Create the animation (10 seconds loop, 20 fps)
ani = animation.FuncAnimation(fig, animate, frames=200, interval=50, blit=True)
# Save as MP4
output_mp4_path = "/mnt/data/animated_water.mp4"
ani.save(output_mp4_path, writer="ffmpeg", fps=20)
# Provide the MP4 file path
output_mp4_path