Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import zipfile
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
import io
|
7 |
+
|
8 |
+
def resize_icons(zip_file, width, height):
|
9 |
+
# Create a temporary directory
|
10 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
11 |
+
# Save uploaded ZIP file
|
12 |
+
input_zip_path = os.path.join(tmpdir, "input.zip")
|
13 |
+
with open(input_zip_path, "wb") as f:
|
14 |
+
f.write(zip_file.read())
|
15 |
+
|
16 |
+
# Extract ZIP
|
17 |
+
extract_dir = os.path.join(tmpdir, "extracted")
|
18 |
+
os.makedirs(extract_dir, exist_ok=True)
|
19 |
+
with zipfile.ZipFile(input_zip_path, 'r') as zip_ref:
|
20 |
+
zip_ref.extractall(extract_dir)
|
21 |
+
|
22 |
+
# Prepare output directory
|
23 |
+
output_dir = os.path.join(tmpdir, "resized")
|
24 |
+
os.makedirs(output_dir, exist_ok=True)
|
25 |
+
|
26 |
+
# Process image files
|
27 |
+
for root, _, files in os.walk(extract_dir):
|
28 |
+
for file in files:
|
29 |
+
file_path = os.path.join(root, file)
|
30 |
+
try:
|
31 |
+
img = Image.open(file_path)
|
32 |
+
img = img.convert("RGBA") # handle transparency
|
33 |
+
img_resized = img.resize((width, height), Image.ANTIALIAS)
|
34 |
+
|
35 |
+
# Save to output directory (preserve folder structure)
|
36 |
+
rel_path = os.path.relpath(file_path, extract_dir)
|
37 |
+
output_path = os.path.join(output_dir, rel_path)
|
38 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
39 |
+
img_resized.save(output_path)
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Skipping file {file_path}: {e}")
|
42 |
+
|
43 |
+
# Zip resized images
|
44 |
+
output_zip_path = os.path.join(tmpdir, "resized_icons.zip")
|
45 |
+
with zipfile.ZipFile(output_zip_path, 'w') as zipf:
|
46 |
+
for root, _, files in os.walk(output_dir):
|
47 |
+
for file in files:
|
48 |
+
full_path = os.path.join(root, file)
|
49 |
+
arcname = os.path.relpath(full_path, output_dir)
|
50 |
+
zipf.write(full_path, arcname)
|
51 |
+
|
52 |
+
# Return the zip as bytes
|
53 |
+
with open(output_zip_path, "rb") as f:
|
54 |
+
return output_zip_path
|
55 |
+
|
56 |
+
# Gradio UI
|
57 |
+
demo = gr.Interface(
|
58 |
+
fn=resize_icons,
|
59 |
+
inputs=[
|
60 |
+
gr.File(label="Upload ZIP of Icons", file_types=[".zip"]),
|
61 |
+
gr.Number(label="Width", value=64),
|
62 |
+
gr.Number(label="Height", value=64)
|
63 |
+
],
|
64 |
+
outputs=gr.File(label="Download Resized ZIP"),
|
65 |
+
title="Icon Resizer",
|
66 |
+
description="Upload a ZIP file containing icons/images. Set the desired width and height. Download a new ZIP with resized images."
|
67 |
+
)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch()
|