pixel_art_video.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. """Pixel art video — overlay procedural animations onto a source image.
  2. Takes any image (typically pre-processed with pixel_art()) and overlays
  3. animated pixel effects (stars, rain, fireflies, etc.), then encodes to MP4
  4. (and optionally GIF) via ffmpeg.
  5. Scene animations ported from pixel-art-studio (MIT) — see ATTRIBUTION.md.
  6. The generative/Pollinations code is intentionally dropped — Hermes uses
  7. `image_generate` + `pixel_art()` for base frames instead.
  8. Usage (import):
  9. from pixel_art_video import pixel_art_video
  10. pixel_art_video("frame.png", "out.mp4", scene="night", duration=6)
  11. Usage (CLI):
  12. python pixel_art_video.py frame.png out.mp4 --scene night --duration 6 --gif
  13. """
  14. import math
  15. import os
  16. import random
  17. import shutil
  18. import subprocess
  19. import tempfile
  20. from PIL import Image, ImageDraw
  21. # ── Pixel drawing helpers ──────────────────────────────────────────────
  22. def _px(draw, x, y, color, size=2):
  23. x, y = int(x), int(y)
  24. W, H = draw.im.size
  25. if 0 <= x < W and 0 <= y < H:
  26. draw.rectangle([x, y, x + size - 1, y + size - 1], fill=color)
  27. def _pixel_cross(draw, x, y, color, arm=2):
  28. x, y = int(x), int(y)
  29. for i in range(-arm, arm + 1):
  30. _px(draw, x + i, y, color, 1)
  31. _px(draw, x, y + i, color, 1)
  32. # ── Animation init/draw pairs ──────────────────────────────────────────
  33. def init_stars(rng, W, H):
  34. return [(rng.randint(0, W), rng.randint(0, H // 2)) for _ in range(15)]
  35. def draw_stars(draw, stars, t, W, H):
  36. for i, (sx, sy) in enumerate(stars):
  37. if math.sin(t * 2.0 + i * 0.7) > 0.65:
  38. _pixel_cross(draw, sx, sy, (255, 255, 220), arm=2)
  39. def init_fireflies(rng, W, H):
  40. return [{"x": rng.randint(20, W - 20), "y": rng.randint(H // 4, H - 20),
  41. "phase": rng.uniform(0, 6.28), "speed": rng.uniform(0.3, 0.8)}
  42. for _ in range(10)]
  43. def draw_fireflies(draw, ff, t, W, H):
  44. for f in ff:
  45. if math.sin(t * 1.5 + f["phase"]) < 0.15:
  46. continue
  47. _px(draw,
  48. f["x"] + math.sin(t * f["speed"] + f["phase"]) * 3,
  49. f["y"] + math.cos(t * f["speed"] * 0.7) * 2,
  50. (200, 255, 100), 2)
  51. def init_leaves(rng, W, H):
  52. return [{"x": rng.randint(0, W), "y": rng.randint(-H, 0),
  53. "speed": rng.uniform(0.5, 1.5), "wobble": rng.uniform(0.02, 0.05),
  54. "phase": rng.uniform(0, 6.28),
  55. "color": rng.choice([(180, 120, 50), (160, 100, 40), (200, 140, 60)])}
  56. for _ in range(12)]
  57. def draw_leaves(draw, leaves, t, W, H):
  58. for leaf in leaves:
  59. _px(draw,
  60. leaf["x"] + math.sin(t * leaf["wobble"] + leaf["phase"]) * 15,
  61. (leaf["y"] + t * leaf["speed"] * 20) % (H + 40) - 20,
  62. leaf["color"], 2)
  63. def init_dust_motes(rng, W, H):
  64. return [{"x": rng.randint(30, W - 30), "y": rng.randint(30, H - 30),
  65. "phase": rng.uniform(0, 6.28), "speed": rng.uniform(0.2, 0.5),
  66. "amp": rng.uniform(2, 6)} for _ in range(20)]
  67. def draw_dust_motes(draw, motes, t, W, H):
  68. for m in motes:
  69. if math.sin(t * 2.0 + m["phase"]) > 0.3:
  70. _px(draw,
  71. m["x"] + math.sin(t * 0.3 + m["phase"]) * m["amp"],
  72. m["y"] - (m["speed"] * t * 15) % H,
  73. (255, 210, 100), 1)
  74. def init_sparkles(rng, W, H):
  75. return [(rng.randint(W // 4, 3 * W // 4), rng.randint(H // 4, 3 * H // 4),
  76. rng.uniform(0, 6.28),
  77. rng.choice([(180, 200, 255), (255, 220, 150), (200, 180, 255)]))
  78. for _ in range(10)]
  79. def draw_sparkles(draw, sparkles, t, W, H):
  80. for sx, sy, phase, color in sparkles:
  81. if math.sin(t * 1.8 + phase) > 0.6:
  82. _pixel_cross(draw, sx, sy, color, arm=2)
  83. def init_rain(rng, W, H):
  84. return [{"x": rng.randint(0, W), "y": rng.randint(0, H),
  85. "speed": rng.uniform(4, 8)} for _ in range(30)]
  86. def draw_rain(draw, rain, t, W, H):
  87. for r in rain:
  88. y = (r["y"] + t * r["speed"] * 20) % H
  89. _px(draw, r["x"], y, (120, 150, 200), 1)
  90. _px(draw, r["x"], y + 4, (100, 130, 180), 1)
  91. def init_lightning(rng, W, H):
  92. return {"timer": 0, "flash": False, "rng": rng}
  93. def draw_lightning(draw, state, t, W, H):
  94. state["timer"] += 1
  95. if state["timer"] > 45 and state["rng"].random() < 0.04:
  96. state["flash"] = True
  97. state["timer"] = 0
  98. if state["flash"]:
  99. for x in range(0, W, 4):
  100. for y in range(0, H // 3, 3):
  101. if state["rng"].random() < 0.12:
  102. _px(draw, x, y, (255, 255, 240), 2)
  103. state["flash"] = False
  104. def init_bubbles(rng, W, H):
  105. return [{"x": rng.randint(20, W - 20), "y": rng.randint(H, H * 2),
  106. "speed": rng.uniform(0.3, 0.8), "size": rng.choice([1, 2, 2])}
  107. for _ in range(15)]
  108. def draw_bubbles(draw, bubbles, t, W, H):
  109. for b in bubbles:
  110. x = b["x"] + math.sin(t * 0.5 + b["x"]) * 3
  111. y = b["y"] - (t * b["speed"] * 20) % (H + 40)
  112. if 0 < y < H:
  113. _px(draw, x, y, (150, 200, 255), b["size"])
  114. def init_embers(rng, W, H):
  115. return [{"x": rng.randint(0, W), "y": rng.randint(0, H),
  116. "speed": rng.uniform(0.3, 0.9), "phase": rng.uniform(0, 6.28),
  117. "color": rng.choice([(255, 150, 30), (255, 100, 20), (255, 200, 50)])}
  118. for _ in range(18)]
  119. def draw_embers(draw, embers, t, W, H):
  120. for e in embers:
  121. x = e["x"] + math.sin(t * 0.4 + e["phase"]) * 5
  122. y = e["y"] - (t * e["speed"] * 15) % H
  123. if math.sin(t * 2.5 + e["phase"]) > 0.2:
  124. _px(draw, x, y, e["color"], 2)
  125. def init_snowflakes(rng, W, H):
  126. return [{"x": rng.randint(0, W), "y": rng.randint(-H, 0),
  127. "speed": rng.uniform(0.3, 0.6), "wobble": rng.uniform(0.04, 0.09),
  128. "size": rng.choice([2, 2, 3])}
  129. for _ in range(40)]
  130. def draw_snowflakes(draw, flakes, t, W, H):
  131. for f in flakes:
  132. x = f["x"] + math.sin(t * f["wobble"] + f["x"]) * 20
  133. y = (f["y"] + t * f["speed"] * 8) % (H + 20) - 10
  134. if f["size"] >= 3:
  135. _pixel_cross(draw, x, y, (230, 235, 255), arm=1)
  136. else:
  137. _px(draw, x, y, (230, 235, 255), 2)
  138. def init_neon_pulse(rng, W, H):
  139. return [(rng.randint(0, W), rng.randint(0, H), rng.uniform(0, 6.28),
  140. rng.choice([(255, 0, 200), (0, 255, 255), (255, 50, 150)]))
  141. for _ in range(8)]
  142. def draw_neon_pulse(draw, points, t, W, H):
  143. for x, y, phase, color in points:
  144. if math.sin(t * 2.5 + phase) > 0.5:
  145. _pixel_cross(draw, x, y, color, arm=3)
  146. def init_heat_shimmer(rng, W, H):
  147. return [{"x": rng.randint(0, W), "y": rng.randint(H // 2, H),
  148. "phase": rng.uniform(0, 6.28)} for _ in range(12)]
  149. def draw_heat_shimmer(draw, points, t, W, H):
  150. for p in points:
  151. x = p["x"] + math.sin(t * 0.8 + p["phase"]) * 2
  152. y = p["y"] + math.sin(t * 1.2 + p["phase"]) * 1
  153. if abs(math.sin(t * 1.5 + p["phase"])) > 0.6:
  154. _px(draw, x, y, (255, 200, 100), 1)
  155. # ── Scene → animation mapping ──────────────────────────────────────────
  156. SCENES = {
  157. "night": ["stars", "fireflies", "leaves"],
  158. "dusk": ["fireflies", "sparkles"],
  159. "tavern": ["dust_motes", "sparkles"],
  160. "indoor": ["dust_motes"],
  161. "urban": ["rain", "neon_pulse"],
  162. "nature": ["leaves", "fireflies"],
  163. "magic": ["sparkles", "fireflies"],
  164. "storm": ["rain", "lightning"],
  165. "underwater": ["bubbles", "sparkles"],
  166. "fire": ["embers", "sparkles"],
  167. "snow": ["snowflakes", "sparkles"],
  168. "desert": ["heat_shimmer", "dust_motes"],
  169. }
  170. # Map scene layer name to (init_fn, draw_fn).
  171. _LAYERS = {
  172. "stars": (init_stars, draw_stars),
  173. "fireflies": (init_fireflies, draw_fireflies),
  174. "leaves": (init_leaves, draw_leaves),
  175. "dust_motes": (init_dust_motes, draw_dust_motes),
  176. "sparkles": (init_sparkles, draw_sparkles),
  177. "rain": (init_rain, draw_rain),
  178. "lightning": (init_lightning, draw_lightning),
  179. "bubbles": (init_bubbles, draw_bubbles),
  180. "embers": (init_embers, draw_embers),
  181. "snowflakes": (init_snowflakes, draw_snowflakes),
  182. "neon_pulse": (init_neon_pulse, draw_neon_pulse),
  183. "heat_shimmer": (init_heat_shimmer, draw_heat_shimmer),
  184. }
  185. def _ensure_ffmpeg():
  186. if shutil.which("ffmpeg") is None:
  187. raise RuntimeError(
  188. "ffmpeg not found on PATH. Install via your package manager or "
  189. "download from https://ffmpeg.org/"
  190. )
  191. def pixel_art_video(
  192. base_image,
  193. output_path,
  194. scene="night",
  195. duration=6,
  196. fps=15,
  197. seed=None,
  198. export_gif=False,
  199. ):
  200. """Overlay pixel animations onto a base image and encode to MP4.
  201. Args:
  202. base_image: path to source image (ideally already pixel-art styled)
  203. output_path: path to MP4 output (GIF sibling written if export_gif=True)
  204. scene: key from SCENES (night, urban, storm, snow, fire, ...)
  205. duration: seconds of animation
  206. fps: frames per second (default 15 for retro feel)
  207. seed: optional int for reproducible animation placement
  208. export_gif: also write a GIF alongside the MP4
  209. Returns:
  210. (mp4_path, gif_path_or_None)
  211. """
  212. if scene not in SCENES:
  213. raise ValueError(
  214. f"Unknown scene {scene!r}. Choose from: {sorted(SCENES)}"
  215. )
  216. _ensure_ffmpeg()
  217. base = Image.open(base_image).convert("RGB")
  218. W, H = base.size
  219. rng = random.Random(seed if seed is not None else 42)
  220. layers = []
  221. for name in SCENES[scene]:
  222. init_fn, draw_fn = _LAYERS[name]
  223. layers.append((draw_fn, init_fn(rng, W, H)))
  224. n_frames = fps * duration
  225. os.makedirs(os.path.dirname(os.path.abspath(output_path)) or ".", exist_ok=True)
  226. with tempfile.TemporaryDirectory(prefix="pixelart_frames_") as frames_dir:
  227. for frame_idx in range(n_frames):
  228. canvas = base.copy()
  229. draw = ImageDraw.Draw(canvas)
  230. t = frame_idx / fps
  231. for draw_fn, state in layers:
  232. draw_fn(draw, state, t, W, H)
  233. canvas.save(os.path.join(frames_dir, f"frame_{frame_idx:04d}.png"))
  234. subprocess.run(
  235. ["ffmpeg", "-y", "-loglevel", "error",
  236. "-framerate", str(fps),
  237. "-i", os.path.join(frames_dir, "frame_%04d.png"),
  238. "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "18",
  239. output_path],
  240. stdin=subprocess.DEVNULL, check=True,
  241. )
  242. gif_path = None
  243. if export_gif:
  244. gif_path = output_path.rsplit(".", 1)[0] + ".gif"
  245. subprocess.run(
  246. ["ffmpeg", "-y", "-loglevel", "error",
  247. "-framerate", str(fps),
  248. "-i", os.path.join(frames_dir, "frame_%04d.png"),
  249. "-vf",
  250. "scale=320:-1:flags=neighbor,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse",
  251. "-loop", "0",
  252. gif_path],
  253. stdin=subprocess.DEVNULL, check=True,
  254. )
  255. return output_path, gif_path
  256. def main():
  257. import argparse
  258. p = argparse.ArgumentParser(description="Overlay pixel animations onto an image → MP4.")
  259. p.add_argument("base_image")
  260. p.add_argument("output")
  261. p.add_argument("--scene", default="night", choices=sorted(SCENES))
  262. p.add_argument("--duration", type=int, default=6)
  263. p.add_argument("--fps", type=int, default=15)
  264. p.add_argument("--seed", type=int, default=None)
  265. p.add_argument("--gif", action="store_true")
  266. args = p.parse_args()
  267. mp4, gif = pixel_art_video(
  268. args.base_image, args.output,
  269. scene=args.scene, duration=args.duration,
  270. fps=args.fps, seed=args.seed, export_gif=args.gif,
  271. )
  272. print(f"Wrote {mp4}")
  273. if gif:
  274. print(f"Wrote {gif}")
  275. if __name__ == "__main__":
  276. main()