TIMBOVILL commited on
Commit
7d18d85
·
verified ·
1 Parent(s): 47a8c9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -5,36 +5,54 @@ from PIL import Image
5
  import gradio as gr
6
 
7
  def resize_icons(zip_file_path, width, height):
8
- # Create a temporary working directory
 
 
9
  with tempfile.TemporaryDirectory() as tmpdir:
10
- # Extract ZIP contents
11
  extract_dir = os.path.join(tmpdir, "extracted")
12
  os.makedirs(extract_dir, exist_ok=True)
13
  with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
14
  zip_ref.extractall(extract_dir)
15
 
16
- # Prepare resized image output folder
17
  output_dir = os.path.join(tmpdir, "resized")
18
  os.makedirs(output_dir, exist_ok=True)
19
 
20
- # Resize all valid images
 
 
21
  for root, _, files in os.walk(extract_dir):
 
 
 
 
22
  for file in files:
 
 
 
 
 
23
  file_path = os.path.join(root, file)
24
  try:
25
  img = Image.open(file_path)
26
  img = img.convert("RGBA")
27
  img_resized = img.resize((int(width), int(height)), Image.LANCZOS)
28
 
29
- # Maintain relative path
30
  rel_path = os.path.relpath(file_path, extract_dir)
31
  output_path = os.path.join(output_dir, rel_path)
32
  os.makedirs(os.path.dirname(output_path), exist_ok=True)
33
  img_resized.save(output_path)
 
34
  except Exception as e:
35
- print(f"Skipping non-image file {file_path}: {e}")
 
 
 
 
36
 
37
- # Zip resized files
38
  output_zip_path = os.path.join(tmpdir, "resized_icons.zip")
39
  with zipfile.ZipFile(output_zip_path, 'w') as zipf:
40
  for root, _, files in os.walk(output_dir):
@@ -54,8 +72,8 @@ demo = gr.Interface(
54
  gr.Number(label="Height", value=64)
55
  ],
56
  outputs=gr.File(label="Download Resized ZIP"),
57
- title="Icon Resizer",
58
- description="Upload a ZIP of icons/images. Set target width and height. Download a ZIP of resized icons."
59
  )
60
 
61
  if __name__ == "__main__":
 
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
+ # Create temporary working directory
11
  with tempfile.TemporaryDirectory() as tmpdir:
12
+ # Extract uploaded zip
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
 
18
+ # Prepare output folder for resized images
19
  output_dir = os.path.join(tmpdir, "resized")
20
  os.makedirs(output_dir, exist_ok=True)
21
 
22
+ files_processed = 0
23
+
24
+ # Resize valid images
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("._"): # Skip macOS metadata
34
+ continue
35
+
36
  file_path = os.path.join(root, file)
37
  try:
38
  img = Image.open(file_path)
39
  img = img.convert("RGBA")
40
  img_resized = img.resize((int(width), int(height)), Image.LANCZOS)
41
 
42
+ # Save resized image keeping subfolder structure
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)
46
  img_resized.save(output_path)
47
+ files_processed += 1
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
+ # Zip the resized images
56
  output_zip_path = os.path.join(tmpdir, "resized_icons.zip")
57
  with zipfile.ZipFile(output_zip_path, 'w') as zipf:
58
  for root, _, files in os.walk(output_dir):
 
72
  gr.Number(label="Height", value=64)
73
  ],
74
  outputs=gr.File(label="Download Resized ZIP"),
75
+ title="📦 Icon Resizer",
76
+ description="Upload a ZIP of images (icons). Set a target width and height. Get back a ZIP with resized icons."
77
  )
78
 
79
  if __name__ == "__main__":