TIMBOVILL commited on
Commit
0026e6e
Β·
verified Β·
1 Parent(s): cbe38c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -5,22 +5,36 @@ import zipfile
5
  import tempfile
6
  import shutil
7
 
8
- def recolor_icons(zip_file, hex_color):
9
- target_rgb = ImageColor.getrgb(hex_color) # Fixed: handles "red", "#ff0000", etc.
10
-
11
- # Rest of the function...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'):
@@ -31,12 +45,12 @@ def recolor_icons(zip_file, hex_color):
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:
@@ -51,10 +65,11 @@ def recolor_icons(zip_file, hex_color):
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():
@@ -62,7 +77,6 @@ with gr.Blocks() as demo:
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)
 
5
  import tempfile
6
  import shutil
7
 
8
+ def parse_color(color_str):
9
+ """
10
+ Parses a color string like "#ff0000", "red", or "rgba(255,0,0,1)" into an (R, G, B) tuple.
11
+ """
12
+ try:
13
+ if color_str.startswith("rgba"):
14
+ rgba = color_str.strip("rgba() ").split(",")
15
+ r = int(float(rgba[0]))
16
+ g = int(float(rgba[1]))
17
+ b = int(float(rgba[2]))
18
+ return (r, g, b)
19
+ else:
20
+ return ImageColor.getrgb(color_str)
21
+ except Exception as e:
22
+ raise ValueError(f"Invalid color input: {color_str} ({e})")
23
+
24
+ def recolor_icons(zip_file, selected_color):
25
+ target_rgb = parse_color(selected_color)
26
 
27
+ # Temp directories and output ZIP
28
  temp_input_dir = tempfile.mkdtemp()
29
  temp_output_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
30
  temp_output_zip.close()
31
 
32
  try:
33
+ # Extract uploaded ZIP
34
  with zipfile.ZipFile(zip_file, 'r') as zip_ref:
35
  zip_ref.extractall(temp_input_dir)
36
 
37
+ # Recolor PNGs
38
  for root, _, files in os.walk(temp_input_dir):
39
  for filename in files:
40
  if filename.lower().endswith('.png'):
 
45
  for y in range(image.height):
46
  for x in range(image.width):
47
  r, g, b, a = pixels[x, y]
48
+ if a != 0: # not transparent
49
  pixels[x, y] = (*target_rgb, a)
50
 
51
  image.save(filepath)
52
 
53
+ # Create output ZIP
54
  with zipfile.ZipFile(temp_output_zip.name, 'w') as zip_out:
55
  for root, _, files in os.walk(temp_input_dir):
56
  for file in files:
 
65
 
66
  # Gradio UI
67
  with gr.Blocks() as demo:
68
+ gr.Markdown("## 🎨 PNG Icon Recolor Tool")
69
  gr.Markdown(
70
+ "Upload a **ZIP** file of `.png` images and pick any color.\n"
71
+ "All non-transparent pixels will be recolored to your chosen color.\n"
72
+ "Transparency is preserved. Get a new ZIP to download."
73
  )
74
 
75
  with gr.Row():
 
77
  color_picker = gr.ColorPicker(label="🎨 Choose Color", value="#ffffff")
78
 
79
  output_zip = gr.File(label="⬇️ Download Recolored ZIP")
 
80
  submit_btn = gr.Button("πŸ”„ Recolor Icons")
81
 
82
  submit_btn.click(fn=recolor_icons, inputs=[zip_input, color_picker], outputs=output_zip)