renderfy commited on
Commit
bbb9d35
·
verified ·
1 Parent(s): ec7e8d3

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +35 -13
streamlit_app.py CHANGED
@@ -75,7 +75,16 @@ def fetch_bytes(url_or_path: str):
75
  except Exception:
76
  return None
77
 
78
- def image_bytes_to_data_url(b: bytes, mime="image/jpeg") -> str:
 
 
 
 
 
 
 
 
 
79
  enc = base64.b64encode(b).decode("ascii")
80
  return f"data:{mime};base64,{enc}"
81
 
@@ -142,7 +151,7 @@ def show_tile_card_display_download(
142
  filename_stub: str = "image",
143
  key_prefix: str = "",
144
  ):
145
- """Gösterimde HAFİF JPEG (display_bytes) tercih edilir; indirmede NATIVE."""
146
  col.markdown(f'<div class="lb-card-title">{title}</div>', unsafe_allow_html=True)
147
 
148
  # ---- Display ----
@@ -151,7 +160,7 @@ def show_tile_card_display_download(
151
  col.caption("—"); return None, None
152
 
153
  if display_bytes is not None:
154
- src = image_bytes_to_data_url(display_bytes, "image/jpeg")
155
  col.markdown(f'<div class="lb-frame"><img src="{src}"/></div>', unsafe_allow_html=True)
156
  else:
157
  abs_url = _abs_backend_url(display_url)
@@ -175,10 +184,22 @@ def show_tile_card_display_download(
175
 
176
  return display_url or "data", native
177
 
 
 
 
 
 
 
 
 
178
  @st.cache_data(ttl=300, show_spinner=False)
179
- def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280) -> bytes:
180
- """Ağır PNG native'i hafif JPEG önizlemeye çevir (içeriği değiştirmez)."""
181
- im = Image.open(io.BytesIO(native)).convert("RGBA")
 
 
 
 
182
  w, h = im.size
183
  if w >= h:
184
  new_w = min(target_long_edge, w)
@@ -186,8 +207,9 @@ def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280) ->
186
  else:
187
  new_h = min(target_long_edge, h)
188
  new_w = max(1, int(w * (new_h / max(1, h))))
189
- im2 = im.resize((new_w, new_h), Image.LANCZOS).convert("RGB")
190
- out = io.BytesIO(); im2.save(out, format="JPEG", quality=88, subsampling=1)
 
191
  return out.getvalue()
192
 
193
  def make_preview_frame(img_bytes: bytes, aspect: str, long_edge: int, ratio: float, bg_rgb=(255,255,255)) -> bytes:
@@ -379,13 +401,13 @@ if run:
379
  st.session_state["job_counter"] += 1
380
 
381
  # ---- ÇIKTILAR ----
382
- # Packshot NATIVE → DISPLAY küçük JPEG
383
  pack_native_url = None; pack_display_jpeg = None
384
  try:
385
  pack_native_url = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
386
  if pack_native_url:
387
  pn = fetch_bytes(pack_native_url)
388
- if pn: pack_display_jpeg = make_small_jpeg_from_native(pn, 1280)
389
  except Exception: pass
390
 
391
  # Padded NATIVE → DISPLAY küçük JPEG (pad etkisi görünür)
@@ -394,16 +416,16 @@ if run:
394
  padded_native_url = (out["packshot_step"]["result"]["padded_native"] or [{}])[0].get("url")
395
  if padded_native_url:
396
  nb = fetch_bytes(padded_native_url)
397
- if nb: padded_display_jpeg = make_small_jpeg_from_native(nb, 1280)
398
  except Exception: pass
399
 
400
- # Model NATIVE → DISPLAY küçük JPEG (preview URL KULLANMA!)
401
  placed_native_url = None; placed_display_jpeg = None
402
  try:
403
  placed_native_url = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
404
  if placed_native_url:
405
  mn = fetch_bytes(placed_native_url)
406
- if mn: placed_display_jpeg = make_small_jpeg_from_native(mn, 1280)
407
  except Exception: pass
408
 
409
  # Video (zincirde üretildiyse)
 
75
  except Exception:
76
  return None
77
 
78
+ def _sniff_mime_from_bytes(b: bytes) -> str:
79
+ try:
80
+ fmt = (Image.open(io.BytesIO(b)).format or "JPEG").lower()
81
+ return {"jpeg":"image/jpeg","jpg":"image/jpeg","png":"image/png","webp":"image/webp"}.get(fmt,"image/jpeg")
82
+ except Exception:
83
+ return "image/jpeg"
84
+
85
+ def image_bytes_to_data_url(b: bytes, mime: str | None = None) -> str:
86
+ if not mime:
87
+ mime = _sniff_mime_from_bytes(b)
88
  enc = base64.b64encode(b).decode("ascii")
