File size: 11,109 Bytes
4d60f00
 
 
30dc2ff
4d60f00
 
 
 
 
 
 
30dc2ff
4d60f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30dc2ff
4d60f00
 
 
 
30dc2ff
4d60f00
 
30dc2ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d60f00
30dc2ff
 
 
 
 
 
 
 
 
 
 
4d60f00
 
30dc2ff
4d60f00
 
 
 
 
 
 
 
 
 
 
 
30dc2ff
 
 
 
 
 
4d60f00
 
 
 
30dc2ff
 
 
 
 
 
 
 
 
 
 
 
4d60f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30dc2ff
4d60f00
30dc2ff
4d60f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import base64
import io
import re
import tempfile
import time
from dataclasses import dataclass
from html import escape
from urllib.parse import quote

import gradio as gr
import requests
from PIL import Image, ImageDraw, ImageFont


POLLINATIONS_URL = (
    "https://image.pollinations.ai/prompt/{prompt}"
    "?width={width}&height={height}&model=flux&nologo=true&seed={seed}"
)


STARTER_HTML = """<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>Mini Forest Game</title>
  <style>
    body { margin: 0; background: #111; display: grid; place-items: center; min-height: 100vh; }
    canvas { border: 2px solid #222; background: #222; image-rendering: pixelated; }
  </style>
</head>
<body>
  <canvas id="gameCanvas" width="800" height="450"></canvas>
  <script>
    const canvas = document.getElementById("gameCanvas");
    const ctx = canvas.getContext("2d");

    const background = new Image();
    background.src = "sprite_background.png";
    const playerImg = new Image();
    playerImg.src = "sprite_player.png";

    const keys = new Set();
    const player = { x: 380, y: 205, w: 48, h: 48, speed: 4 };

    window.addEventListener("keydown", e => keys.add(e.key));
    window.addEventListener("keyup", e => keys.delete(e.key));

    function update() {
      if (keys.has("a") || keys.has("ArrowLeft")) player.x -= player.speed;
      if (keys.has("d") || keys.has("ArrowRight")) player.x += player.speed;
      if (keys.has("w") || keys.has("ArrowUp")) player.y -= player.speed;
      if (keys.has("s") || keys.has("ArrowDown")) player.y += player.speed;
      player.x = Math.max(0, Math.min(canvas.width - player.w, player.x));
      player.y = Math.max(0, Math.min(canvas.height - player.h, player.y));
    }

    function draw() {
      ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
      ctx.drawImage(playerImg, player.x, player.y, player.w, player.h);
      ctx.fillStyle = "white";
      ctx.font = "18px sans-serif";
      ctx.fillText("Use WASD or arrow keys", 18, 28);
    }

    function loop() {
      update();
      draw();
      requestAnimationFrame(loop);
    }
    loop();
  </script>
</body>
</html>"""


DEFAULT_ROLES = """player: top-down pixel-art adventurer hero, transparent background, bright readable silhouette
background: enchanted forest clearing game background, top-down view, soft moonlight, detailed but not too busy"""


@dataclass
class AssetSpec:
    role: str
    prompt: str
    filename: str
    width: int
    height: int


def slugify(value: str) -> str:
    value = re.sub(r"[^a-zA-Z0-9]+", "_", value.strip().lower()).strip("_")
    return value or "asset"


def parse_assets(raw_roles: str, style_hint: str) -> list[AssetSpec]:
    specs: list[AssetSpec] = []
    for line in raw_roles.splitlines():
        line = line.strip()
        if not line or line.startswith("#"):
            continue

        if ":" in line:
            role, prompt = line.split(":", 1)
        elif "=" in line:
            role, prompt = line.split("=", 1)
        else:
            role, prompt = line, line

        role = role.strip()
        prompt = prompt.strip() or role
        slug = slugify(role)
        is_background = any(word in slug for word in ("background", "backdrop", "scene", "map", "level"))
        width, height = (800, 450) if is_background else (128, 128)
        filename = f"sprite_{slug}.png"
        full_prompt = (
            f"{prompt}, {style_hint}, game asset, clean readable shape, "
            "no text, no watermark"
        ).strip(", ")
        specs.append(AssetSpec(role=role, prompt=full_prompt, filename=filename, width=width, height=height))
    return specs


def image_to_png_bytes(content: bytes, width: int, height: int) -> bytes:
    image = Image.open(io.BytesIO(content)).convert("RGBA")
    image = image.resize((width, height), Image.LANCZOS)
    out = io.BytesIO()
    image.save(out, format="PNG")
    return out.getvalue()


def png_bytes_to_data_uri(content: bytes) -> str:
    return "data:image/png;base64," + base64.b64encode(content).decode("ascii")


def write_gallery_image(content: bytes, role: str) -> str:
    handle = tempfile.NamedTemporaryFile(
        prefix=f"{slugify(role)}_",
        suffix=".png",
        delete=False,
    )
    handle.write(content)
    handle.close()
    return handle.name


def placeholder_png_bytes(role: str, width: int, height: int) -> bytes:
    label = escape(role[:18])
    image = Image.new("RGBA", (width, height), "#222222")
    draw = ImageDraw.Draw(image)
    draw.rounded_rectangle((4, 4, width - 4, height - 4), radius=12, fill="#3b82f6")
    font = ImageFont.load_default()
    bbox = draw.textbbox((0, 0), label, font=font)
    x = (width - (bbox[2] - bbox[0])) / 2
    y = (height - (bbox[3] - bbox[1])) / 2
    draw.text((x, y), label, fill="white", font=font)
    out = io.BytesIO()
    image.save(out, format="PNG")
    return out.getvalue()


