Upload iconsheet.py
Browse files- iconsheet.py +136 -0
iconsheet.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
import tempfile
|
4 |
+
from lxml import etree as ET
|
5 |
+
|
6 |
+
def create_icon_sheet(svg_files, columns, padding, background_color, sheet_width, transparent_background):
|
7 |
+
# Calculate dimensions
|
8 |
+
icons = []
|
9 |
+
max_icon_size = 0
|
10 |
+
for svg_file in svg_files:
|
11 |
+
# Verify the content is an SVG
|
12 |
+
try:
|
13 |
+
tree = ET.parse(svg_file.name)
|
14 |
+
root = tree.getroot()
|
15 |
+
if root.tag != '{http://www.w3.org/2000/svg}svg':
|
16 |
+
raise ValueError("File is not a valid SVG")
|
17 |
+
except ET.ParseError:
|
18 |
+
raise ValueError("File is not a valid SVG")
|
19 |
+
|
20 |
+
width = float(root.get('width', '0').replace('px', ''))
|
21 |
+
height = float(root.get('height', '0').replace('px', ''))
|
22 |
+
max_icon_size = max(max_icon_size, width, height)
|
23 |
+
icons.append(svg_file.name)
|
24 |
+
|
25 |
+
icon_size = max_icon_size + padding * 2
|
26 |
+
rows = (len(icons) + columns - 1) // columns
|
27 |
+
|
28 |
+
# Calculate scaling factor
|
29 |
+
scale_factor = sheet_width / (columns * icon_size)
|
30 |
+
|
31 |
+
# Adjust icon size and sheet height based on the scaling factor
|
32 |
+
scaled_icon_size = icon_size * scale_factor
|
33 |
+
sheet_height = rows * scaled_icon_size
|
34 |
+
|
35 |
+
# Create SVG root element
|
36 |
+
svg_ns = "http://www.w3.org/2000/svg"
|
37 |
+
# No need to register the namespace manually with lxml
|
38 |
+
svg = ET.Element("{%s}svg" % svg_ns, nsmap={None: svg_ns})
|
39 |
+
svg.set("width", str(sheet_width))
|
40 |
+
svg.set("height", str(sheet_height))
|
41 |
+
|
42 |
+
# Add background if not transparent
|
43 |
+
if not transparent_background:
|
44 |
+
rect = ET.SubElement(svg, "{%s}rect" % svg_ns)
|
45 |
+
rect.set("x", "0")
|
46 |
+
rect.set("y", "0")
|
47 |
+
rect.set("width", str(sheet_width))
|
48 |
+
rect.set("height", str(sheet_height))
|
49 |
+
rect.set("fill", background_color)
|
50 |
+
|
51 |
+
# Place icons
|
52 |
+
for i, svg_file in enumerate(icons):
|
53 |
+
row = i // columns
|
54 |
+
col = i % columns
|
55 |
+
x = col * scaled_icon_size
|
56 |
+
y = row * scaled_icon_size
|
57 |
+
|
58 |
+
# Read SVG content
|
59 |
+
with open(svg_file, 'r') as f:
|
60 |
+
svg_content = f.read()
|
61 |
+
|
62 |
+
# Remove XML declaration and DOCTYPE
|
63 |
+
svg_content = re.sub(r'<\?xml[^>]+\?>', '', svg_content)
|
64 |
+
svg_content = re.sub(r'<!DOCTYPE[^>]+>', '', svg_content)
|
65 |
+
|
66 |
+
# Parse the SVG content
|
67 |
+
icon_svg = ET.fromstring(svg_content)
|
68 |
+
|
69 |
+
# Create a group with the appropriate transform
|
70 |
+
icon_group = ET.SubElement(svg, "{%s}g" % svg_ns)
|
71 |
+
|
72 |
+
# Adjust viewBox if necessary
|
73 |
+
viewBox = icon_svg.get('viewBox')
|
74 |
+
if viewBox:
|
75 |
+
x_offset, y_offset, svg_width, svg_height = map(float, viewBox.split())
|
76 |
+
icon_scale_x = (scaled_icon_size - padding * 2 * scale_factor) / svg_width
|
77 |
+
icon_scale_y = (scaled_icon_size - padding * 2 * scale_factor) / svg_height
|
78 |
+
icon_scale = min(icon_scale_x, icon_scale_y)
|
79 |
+
translate_x = x + padding * scale_factor - x_offset * icon_scale
|
80 |
+
translate_y = y + padding * scale_factor - y_offset * icon_scale
|
81 |
+
transform = f"translate({translate_x},{translate_y}) scale({icon_scale})"
|
82 |
+
else:
|
83 |
+
width = float(icon_svg.get('width', '0').replace('px', ''))
|
84 |
+
height = float(icon_svg.get('height', '0').replace('px', ''))
|
85 |
+
icon_scale = min((scaled_icon_size - padding * 2 * scale_factor) / width, (scaled_icon_size - padding * 2 * scale_factor) / height)
|
86 |
+
transform = f"translate({x + padding * scale_factor},{y + padding * scale_factor}) scale({icon_scale})"
|
87 |
+
|
88 |
+
icon_group.set("transform", transform)
|
89 |
+
|
90 |
+
# Move the children of icon_svg into icon_group
|
91 |
+
for element in icon_svg.iterchildren():
|
92 |
+
icon_group.append(element)
|
93 |
+
|
94 |
+
# Write the SVG to a file
|
95 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg', mode='wb')
|
96 |
+
tree = ET.ElementTree(svg)
|
97 |
+
tree.write(output_file, xml_declaration=True, encoding='utf-8')
|
98 |
+
output_file.close()
|
99 |
+
|
100 |
+
return output_file.name
|
101 |
+
|
102 |
+
|
103 |
+
def icon_sheet_interface():
|
104 |
+
with gr.Blocks() as icon_sheet_app:
|
105 |
+
gr.Markdown("# Icon Sheet Generator")
|
106 |
+
|
107 |
+
with gr.Row():
|
108 |
+
svg_files = gr.File(label="Upload SVG Icons", file_count="multiple")
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
columns = gr.Slider(1, 10, value=4, step=1, label="Number of Columns")
|
112 |
+
padding = gr.Slider(0, 50, value=10, step=1, label="Padding (px)")
|
113 |
+
background_color = gr.ColorPicker(label="Background Color", value="#ffffff")
|
114 |
+
sheet_width = gr.Slider(100, 2000, value=1000, step=10, label="Sheet Width (px)")
|
115 |
+
transparent_background = gr.Checkbox(label="Transparent Background", value=False)
|
116 |
+
|
117 |
+
generate_btn = gr.Button("Generate Icon Sheet")
|
118 |
+
output = gr.File(label="Generated Icon Sheet")
|
119 |
+
|
120 |
+
def process_and_return(svg_files, columns, padding, background_color, sheet_width, transparent_background):
|
121 |
+
# Validate that all uploaded files are SVGs
|
122 |
+
for svg_file in svg_files:
|
123 |
+
if not svg_file.name.endswith('.svg'):
|
124 |
+
raise ValueError(f"Invalid file type: {svg_file.name}. Please upload only SVG files.")
|
125 |
+
temp_file = create_icon_sheet(svg_files, columns, padding, background_color, sheet_width, transparent_background)
|
126 |
+
return temp_file
|
127 |
+
|
128 |
+
generate_btn.click(process_and_return,
|
129 |
+
inputs=[svg_files, columns, padding, background_color, sheet_width, transparent_background],
|
130 |
+
outputs=output)
|
131 |
+
|
132 |
+
return icon_sheet_app
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
icon_sheet_app = icon_sheet_interface()
|
136 |
+
icon_sheet_app.launch()
|