89
  return f"data:{mime};base64,{enc}"
90
 
 
151
  filename_stub: str = "image",
152
  key_prefix: str = "",
153
  ):
154
+ """Gösterimde HAFİF JPEG/PNG (display_bytes) tercih edilir; indirmede NATIVE."""
155
  col.markdown(f'<div class="lb-card-title">{title}</div>', unsafe_allow_html=True)
156
 
157
  # ---- Display ----
 
160
  col.caption("—"); return None, None
161
 
162
  if display_bytes is not None:
163
+ src = image_bytes_to_data_url(display_bytes) # mime otomatik
164
  col.markdown(f'<div class="lb-frame"><img src="{src}"/></div>', unsafe_allow_html=True)
165
  else:
166
  abs_url = _abs_backend_url(display_url)
 
184
 
185
  return display_url or "data", native
186
 
187
+ def rgba_to_rgb_on_bg(img_rgba: Image.Image, bg_rgb=(255,255,255)) -> Image.Image:
188
+ """RGBA görseli seçilen arka plana kompoze ederek RGB'ye çevir (siyah sorununu çözer)."""
189
+ if img_rgba.mode != "RGBA":
190
+ return img_rgba.convert("RGB")
191
+ bg = Image.new("RGBA", img_rgba.size, (*bg_rgb, 255))
192
+ bg.paste(img_rgba, (0,0), img_rgba)
193
+ return bg.convert("RGB")
194
+
195
  @st.cache_data(ttl=300, show_spinner=False)
196
+ def make_small_jpeg_from_native(native: bytes, target_long_edge: int = 1280, bg_rgb=(255,255,255)) -> bytes:
197
+ """Şeffaf PNG → beyaz zemine kompoze → hafif JPEG (piksel piksel yüklenmeyi azaltır)."""
198
+ im = Image.open(io.BytesIO(native))
199
+ if im.mode != "RGBA" and "transparency" not in im.info:
200
+ im = im.convert("RGB")
201
+ else:
202
+ im = im.convert("RGBA")
203
  w, h = im.size
204
  if w >= h:
205
  new_w = min(target_long_edge, w)
 
207
  else:
208
  new_h = min(target_long_edge, h)
209
  new_w = max(1, int(w * (new_h / max(1, h))))
210
+ im2 = im.resize((new_w, new_h), Image.LANCZOS)
211
+ im2_rgb = rgba_to_rgb_on_bg(im2, bg_rgb=bg_rgb)
212
+ out = io.BytesIO(); im2_rgb.save(out, format="JPEG", quality=88, subsampling=1)
213
  return out.getvalue()
214
 
215
  def make_preview_frame(img_bytes: bytes, aspect: str, long_edge: int, ratio: float, bg_rgb=(255,255,255)) -> bytes:
 
401
  st.session_state["job_counter"] += 1
402
 
403
  # ---- ÇIKTILAR ----
404
+ # Packshot NATIVE → DISPLAY küçük JPEG (şeffaflık beyaza kompoze)
405
  pack_native_url = None; pack_display_jpeg = None
406
  try:
407
  pack_native_url = (out["packshot_step"]["result"]["images_native"] or [{}])[0].get("url")
408
  if pack_native_url:
409
  pn = fetch_bytes(pack_native_url)
410
+ if pn: pack_display_jpeg = make_small_jpeg_from_native(pn, 1280, bg_rgb=(255,255,255))
411
  except Exception: pass
412
 
413
  # Padded NATIVE → DISPLAY küçük JPEG (pad etkisi görünür)
 
416
  padded_native_url = (out["packshot_step"]["result"]["padded_native"] or [{}])[0].get("url")
417
  if padded_native_url:
418
  nb = fetch_bytes(padded_native_url)
419
+ if nb: padded_display_jpeg = make_small_jpeg_from_native(nb, 1280, bg_rgb=(255,255,255))
420
  except Exception: pass
421
 
422
+ # Model NATIVE → DISPLAY küçük JPEG
423
  placed_native_url = None; placed_display_jpeg = None
424
  try:
425
  placed_native_url = (out["image_step"]["result"]["images_native"] or [{}])[0].get("url")
426
  if placed_native_url:
427
  mn = fetch_bytes(placed_native_url)
428
+ if mn: placed_display_jpeg = make_small_jpeg_from_native(mn, 1280, bg_rgb=(255,255,255))
429
  except Exception: pass
430
 
431
  # Video (zincirde üretildiyse)