TIMBOVILL commited on
Commit
47a8c9f
·
verified ·
1 Parent(s): 76cb92a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -22
app.py CHANGED
@@ -3,44 +3,38 @@ 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):
@@ -49,11 +43,9 @@ def resize_icons(zip_file, width, height):
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=[
@@ -63,7 +55,7 @@ demo = gr.Interface(
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__":
 
3
  import tempfile
4
  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):
 
43
  arcname = os.path.relpath(full_path, output_dir)
44
  zipf.write(full_path, arcname)
45
 
46
+ return output_zip_path
 
 
47
 
48
+ # Gradio interface
49
  demo = gr.Interface(
50
  fn=resize_icons,
51
  inputs=[
 
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__":