fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. from PIL import Image
  5.  
  6. # Load the uploaded image
  7. image_path = "/mnt/data/bg_home.png"
  8. bg_image = Image.open(image_path)
  9.  
  10. # Convert image to numpy array
  11. bg_array = np.array(bg_image)
  12.  
  13. # Create figure and axis for animation
  14. fig, ax = plt.subplots(figsize=(6, 10))
  15. ax.set_xlim(0, bg_array.shape[1])
  16. ax.set_ylim(bg_array.shape[0], 0)
  17. ax.axis("off")
  18.  
  19. # Display the background image
  20. img_display = ax.imshow(bg_array, animated=True)
  21.  
  22. # Generate bubble positions
  23. num_bubbles = 15
  24. bubble_positions = np.column_stack((
  25. np.random.randint(50, bg_array.shape[1] - 50, size=num_bubbles),
  26. np.random.randint(0, bg_array.shape[0], size=num_bubbles)
  27. ))
  28. bubble_scatters = ax.scatter(bubble_positions[:, 0], bubble_positions[:, 1], c='white', s=20, alpha=0.5)
  29.  
  30. # Animation function
  31. def animate(frame):
  32. # Move bubbles upwards
  33. bubble_positions[:, 1] -= 3
  34. bubble_positions[:, 1] = np.where(bubble_positions[:, 1] < 0, bg_array.shape[0], bubble_positions[:, 1])
  35. bubble_scatters.set_offsets(bubble_positions)
  36.  
  37. return img_display, bubble_scatters
  38.  
  39. # Create the animation (10 seconds loop, 20 fps)
  40. ani = animation.FuncAnimation(fig, animate, frames=200, interval=50, blit=True)
  41.  
  42. # Save as MP4
  43. output_mp4_path = "/mnt/data/animated_water.mp4"
  44. ani.save(output_mp4_path, writer="ffmpeg", fps=20)
  45.  
  46. # Provide the MP4 file path
  47. output_mp4_path
  48.  
Success #stdin #stdout 0.03s 25896KB
stdin
Standard input is empty
stdout
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