LeafCat79 commited on
Commit
bec5b87
·
verified ·
1 Parent(s): 7e1ea74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -13
app.py CHANGED
@@ -158,36 +158,49 @@ THEME_EXAMPLES = {
158
  def generate_image_prompts(theme: str, game_type: str) -> dict:
159
  client = get_groq_client()
160
  if game_type == "Top-Down Shooter":
161
- perspective = "top-down aerial view"
162
- char_style = "top-down aerial view sprite, character seen from directly above"
 
 
 
 
163
  else:
164
- perspective = "2D side-view flat"
165
- char_style = "2D side-view flat sprite, full body visible from the side"
 
 
 
 
166
 
167
  seeds = {
168
  "sprite_player.png": (
169
  f"pixel-art game player character, {char_style}, {theme} theme, "
170
- f"vibrant colors, character ONLY with NO background, transparent background, "
171
- f"clear silhouette, 64x64 pixel style"
 
172
  ),
173
  "sprite_background.png": (
174
  f"2D game background scene, {perspective}, {theme} theme, "
175
- f"wide landscape, atmospheric, game art style, 800x450, no characters"
 
176
  ),
177
  "sprite_enemy.png": (
178
  f"pixel-art enemy character, {char_style}, {theme} theme, "
179
- f"menacing, character ONLY with NO background, transparent background, "
180
- f"clear silhouette, 64x64 pixel style"
 
181
  ),
182
  }
183
  if game_type == "Platformer":
184
  seeds["sprite_platform.png"] = (
185
  f"pixel-art solid platform tile, 2D side-view, {theme} theme, "
186
- f"rectangular textured surface, NO background, transparent background, 128x24"
 
187
  )
188
  seeds["sprite_goal.png"] = (
189
- f"pixel-art goal treasure chest or star, 2D side-view, {theme} theme, "
190
- f"glowing, clearly visible, NO background, transparent background, 40x40"
 
191
  )
192
  prompts = {}
193
  for sprite_name, seed in seeds.items():
@@ -211,6 +224,22 @@ def generate_image_prompts(theme: str, game_type: str) -> dict:
211
  # Step 2: Generate images via Pollinations.AI
212
  # ---------------------------------------------------------------------------
213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  def _pil_to_data_uri(pil_image: Image.Image, size: tuple = None) -> str:
215
  img = pil_image.resize(size, Image.LANCZOS) if size else pil_image
216
  buf = io.BytesIO()
@@ -253,7 +282,10 @@ def generate_sprites(image_prompts: dict) -> tuple:
253
  else:
254
  raise retry_exc
255
  pil_img = Image.open(io.BytesIO(response.content))
256
- size = None if is_bg else (64, 64)
 
 
 
257
  sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
258
  print(f"[Pollinations] OK: {sprite_name}")
259
  except Exception as exc:
 
158
  def generate_image_prompts(theme: str, game_type: str) -> dict:
159
  client = get_groq_client()
160
  if game_type == "Top-Down Shooter":
161
+ perspective = "bird's eye view directly overhead, isometric top-down game art"
162
+ char_style = (
163
+ "bird's eye view directly overhead, isometric top-down game sprite, "
164
+ "character seen from above looking down, head visible at top, feet at bottom, "
165
+ "no perspective distortion, flat top-down angle"
166
+ )
167
  else:
168
+ perspective = "2D side-scrolling platformer background, horizontal flat landscape"
169
+ char_style = (
170
+ "2D side-view platformer sprite, character facing right, "
171
+ "full body visible from the side, flat 2D game art style, "
172
+ "like classic side-scrolling game characters"
173
+ )
174
 
175
  seeds = {
176
  "sprite_player.png": (
177
  f"pixel-art game player character, {char_style}, {theme} theme, "
178
+ f"isolated character on pure solid-color background, "
179
+ f"strong clear silhouette, vibrant colors, 64x64 pixel style, "
180
+ f"single character only, no scenery no environment"
181
  ),
182
  "sprite_background.png": (
183
  f"2D game background scene, {perspective}, {theme} theme, "
184
+ f"wide landscape, atmospheric, game art style, 800x450, "
185
+ f"no characters no sprites, environment only"
186
  ),
187
  "sprite_enemy.png": (
188
  f"pixel-art enemy character, {char_style}, {theme} theme, "
189
+ f"menacing and threatening, isolated character on pure solid-color background, "
190
+ f"strong clear silhouette, 64x64 pixel style, "
191
+ f"single enemy only, no scenery no environment"
192
  ),
193
  }
194
  if game_type == "Platformer":
195
  seeds["sprite_platform.png"] = (
196
  f"pixel-art solid platform tile, 2D side-view, {theme} theme, "
197
+ f"rectangular stone or wooden surface, isolated on solid-color background, "
198
+ f"no characters, 128x24 pixel style"
199
  )
200
  seeds["sprite_goal.png"] = (
201
+ f"pixel-art goal treasure chest or glowing star, 2D side-view, {theme} theme, "
202
+ f"glowing and clearly visible, isolated on solid-color background, "
203
+ f"no characters, 40x40 pixel style"
204
  )
205
  prompts = {}
206
  for sprite_name, seed in seeds.items():
 
224
  # Step 2: Generate images via Pollinations.AI
225
  # ---------------------------------------------------------------------------
226
 
227
+ def _remove_background(pil_image: Image.Image) -> Image.Image:
228
+ """Remove background from sprite using rembg library."""
229
+ try:
230
+ from rembg import remove
231
+ buf_in = io.BytesIO()
232
+ pil_image.save(buf_in, format="PNG")
233
+ buf_in.seek(0)
234
+ buf_out = io.BytesIO()
235
+ remove(buf_in.read(), output=buf_out)
236
+ buf_out.seek(0)
237
+ return Image.open(buf_out).convert("RGBA")
238
+ except Exception as exc:
239
+ print(f"[rembg] Background removal failed: {exc}")
240
+ return pil_image
241
+
242
+
243
  def _pil_to_data_uri(pil_image: Image.Image, size: tuple = None) -> str:
244
  img = pil_image.resize(size, Image.LANCZOS) if size else pil_image
245
  buf = io.BytesIO()
 
282
  else:
283
  raise retry_exc
284
  pil_img = Image.open(io.BytesIO(response.content))
285
+ # Remove background for character/object sprites (not background scene)
286
+ if not is_bg:
287
+ pil_img = _remove_background(pil_img)
288
+ size = None if is_bg else (64, 64)
289
  sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
290
  print(f"[Pollinations] OK: {sprite_name}")
291
  except Exception as exc: