A
File size: 2,915 Bytes
76cb92a
d30e079
76cb92a
 
 
d30e079
76cb92a
47a8c9f
d30e079
 
7d18d85
 
d30e079
76cb92a
d30e079
76cb92a
 
d30e079
47a8c9f
76cb92a
 
7d18d85
76cb92a
 
 
7d18d85
 
d30e079
76cb92a
7d18d85
 
 
76cb92a
7d18d85
 
d30e079
 
7d18d85
76cb92a
 
 
47a8c9f
 
76cb92a
d30e079
76cb92a
 
 
 
7d18d85
76cb92a
7d18d85
 
 
 
76cb92a
d30e079
 
 
 
 
76cb92a
 
 
 
 
 
d30e079
76cb92a
d30e079
76cb92a
 
 
 
 
 
 
 
7d18d85
d30e079
76cb92a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import zipfile
import tempfile
from PIL import Image
import gradio as gr
import shutil

def resize_icons(zip_file_path, width, height):
    import uuid

    valid_extensions = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp")

    # Use temporary working directory for processing
    with tempfile.TemporaryDirectory() as tmpdir:
        # Extract uploaded ZIP
        extract_dir = os.path.join(tmpdir, "extracted")
        os.makedirs(extract_dir, exist_ok=True)

        with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
            zip_ref.extractall(extract_dir)

        # Prepare output folder for resized images
        output_dir = os.path.join(tmpdir, "resized")
        os.makedirs(output_dir, exist_ok=True)

        files_processed = 0

        # Walk and resize images
        for root, _, files in os.walk(extract_dir):
            if "__MACOSX" in root:
                continue

            for file in files:
                if not file.lower().endswith(valid_extensions):
                    continue
                if file.startswith("._"):
                    continue  # Skip macOS metadata files

                file_path = os.path.join(root, file)
                try:
                    img = Image.open(file_path)
                    img = img.convert("RGBA")
                    img_resized = img.resize((int(width), int(height)), Image.LANCZOS)

                    # Preserve folder structure
                    rel_path = os.path.relpath(file_path, extract_dir)
                    output_path = os.path.join(output_dir, rel_path)
                    os.makedirs(os.path.dirname(output_path), exist_ok=True)
                    img_resized.save(output_path)
                    files_processed += 1
                except Exception as e:
                    print(f"Skipping {file_path}: {e}")

        if files_processed == 0:
            raise FileNotFoundError("No valid image files found to resize.")

        # Write ZIP to persistent path
        unique_id = uuid.uuid4().hex
        final_zip_path = f"/tmp/resized_icons_{unique_id}.zip"

        with zipfile.ZipFile(final_zip_path, 'w') as zipf:
            for root, _, files in os.walk(output_dir):
                for file in files:
                    full_path = os.path.join(root, file)
                    arcname = os.path.relpath(full_path, output_dir)
                    zipf.write(full_path, arcname)

        return final_zip_path

# Gradio UI
demo = gr.Interface(
    fn=resize_icons,
    inputs=[
        gr.File(label="Upload ZIP of Icons", file_types=[".zip"]),
        gr.Number(label="Width", value=64),
        gr.Number(label="Height", value=64)
    ],
    outputs=gr.File(label="Download Resized ZIP"),
    title="📦 Icon Resizer",
    description="Upload a ZIP of images (icons). Set width and height. Download resized icons as a ZIP."
)

if __name__ == "__main__":
    demo.launch()