def generate_asset(spec: AssetSpec, index: int) -> tuple[str, str, str | None]:
    seed = abs(hash((spec.role, spec.prompt, index))) % 999999
    url = POLLINATIONS_URL.format(
        prompt=quote(spec.prompt),
        width=spec.width,
        height=spec.height,
        seed=seed,
    )

    for attempt in range(3):
        try:
            response = requests.get(url, timeout=120)
            response.raise_for_status()
            png_content = image_to_png_bytes(response.content, spec.width, spec.height)
            return (
                png_bytes_to_data_uri(png_content),
                write_gallery_image(png_content, spec.role),
                None,
            )
        except Exception as exc:
            if attempt < 2:
                time.sleep(8)
            else:
                png_content = placeholder_png_bytes(spec.role, spec.width, spec.height)
                return (
                    png_bytes_to_data_uri(png_content),
                    write_gallery_image(png_content, spec.role),
                    str(exc),
                )
    png_content = placeholder_png_bytes(spec.role, spec.width, spec.height)
    return (
        png_bytes_to_data_uri(png_content),
        write_gallery_image(png_content, spec.role),
        "Unknown generation error",
    )


def replacement_names(spec: AssetSpec) -> set[str]:
    slug = slugify(spec.role)
    names = {
        spec.filename,
        f"{slug}.png",
        f"{slug}.jpg",
        f"{slug}.jpeg",
        f"{slug}.webp",
        f"asset_{slug}.png",
        f"{spec.role.strip()}.png",
        f"{{{{{slug}}}}}",
        f"{{{slug}}}",
    }
    if slug == "background":
        names.update({"background.png", "sprite_background.jpg", "background.jpg"})
    if slug == "player":
        names.update({"player.png", "sprite_player.jpg", "hero.png"})
    if slug == "enemy":
        names.update({"enemy.png", "monster.png", "sprite_enemy.jpg"})
    return names


def embed_assets(html_code: str, assets: dict[str, str], specs: list[AssetSpec]) -> str:
    output = html_code
    manifest_lines = ["<!-- Embedded game assets generated by Image Generator for HTML Games"]

    for spec in specs:
        data_uri = assets[spec.role]
        manifest_lines.append(f"{spec.role}: {spec.filename}")
        for name in replacement_names(spec):
            output = output.replace(f'"{name}"', f'"{data_uri}"')
            output = output.replace(f"'{name}'", f"'{data_uri}'")
            output = output.replace(name, data_uri)

    manifest_lines.append("-->")
    manifest = "\n".join(manifest_lines) + "\n"
    asset_map = ",\n".join(
        f"    {slugify(spec.role)!r}: {assets[spec.role]!r}" for spec in specs
    )
    helper_script = f"""<script>
  window.GENERATED_GAME_ASSETS = {{
{asset_map}
  }};
</script>"""

    if "</head>" in output:
        output = output.replace("</head>", helper_script + "\n</head>", 1)
    elif "<body" in output:
        output = output.replace("<body", helper_script + "\n<body", 1)
    else:
        output = helper_script + "\n" + output

    return manifest + output


def build_preview(html_code: str) -> str:
    encoded = base64.b64encode(html_code.encode("utf-8")).decode("ascii")
    return (
        f'<iframe src="data:text/html;base64,{encoded}" '
        'style="width:100%;height:560px;border:1px solid #333;border-radius:8px;background:#000;" '
        'sandbox="allow-scripts" title="Game preview"></iframe>'
    )


def generate_images_and_game(html_code: str, roles: str, style_hint: str):
    if not html_code.strip():
        return "", "Paste HTML game code first.", [], ""

    specs = parse_assets(roles, style_hint or "pixel art style")
    if not specs:
        return html_code, "Add at least one asset role, like `player: brave knight`.", [], build_preview(html_code)

    assets: dict[str, str] = {}
    gallery = []
    errors = []

    for index, spec in enumerate(specs):
        data_uri, gallery_path, error = generate_asset(spec, index)
        assets[spec.role] = data_uri
        gallery.append((gallery_path, f"{spec.role} -> {spec.filename}"))
        if error:
            errors.append(f"{spec.role}: fallback used ({error})")

    rewritten = embed_assets(html_code, assets, specs)
    status = f"Generated and embedded {len(specs)} asset(s)."
    if errors:
        status += "\n\n" + "\n".join(errors)
    return rewritten, status, gallery, build_preview(rewritten)


with gr.Blocks(title="Image Generator for HTML Games") as demo:
    gr.Markdown(
        "# Image Generator for HTML Games\n"
        "Paste an HTML canvas game, list the image roles you want, and generate a rewritten version "
        "with the images embedded directly into the code."
    )

    with gr.Row():
        with gr.Column(scale=1):
            roles = gr.Textbox(
                label="Image roles to generate",
                value=DEFAULT_ROLES,
                lines=8,
                info="One per line: role: image description. Example: player: blue robot hero",
            )
            style = gr.Textbox(
                label="Shared visual style",
                value="cohesive 2D pixel-art game style, transparent subject sprites where possible",
                lines=2,
            )
            generate_btn = gr.Button("Generate Images + Embed Game", variant="primary")
            status = gr.Markdown("Ready.")
            gallery = gr.Gallery(label="Generated assets", columns=2, height=300)

        with gr.Column(scale=2):
            html_input = gr.Code(
                label="Original HTML game code",
                value=STARTER_HTML,
                language="html",
                lines=18,
            )
            output_code = gr.Code(
                label="Rewritten HTML with embedded images",
                language="html",
                lines=18,
            )

    gr.Markdown("## Game preview")
    preview = gr.HTML(build_preview(STARTER_HTML))

    generate_btn.click(
        fn=generate_images_and_game,
        inputs=[html_input, roles, style],
        outputs=[output_code, status, gallery, preview],
    )


if __name__ == "__main__":
    demo.launch()