TIMBOVILL commited on
Commit
2d78709
Β·
verified Β·
1 Parent(s): c708277

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -5,36 +5,38 @@ import zipfile
5
  import tempfile
6
  import shutil
7
 
8
- def make_icons_white_in_folder(folder_path):
9
- for root, _, files in os.walk(folder_path):
10
- for filename in files:
11
- if filename.lower().endswith('.png'):
12
- filepath = os.path.join(root, filename)
13
- image = Image.open(filepath).convert("RGBA")
14
- pixels = image.load()
15
-
16
- for y in range(image.height):
17
- for x in range(image.width):
18
- r, g, b, a = pixels[x, y]
19
- if a != 0:
20
- pixels[x, y] = (255, 255, 255, a)
21
-
22
- image.save(filepath)
23
-
24
- def process_zip(zip_file):
25
- # Create temp directories
26
  temp_input_dir = tempfile.mkdtemp()
27
  temp_output_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
 
28
 
29
  try:
30
- # Extract uploaded zip
31
  with zipfile.ZipFile(zip_file, 'r') as zip_ref:
32
  zip_ref.extractall(temp_input_dir)
33
 
34
- # Process PNGs in the folder
35
- make_icons_white_in_folder(temp_input_dir)
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Repack processed files into a new zip
 
 
38
  with zipfile.ZipFile(temp_output_zip.name, 'w') as zip_out:
39
  for root, _, files in os.walk(temp_input_dir):
40
  for file in files:
@@ -45,17 +47,25 @@ def process_zip(zip_file):
45
  return temp_output_zip.name
46
 
47
  finally:
48
- # Clean up the temp directory
49
  shutil.rmtree(temp_input_dir)
50
 
51
- # Gradio interface
52
- demo = gr.Interface(
53
- fn=process_zip,
54
- inputs=gr.File(label="Upload ZIP of PNGs", file_types=[".zip"]),
55
- outputs=gr.File(label="Download Processed ZIP"),
56
- title="PNG Icon Whitenifier",
57
- description="Upload a ZIP file containing PNG images. The tool will turn all visible parts of each PNG white (preserving transparency) and give you a new ZIP for download."
58
- )
 
 
 
 
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  demo.launch()
 
5
  import tempfile
6
  import shutil
7
 
8
+ def recolor_icons(zip_file, hex_color):
9
+ # Convert hex color to RGB tuple
10
+ hex_color = hex_color.lstrip("#")
11
+ target_rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
12
+
13
+ # Set up temp directories
 
 
 
 
 
 
 
 
 
 
 
 
14
  temp_input_dir = tempfile.mkdtemp()
15
  temp_output_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
16
+ temp_output_zip.close()
17
 
18
  try:
19
+ # Extract zip file
20
  with zipfile.ZipFile(zip_file, 'r') as zip_ref:
21
  zip_ref.extractall(temp_input_dir)
22
 
23
+ # Process PNGs
24
+ for root, _, files in os.walk(temp_input_dir):
25
+ for filename in files:
26
+ if filename.lower().endswith('.png'):
27
+ filepath = os.path.join(root, filename)
28
+ image = Image.open(filepath).convert("RGBA")
29
+ pixels = image.load()
30
+
31
+ for y in range(image.height):
32
+ for x in range(image.width):
33
+ r, g, b, a = pixels[x, y]
34
+ if a != 0: # If not transparent
35
+ pixels[x, y] = (*target_rgb, a)
36
 
37
+ image.save(filepath)
38
+
39
+ # Create new zip
40
  with zipfile.ZipFile(temp_output_zip.name, 'w') as zip_out:
41
  for root, _, files in os.walk(temp_input_dir):
42
  for file in files:
 
47
  return temp_output_zip.name
48
 
49
  finally:
 
50
  shutil.rmtree(temp_input_dir)
51
 
52
+ # Gradio UI
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("## 🎨 PNG Recoloring Tool")
55
+ gr.Markdown(
56
+ "Upload a **ZIP** file of `.png` icons and choose a color to apply to all visible pixels.\n"
57
+ "Transparency is preserved β€” only the visible parts are recolored."
58
+ )
59
+
60
+ with gr.Row():
61
+ zip_input = gr.File(label="πŸ“ Upload ZIP of PNGs", file_types=[".zip"])
62
+ color_picker = gr.ColorPicker(label="🎨 Choose Color", value="#ffffff")
63
+
64
+ output_zip = gr.File(label="⬇️ Download Recolored ZIP")
65
+
66
+ submit_btn = gr.Button("πŸ”„ Recolor Icons")
67
+
68
+ submit_btn.click(fn=recolor_icons, inputs=[zip_input, color_picker], outputs=output_zip)
69
 
70
  if __name__ == "__main__":
71
  demo.launch()