Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,7 +179,6 @@ def download_image(url, save_path):
|
|
| 179 |
# Fail
|
| 180 |
log_failure(url, "All methods failed")
|
| 181 |
return None
|
| 182 |
-
|
| 183 |
lamp_html = """
|
| 184 |
<style>
|
| 185 |
* {
|
|
@@ -345,103 +344,68 @@ def threaded_download_and_open(item, temp_dir):
|
|
| 345 |
def resize_and_save_worker(args):
|
| 346 |
name, img, fmt, w, h, out_dir = args
|
| 347 |
try:
|
| 348 |
-
# 1) Composite RGBA over white
|
| 349 |
img = img.convert('RGBA')
|
| 350 |
-
bg
|
| 351 |
img = Image.alpha_composite(bg, img)
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
fmt_upper = fmt.upper()
|
| 355 |
-
save_fmt = "JPEG" if fmt_upper in ("JPEG", "JPG") else fmt_upper
|
| 356 |
-
ext = "jpg" if fmt_upper in ("JPEG", "JPG") else fmt.lower()
|
| 357 |
-
|
| 358 |
-
# 3) Convert to RGB if needed
|
| 359 |
-
if save_fmt == "JPEG":
|
| 360 |
-
img = img.convert("RGB")
|
| 361 |
-
|
| 362 |
-
# 4) Resize with padding
|
| 363 |
img = resize_with_padding(img, w, h)
|
| 364 |
-
|
| 365 |
-
# 5) Build filename & save
|
| 366 |
-
out_name = f"{name}.{ext}"
|
| 367 |
out_path = os.path.join(out_dir, out_name)
|
| 368 |
-
img.save(out_path, format=
|
| 369 |
-
|
| 370 |
return out_path, None
|
| 371 |
-
|
| 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
|
| 380 |
proc = os.path.join(tmp, 'out')
|
| 381 |
os.makedirs(proc, exist_ok=True)
|
| 382 |
-
|
| 383 |
files, fails = [], []
|
| 384 |
-
|
| 385 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
fails.append(f"{name}: {err}")
|
| 393 |
continue
|
| 394 |
|
| 395 |
for i, img in enumerate(imgs):
|
| 396 |
try:
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
bg = Image.new("RGBA", img.size, (255,255,255,255))
|
| 400 |
img = Image.alpha_composite(bg, img)
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 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 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
else f"{name}.{ext}"
|
| 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 |
-
|
| 426 |
except Exception as e:
|
| 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 |
-
|
|
|
|
| 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)")
|
|
@@ -774,6 +738,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 774 |
)
|
| 775 |
|
| 776 |
if __name__ == "__main__":
|
| 777 |
-
demo.queue().launch(debug=True)
|
| 778 |
-
|
| 779 |
-
|
|
|
|
| 179 |
# Fail
|
| 180 |
log_failure(url, "All methods failed")
|
| 181 |
return None
|
|
|
|
| 182 |
lamp_html = """
|
| 183 |
<style>
|
| 184 |
* {
|
|
|
|
| 344 |
def resize_and_save_worker(args):
|
| 345 |
name, img, fmt, w, h, out_dir = args
|
| 346 |
try:
|
|
|
|
| 347 |
img = img.convert('RGBA')
|
| 348 |
+
bg = Image.new('RGBA', img.size, (255,255,255,255))
|
| 349 |
img = Image.alpha_composite(bg, img)
|
| 350 |
+
if fmt.upper() == 'JPEG':
|
| 351 |
+
img = img.convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
img = resize_with_padding(img, w, h)
|
| 353 |
+
out_name = f"{name}.{fmt.lower()}"
|
|
|
|
|
|
|
| 354 |
out_path = os.path.join(out_dir, out_name)
|
| 355 |
+
img.save(out_path, format=fmt.upper(), quality=90)
|
|
|
|
| 356 |
return out_path, None
|
|
|
|
| 357 |
except Exception as e:
|
| 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 |
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 stop_event.is_set():
|
| 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 |
+
img = img.convert('RGBA')
|
| 381 |
+
bg = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
|
|
|
| 382 |
img = Image.alpha_composite(bg, img)
|
| 383 |
|
| 384 |
+
if fmt.upper() == 'JPG':
|
| 385 |
+
img = img.convert('RGB')
|
|
|
|
|
|
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
img = resize_with_padding(img, w, h)
|
| 388 |
|
| 389 |
+
fname = f"{name}_frame{i+1}.{fmt.lower()}" if len(imgs) > 1 else f"{name}.{fmt.lower()}"
|
| 390 |
+
path = os.path.join(proc, fname)
|
| 391 |
+
img.save(path, format=fmt.upper(), quality=90)
|
| 392 |
+
files.append(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)")
|
|
|
|
| 738 |
)
|
| 739 |
|
| 740 |
if __name__ == "__main__":
|
| 741 |
+
demo.queue().launch(debug=True)
|
|
|
|
|
|