Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -372,6 +372,8 @@ def resize_and_save_worker(args):
|
|
| 372 |
except Exception as e:
|
| 373 |
return None, f"{name}: {e}"
|
| 374 |
|
|
|
|
|
|
|
| 375 |
|
| 376 |
def process_and_zip(items, fmt, w, h):
|
| 377 |
tmp = tempfile.mkdtemp()
|
|
@@ -379,37 +381,37 @@ def process_and_zip(items, fmt, w, h):
|
|
| 379 |
os.makedirs(proc, exist_ok=True)
|
| 380 |
|
| 381 |
files, fails = [], []
|
| 382 |
-
# Download
|
| 383 |
-
|
|
|
|
| 384 |
results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
|
| 385 |
|
| 386 |
-
#
|
| 387 |
for name, imgs, err in results:
|
| 388 |
if err or not imgs:
|
| 389 |
fails.append(f"{name}: {err}")
|
| 390 |
-
continue
|
| 391 |
|
| 392 |
-
# imgs is a list of frames (even if just one)
|
| 393 |
for i, img in enumerate(imgs):
|
| 394 |
try:
|
| 395 |
-
#
|
| 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 |
-
#
|
| 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 |
-
#
|
| 406 |
if save_fmt == "JPEG":
|
| 407 |
img = img.convert("RGB")
|
| 408 |
|
| 409 |
-
#
|
| 410 |
img = resize_with_padding(img, w, h)
|
| 411 |
|
| 412 |
-
#
|
| 413 |
fname = (
|
| 414 |
f"{name}_frame{i+1}.{ext}"
|
| 415 |
if len(imgs) > 1
|
|
@@ -417,7 +419,7 @@ def process_and_zip(items, fmt, w, h):
|
|
| 417 |
)
|
| 418 |
out_path = os.path.join(proc, fname)
|
| 419 |
|
| 420 |
-
#
|
| 421 |
img.save(out_path, format=save_fmt, quality=90)
|
| 422 |
files.append(out_path)
|
| 423 |
|
|
@@ -425,21 +427,21 @@ def process_and_zip(items, fmt, w, h):
|
|
| 425 |
print(f"⚠️ Save failed for {name}: {e}")
|
| 426 |
fails.append(f"{name}_frame{i+1}: {e}")
|
| 427 |
|
| 428 |
-
# If nothing
|
| 429 |
if not files:
|
| 430 |
stop_event.clear()
|
| 431 |
shutil.rmtree(tmp)
|
| 432 |
return None, None, "No images processed.", None
|
| 433 |
|
| 434 |
-
#
|
| 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)")
|
|
|
|
| 372 |
except Exception as e:
|
| 373 |
return None, f"{name}: {e}"
|
| 374 |
|
| 375 |
+
from multiprocessing import cpu_count
|
| 376 |
+
import zipfile
|
| 377 |
|
| 378 |
def process_and_zip(items, fmt, w, h):
|
| 379 |
tmp = tempfile.mkdtemp()
|
|
|
|
| 381 |
os.makedirs(proc, exist_ok=True)
|
| 382 |
|
| 383 |
files, fails = [], []
|
| 384 |
+
# 1) Download & open in parallel
|
| 385 |
+
workers = cpu_count() * 2
|
| 386 |
+
with ThreadPoolExecutor(max_workers=workers) as ex:
|
| 387 |
results = list(ex.map(lambda it: threaded_download_and_open(it, tmp), items))
|
| 388 |
|
| 389 |
+
# 2) Process each downloaded image/list of frames
|
| 390 |
for name, imgs, err in results:
|
| 391 |
if err or not imgs:
|
| 392 |
fails.append(f"{name}: {err}")
|
| 393 |
+
continue
|
| 394 |
|
|
|
|
| 395 |
for i, img in enumerate(imgs):
|
| 396 |
try:
|
| 397 |
+
# Composite RGBA over white
|
| 398 |
img = img.convert("RGBA")
|
| 399 |
bg = Image.new("RGBA", img.size, (255,255,255,255))
|
| 400 |
img = Image.alpha_composite(bg, img)
|
| 401 |
|
| 402 |
+
# Determine save format & extension
|
| 403 |
fmt_upper = fmt.upper()
|
| 404 |
save_fmt = "JPEG" if fmt_upper in ("JPEG","JPG") else fmt_upper
|
| 405 |
ext = "jpg" if fmt_upper in ("JPEG","JPG") else fmt.lower()
|
| 406 |
|
| 407 |
+
# Convert to RGB if JPEG
|
| 408 |
if save_fmt == "JPEG":
|
| 409 |
img = img.convert("RGB")
|
| 410 |
|
| 411 |
+
# Resize with padding
|
| 412 |
img = resize_with_padding(img, w, h)
|
| 413 |
|
| 414 |
+
# Build filename
|
| 415 |
fname = (
|
| 416 |
f"{name}_frame{i+1}.{ext}"
|
| 417 |
if len(imgs) > 1
|
|
|
|
| 419 |
)
|
| 420 |
out_path = os.path.join(proc, fname)
|
| 421 |
|
| 422 |
+
# Save & record
|
| 423 |
img.save(out_path, format=save_fmt, quality=90)
|
| 424 |
files.append(out_path)
|
| 425 |
|
|
|
|
| 427 |
print(f"⚠️ Save failed for {name}: {e}")
|
| 428 |
fails.append(f"{name}_frame{i+1}: {e}")
|
| 429 |
|
| 430 |
+
# 3) If nothing succeeded, clean up and report
|
| 431 |
if not files:
|
| 432 |
stop_event.clear()
|
| 433 |
shutil.rmtree(tmp)
|
| 434 |
return None, None, "No images processed.", None
|
| 435 |
|
| 436 |
+
# 4) Create ZIP in “store” mode (no compression)
|
| 437 |
date_str = datetime.now().strftime("%Y-%m-%d")
|
| 438 |
zip_name = f"{date_str}.zip"
|
| 439 |
zip_path = os.path.join(tmp, zip_name)
|
| 440 |
+
with zipfile.ZipFile(zip_path, 'w', compression=zipfile.ZIP_STORED) as zf:
|
| 441 |
for f in files:
|
| 442 |
zf.write(f, os.path.basename(f))
|
| 443 |
|
| 444 |
+
# 5) Build status message
|
| 445 |
msg_lines = [f"✅ Processed {len(files)} image(s)."]
|
| 446 |
if fails:
|
| 447 |
msg_lines.append(f"❌ Failed: {len(fails)} image(s)")
|