Bubbles30 commited on
Commit
f63f3d9
·
verified ·
1 Parent(s): 272cace

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -36
app.py CHANGED
@@ -374,65 +374,72 @@ def resize_and_save_worker(args):
374
 
375
 
376
  def process_and_zip(items, fmt, w, h):
377
- tmp = tempfile.mkdtemp()
378
  proc = os.path.join(tmp, 'out')
379
  os.makedirs(proc, exist_ok=True)
380
- files, fails = [], []
381
 
 
 
382
  with ThreadPoolExecutor(max_workers=8) as ex:
383
  results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
384
 
 
385
  for name, imgs, err in results:
386
- if err or imgs is None:
387
  fails.append(f"{name}: {err}")
388
- continue
389
-
390
- for i, img in enumerate(imgs):
391
- try:
392
- # Ensure RGBA → RGB for JPEG
393
- img = img.convert("RGBA")
394
- bg = Image.new("RGBA", img.size, (255,255,255,255))
395
- img = Image.alpha_composite(bg, img)
396
-
397
- # Determine save format and extension
398
- fmt_upper = fmt.upper()
399
- save_fmt = "JPEG" if fmt_upper in ("JPEG","JPG") else fmt_upper
400
- ext = "jpg" if fmt_upper in ("JPEG","JPG") else fmt.lower()
401
-
402
- if save_fmt == "JPEG":
403
- img = img.convert("RGB")
404
-
405
- # Build filename with correct extension
406
- fname = (
407
- f"{name}_frame{i+1}.{ext}"
408
- if len(imgs) > 1
409
- else f"{name}.{ext}"
410
- )
411
- out_path = os.path.join(proc, fname)
412
-
413
- # Save and record
414
- img.save(out_path, format=save_fmt, quality=90)
415
- files.append(out_path)
 
416
 
417
- except Exception as e:
418
- # 🔍 Print the save error so you’ll see it in console
419
- print(f"⚠️ Save failed for {name}: {e}")
420
- fails.append(f"{name}_frame{i+1}: {e}")
421
 
 
 
 
422
 
 
423
  if not files:
424
  stop_event.clear()
425
  shutil.rmtree(tmp)
426
  return None, None, "No images processed.", None
427
 
 
428
  date_str = datetime.now().strftime("%Y-%m-%d")
429
  zip_name = f"{date_str}.zip"
430
  zip_path = os.path.join(tmp, zip_name)
431
-
432
  with zipfile.ZipFile(zip_path, 'w') as zf:
433
  for f in files:
434
  zf.write(f, os.path.basename(f))
435
 
 
436
  msg_lines = [f"✅ Processed {len(files)} image(s)."]
437
  if fails:
438
  msg_lines.append(f"❌ Failed: {len(fails)} image(s)")
@@ -440,6 +447,7 @@ def process_and_zip(items, fmt, w, h):
440
 
441
  stop_event.clear()
442
  return files, zip_path, "\n".join(msg_lines), tmp
 
443
  def read_uploaded_workbook(file):
444
  if not file:
445
  return [], "❌ No file uploaded"
 
374
 
375
 
376
  def process_and_zip(items, fmt, w, h):
377
+ tmp = tempfile.mkdtemp()
378
  proc = os.path.join(tmp, 'out')
379
  os.makedirs(proc, exist_ok=True)
 
380
 
381
+ files, fails = [], []
382
+ # Download + open all images
383
  with ThreadPoolExecutor(max_workers=8) as ex:
384
  results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
385
 
386
+ # For each downloaded item, process its frames
387
  for name, imgs, err in results:
388
+ if err or not imgs:
389
  fails.append(f"{name}: {err}")
390
+ continue # skip to next item
391
+
392
+ # imgs is a list of frames (even if just one)
393
+ for i, img in enumerate(imgs):
394
+ try:
395
+ # 1) Composite RGBA over white
396
+ img = img.convert("RGBA")
397
+ bg = Image.new("RGBA", img.size, (255,255,255,255))
398
+ img = Image.alpha_composite(bg, img)
399
+
400
+ # 2) Decide format & extension
401
+ fmt_upper = fmt.upper()
402
+ save_fmt = "JPEG" if fmt_upper in ("JPEG","JPG") else fmt_upper
403
+ ext = "jpg" if fmt_upper in ("JPEG","JPG") else fmt.lower()
404
+
405
+ # 3) Convert to RGB if JPEG
406
+ if save_fmt == "JPEG":
407
+ img = img.convert("RGB")
408
+
409
+ # 4) Resize with padding
410
+ img = resize_with_padding(img, w, h)
411
+
412
+ # 5) Build filename
413
+ fname = (
414
+ f"{name}_frame{i+1}.{ext}"
415
+ if len(imgs) > 1
416
+ else f"{name}.{ext}"
417
+ )
418
+ out_path = os.path.join(proc, fname)
419
 
420
+ # 6) Save & record
421
+ img.save(out_path, format=save_fmt, quality=90)
422
+ files.append(out_path)
 
423
 
424
+ except Exception as e:
425
+ print(f"⚠️ Save failed for {name}: {e}")
426
+ fails.append(f"{name}_frame{i+1}: {e}")
427
 
428
+ # If nothing made it through, clean up & report
429
  if not files:
430
  stop_event.clear()
431
  shutil.rmtree(tmp)
432
  return None, None, "No images processed.", None
433
 
434
+ # Otherwise, zip up
435
  date_str = datetime.now().strftime("%Y-%m-%d")
436
  zip_name = f"{date_str}.zip"
437
  zip_path = os.path.join(tmp, zip_name)
 
438
  with zipfile.ZipFile(zip_path, 'w') as zf:
439
  for f in files:
440
  zf.write(f, os.path.basename(f))
441
 
442
+ # Build status message
443
  msg_lines = [f"✅ Processed {len(files)} image(s)."]
444
  if fails:
445
  msg_lines.append(f"❌ Failed: {len(fails)} image(s)")
 
447
 
448
  stop_event.clear()
449
  return files, zip_path, "\n".join(msg_lines), tmp
450
+
451
  def read_uploaded_workbook(file):
452
  if not file:
453
  return [], "❌ No file uploaded"