Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -358,54 +358,72 @@ def resize_and_save_worker(args):
|
|
| 358 |
return None, f"{name}: {e}"
|
| 359 |
|
| 360 |
def process_and_zip(items, fmt, w, h):
|
| 361 |
-
tmp
|
| 362 |
proc = os.path.join(tmp, 'out')
|
| 363 |
os.makedirs(proc, exist_ok=True)
|
| 364 |
-
files, fails = [], []
|
| 365 |
|
|
|
|
|
|
|
| 366 |
with ThreadPoolExecutor(max_workers=8) as ex:
|
| 367 |
results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
|
| 368 |
|
|
|
|
| 369 |
for name, imgs, err in results:
|
| 370 |
-
if
|
| 371 |
-
print("🛑 Stopped before processing.")
|
| 372 |
-
break
|
| 373 |
-
|
| 374 |
-
if err or imgs is None:
|
| 375 |
fails.append(f"{name}: {err}")
|
| 376 |
-
continue
|
| 377 |
|
|
|
|
| 378 |
for i, img in enumerate(imgs):
|
| 379 |
try:
|
| 380 |
-
|
| 381 |
-
|
|
|
|
| 382 |
img = Image.alpha_composite(bg, img)
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
|
|
|
| 387 |
img = resize_with_padding(img, w, h)
|
| 388 |
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
except Exception as e:
|
|
|
|
| 394 |
fails.append(f"{name}_frame{i+1}: {e}")
|
| 395 |
|
|
|
|
| 396 |
if not files:
|
| 397 |
stop_event.clear()
|
| 398 |
shutil.rmtree(tmp)
|
| 399 |
return None, None, "No images processed.", None
|
| 400 |
|
|
|
|
| 401 |
date_str = datetime.now().strftime("%Y-%m-%d")
|
| 402 |
zip_name = f"{date_str}.zip"
|
| 403 |
zip_path = os.path.join(tmp, zip_name)
|
| 404 |
-
|
| 405 |
with zipfile.ZipFile(zip_path, 'w') as zf:
|
| 406 |
for f in files:
|
| 407 |
zf.write(f, os.path.basename(f))
|
| 408 |
|
|
|
|
| 409 |
msg_lines = [f"✅ Processed {len(files)} image(s)."]
|
| 410 |
if fails:
|
| 411 |
msg_lines.append(f"❌ Failed: {len(fails)} image(s)")
|
|
|
|
| 358 |
return None, f"{name}: {e}"
|
| 359 |
|
| 360 |
def process_and_zip(items, fmt, w, h):
|
| 361 |
+
tmp = tempfile.mkdtemp()
|
| 362 |
proc = os.path.join(tmp, 'out')
|
| 363 |
os.makedirs(proc, exist_ok=True)
|
|
|
|
| 364 |
|
| 365 |
+
files, fails = [], []
|
| 366 |
+
# Download + open all images
|
| 367 |
with ThreadPoolExecutor(max_workers=8) as ex:
|
| 368 |
results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
|
| 369 |
|
| 370 |
+
# For each downloaded item, process its frames
|
| 371 |
for name, imgs, err in results:
|
| 372 |
+
if err or not imgs:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
fails.append(f"{name}: {err}")
|
| 374 |
+
continue # skip to next item
|
| 375 |
|
| 376 |
+
# imgs is a list of frames (even if just one)
|
| 377 |
for i, img in enumerate(imgs):
|
| 378 |
try:
|
| 379 |
+
# 1) Composite RGBA over white
|
| 380 |
+
img = img.convert("RGBA")
|
| 381 |
+
bg = Image.new("RGBA", img.size, (255,255,255,255))
|
| 382 |
img = Image.alpha_composite(bg, img)
|
| 383 |
|
| 384 |
+
# 2) Decide format & extension
|
| 385 |
+
fmt_upper = fmt.upper()
|
| 386 |
+
save_fmt = "JPEG" if fmt_upper in ("JPEG","JPG") else fmt_upper
|
| 387 |
+
ext = "jpg" if fmt_upper in ("JPEG","JPG") else fmt.lower()
|
| 388 |
+
|
| 389 |
+
# 3) Convert to RGB if JPEG
|
| 390 |
+
if save_fmt == "JPEG":
|
| 391 |
+
img = img.convert("RGB")
|
| 392 |
|
| 393 |
+
# 4) Resize with padding
|
| 394 |
img = resize_with_padding(img, w, h)
|
| 395 |
|
| 396 |
+
# 5) Build filename
|
| 397 |
+
fname = (
|
| 398 |
+
f"{name}_frame{i+1}.{ext}"
|
| 399 |
+
if len(imgs) > 1
|
| 400 |
+
else f"{name}.{ext}"
|
| 401 |
+
)
|
| 402 |
+
out_path = os.path.join(proc, fname)
|
| 403 |
+
|
| 404 |
+
# 6) Save & record
|
| 405 |
+
img.save(out_path, format=save_fmt, quality=90)
|
| 406 |
+
files.append(out_path)
|
| 407 |
+
|
| 408 |
except Exception as e:
|
| 409 |
+
print(f"⚠️ Save failed for {name}: {e}")
|
| 410 |
fails.append(f"{name}_frame{i+1}: {e}")
|
| 411 |
|
| 412 |
+
# If nothing made it through, clean up & report
|
| 413 |
if not files:
|
| 414 |
stop_event.clear()
|
| 415 |
shutil.rmtree(tmp)
|
| 416 |
return None, None, "No images processed.", None
|
| 417 |
|
| 418 |
+
# Otherwise, zip up
|
| 419 |
date_str = datetime.now().strftime("%Y-%m-%d")
|
| 420 |
zip_name = f"{date_str}.zip"
|
| 421 |
zip_path = os.path.join(tmp, zip_name)
|
|
|
|
| 422 |
with zipfile.ZipFile(zip_path, 'w') as zf:
|
| 423 |
for f in files:
|
| 424 |
zf.write(f, os.path.basename(f))
|
| 425 |
|
| 426 |
+
# Build status message
|
| 427 |
msg_lines = [f"✅ Processed {len(files)} image(s)."]
|
| 428 |
if fails:
|
| 429 |
msg_lines.append(f"❌ Failed: {len(fails)} image(s)")
|