Update app.py
Browse files
app.py
CHANGED
@@ -5,36 +5,38 @@ import zipfile
|
|
5 |
import tempfile
|
6 |
import shutil
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
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
|
31 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
32 |
zip_ref.extractall(temp_input_dir)
|
33 |
|
34 |
-
# Process PNGs
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
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
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
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()
|