Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,21 @@
|
|
1 |
-
import zipfile
|
2 |
import os
|
|
|
3 |
import tempfile
|
4 |
from PIL import Image
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
def resize_icons(zip_file_path, width, height):
|
|
|
|
|
8 |
valid_extensions = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp")
|
9 |
|
10 |
-
#
|
11 |
with tempfile.TemporaryDirectory() as tmpdir:
|
12 |
-
# Extract uploaded
|
13 |
extract_dir = os.path.join(tmpdir, "extracted")
|
14 |
os.makedirs(extract_dir, exist_ok=True)
|
|
|
15 |
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
16 |
zip_ref.extractall(extract_dir)
|
17 |
|
@@ -21,17 +25,16 @@ def resize_icons(zip_file_path, width, height):
|
|
21 |
|
22 |
files_processed = 0
|
23 |
|
24 |
-
#
|
25 |
for root, _, files in os.walk(extract_dir):
|
26 |
-
# Skip macOS __MACOSX folders
|
27 |
if "__MACOSX" in root:
|
28 |
continue
|
29 |
|
30 |
for file in files:
|
31 |
if not file.lower().endswith(valid_extensions):
|
32 |
continue
|
33 |
-
if file.startswith("._"):
|
34 |
-
continue
|
35 |
|
36 |
file_path = os.path.join(root, file)
|
37 |
try:
|
@@ -39,7 +42,7 @@ def resize_icons(zip_file_path, width, height):
|
|
39 |
img = img.convert("RGBA")
|
40 |
img_resized = img.resize((int(width), int(height)), Image.LANCZOS)
|
41 |
|
42 |
-
#
|
43 |
rel_path = os.path.relpath(file_path, extract_dir)
|
44 |
output_path = os.path.join(output_dir, rel_path)
|
45 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
@@ -48,22 +51,23 @@ def resize_icons(zip_file_path, width, height):
|
|
48 |
except Exception as e:
|
49 |
print(f"Skipping {file_path}: {e}")
|
50 |
|
51 |
-
# If nothing processed, raise error
|
52 |
if files_processed == 0:
|
53 |
raise FileNotFoundError("No valid image files found to resize.")
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
for root, _, files in os.walk(output_dir):
|
59 |
for file in files:
|
60 |
full_path = os.path.join(root, file)
|
61 |
arcname = os.path.relpath(full_path, output_dir)
|
62 |
zipf.write(full_path, arcname)
|
63 |
|
64 |
-
return
|
65 |
|
66 |
-
# Gradio
|
67 |
demo = gr.Interface(
|
68 |
fn=resize_icons,
|
69 |
inputs=[
|
@@ -73,7 +77,7 @@ demo = gr.Interface(
|
|
73 |
],
|
74 |
outputs=gr.File(label="Download Resized ZIP"),
|
75 |
title="📦 Icon Resizer",
|
76 |
-
description="Upload a ZIP of images (icons). Set
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
|
|
|
|
1 |
import os
|
2 |
+
import zipfile
|
3 |
import tempfile
|
4 |
from PIL import Image
|
5 |
import gradio as gr
|
6 |
+
import shutil
|
7 |
|
8 |
def resize_icons(zip_file_path, width, height):
|
9 |
+
import uuid
|
10 |
+
|
11 |
valid_extensions = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp")
|
12 |
|
13 |
+
# Use temporary working directory for processing
|
14 |
with tempfile.TemporaryDirectory() as tmpdir:
|
15 |
+
# Extract uploaded ZIP
|
16 |
extract_dir = os.path.join(tmpdir, "extracted")
|
17 |
os.makedirs(extract_dir, exist_ok=True)
|
18 |
+
|
19 |
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
20 |
zip_ref.extractall(extract_dir)
|
21 |
|
|
|
25 |
|
26 |
files_processed = 0
|
27 |
|
28 |
+
# Walk and resize images
|
29 |
for root, _, files in os.walk(extract_dir):
|
|
|
30 |
if "__MACOSX" in root:
|
31 |
continue
|
32 |
|
33 |
for file in files:
|
34 |
if not file.lower().endswith(valid_extensions):
|
35 |
continue
|
36 |
+
if file.startswith("._"):
|
37 |
+
continue # Skip macOS metadata files
|
38 |
|
39 |
file_path = os.path.join(root, file)
|
40 |
try:
|
|
|
42 |
img = img.convert("RGBA")
|
43 |
img_resized = img.resize((int(width), int(height)), Image.LANCZOS)
|
44 |
|
45 |
+
# Preserve folder structure
|
46 |
rel_path = os.path.relpath(file_path, extract_dir)
|
47 |
output_path = os.path.join(output_dir, rel_path)
|
48 |
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
|
|
51 |
except Exception as e:
|
52 |
print(f"Skipping {file_path}: {e}")
|
53 |
|
|
|
54 |
if files_processed == 0:
|
55 |
raise FileNotFoundError("No valid image files found to resize.")
|
56 |
|
57 |
+
# Write ZIP to persistent path
|
58 |
+
unique_id = uuid.uuid4().hex
|
59 |
+
final_zip_path = f"/tmp/resized_icons_{unique_id}.zip"
|
60 |
+
|
61 |
+
with zipfile.ZipFile(final_zip_path, 'w') as zipf:
|
62 |
for root, _, files in os.walk(output_dir):
|
63 |
for file in files:
|
64 |
full_path = os.path.join(root, file)
|
65 |
arcname = os.path.relpath(full_path, output_dir)
|
66 |
zipf.write(full_path, arcname)
|
67 |
|
68 |
+
return final_zip_path
|
69 |
|
70 |
+
# Gradio UI
|
71 |
demo = gr.Interface(
|
72 |
fn=resize_icons,
|
73 |
inputs=[
|
|
|
77 |
],
|
78 |
outputs=gr.File(label="Download Resized ZIP"),
|
79 |
title="📦 Icon Resizer",
|
80 |
+
description="Upload a ZIP of images (icons). Set width and height. Download resized icons as a ZIP."
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|