ComfyGen: shared generator tools (images, portraits, vintage, video, audio, voice)
Moved from YourlifeisGame/Spritegenerator; projects are found via _bootstrap.py (cwd or --project) and provide config.py plus list modules. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
103
adopt_video.py
Normal file
103
adopt_video.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""
|
||||
Copies rendered intro clips (output/video/<name>/frame_*.png) into the game as
|
||||
Assets/Video/<name>_NN.jpg. The vignette the intro used to paint over the stills
|
||||
is baked in here, so the game only has to scale one small frame per tick.
|
||||
|
||||
Wan renders 49 frames (~3 s at 16 fps) but a chapter lasts 5 s, so one in-between
|
||||
frame is synthesised for every pair via bidirectional optical flow (OpenCV
|
||||
Farneback): 97 frames, ~19 fps on screen instead of a choppy ~10.
|
||||
|
||||
Sidebar scenes (scene_*) are a slow loop in the game's left column instead: no
|
||||
vignette and no in-between frames, but the same darkening towards the bottom the
|
||||
still image had (MainForm.SceneBitmap), so it blends into the dark column.
|
||||
|
||||
Usage:
|
||||
py -3.12 adopt_video.py every clip that has frames
|
||||
py -3.12 adopt_video.py story_old just one
|
||||
"""
|
||||
import glob
|
||||
import os
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from PIL import Image, ImageDraw, ImageFilter
|
||||
|
||||
import _bootstrap # Projektordner (config.py, Listen) vor allen anderen Modulen
|
||||
import config
|
||||
|
||||
SRC = os.path.join(config.OUTPUT_DIR, "video")
|
||||
DEST = os.path.join(config.ASSETS_DIR, "Video")
|
||||
QUALITY = 80
|
||||
|
||||
|
||||
def vignette(size):
|
||||
"""Same shape as IntroForm.BuildScene: ellipse, clear centre, 170/255 black at the edge."""
|
||||
w, h = size
|
||||
k = 4 # draw at 1/4 size, blur, scale up: smooth radial falloff
|
||||
m = Image.new("L", (w // k, h // k), 170)
|
||||
d = ImageDraw.Draw(m)
|
||||
d.ellipse((-w * 0.2 / k, -h * 0.3 / k, w * 1.2 / k, h * 1.3 / k), fill=0)
|
||||
m = m.filter(ImageFilter.GaussianBlur(w / k * 0.12)).resize((w, h), Image.BILINEAR)
|
||||
return m
|
||||
|
||||
|
||||
def bottom_shade(size):
|
||||
"""Top clear → bottom 120/255 of (10,12,26), like MainForm.SceneBitmap."""
|
||||
w, h = size
|
||||
return Image.linear_gradient("L").resize((w, h)).point(lambda v: v * 120 // 255)
|
||||
|
||||
|
||||
def _warp(img, flow):
|
||||
h, w = flow.shape[:2]
|
||||
gx, gy = np.meshgrid(np.arange(w, dtype=np.float32), np.arange(h, dtype=np.float32))
|
||||
return cv2.remap(img, gx + flow[..., 0], gy + flow[..., 1], cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE)
|
||||
|
||||
|
||||
def midframe(a, b):
|
||||
"""Frame halfway between a and b (RGB uint8 arrays)."""
|
||||
ga, gb = cv2.cvtColor(a, cv2.COLOR_RGB2GRAY), cv2.cvtColor(b, cv2.COLOR_RGB2GRAY)
|
||||
args = (None, 0.5, 4, 21, 3, 5, 1.1, 0)
|
||||
fab = cv2.calcOpticalFlowFarneback(ga, gb, *args)
|
||||
fba = cv2.calcOpticalFlowFarneback(gb, ga, *args)
|
||||
# the pixel at x in the middle frame came from x - fab/2 in a and x - fba/2 in b
|
||||
wa = _warp(a, -0.5 * fab)
|
||||
wb = _warp(b, -0.5 * fba)
|
||||
return cv2.addWeighted(wa, 0.5, wb, 0.5, 0)
|
||||
|
||||
|
||||
def interpolate(frames):
|
||||
out = []
|
||||
for a, b in zip(frames, frames[1:]):
|
||||
out += [a, midframe(a, b)]
|
||||
return out + [frames[-1]]
|
||||
|
||||
|
||||
def adopt(name):
|
||||
frames = sorted(glob.glob(os.path.join(SRC, name, "frame_*.png")))
|
||||
if not frames:
|
||||
print(f"{name}: no frames"); return
|
||||
os.makedirs(DEST, exist_ok=True)
|
||||
for old in glob.glob(os.path.join(DEST, f"{name}_*.jpg")):
|
||||
os.remove(old)
|
||||
raw = [np.asarray(Image.open(f).convert("RGB")) for f in frames]
|
||||
scene = name.startswith("scene_")
|
||||
if not scene:
|
||||
raw = interpolate(raw)
|
||||
size = (raw[0].shape[1], raw[0].shape[0])
|
||||
mask = bottom_shade(size) if scene else vignette(size)
|
||||
black = Image.new("RGB", size, (10, 12, 26) if scene else (0, 0, 0))
|
||||
total = 0
|
||||
for i, arr in enumerate(raw):
|
||||
im = Image.composite(black, Image.fromarray(arr), mask)
|
||||
p = os.path.join(DEST, f"{name}_{i:02d}.jpg")
|
||||
im.save(p, quality=QUALITY, optimize=True)
|
||||
total += os.path.getsize(p)
|
||||
print(f"{name}: {len(frames)} -> {len(raw)} frames, {total / 1e6:.1f} MB")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
names = sys.argv[1:] or sorted(os.listdir(SRC))
|
||||
for n in names:
|
||||
if os.path.isdir(os.path.join(SRC, n)):
|
||||
adopt(n)
|
||||
Reference in New Issue
Block a user