Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
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:
|
41 |
+
full_path = os.path.join(root, file)
|
42 |
+
arcname = os.path.relpath(full_path, temp_input_dir)
|
43 |
+
zip_out.write(full_path, arcname)
|
44 |
+
|
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()
|