SathvikGanta commited on
Commit
3dab85e
1 Parent(s): 06f551f

Create svg_editor.py

Browse files
Files changed (1) hide show
  1. svg_editor.py +49 -0
svg_editor.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from xml.etree.ElementTree import ElementTree
2
+
3
+ def apply_transformations(output_svg, brightness, text_scale, line_scale):
4
+ """
5
+ Applies transformations to the SVG file for brightness, text size, and line thickness.
6
+
7
+ Args:
8
+ output_svg (str): Path to the SVG file.
9
+ brightness (float): Brightness level (0.5 to 5.0).
10
+ text_scale (float): Scale for text size (e.g., 1.5 for 150%).
11
+ line_scale (float): Scale for line thickness (0.5 to 5.0).
12
+ """
13
+ tree = ElementTree()
14
+ tree.parse(output_svg)
15
+ root = tree.getroot()
16
+
17
+ # Adjust line thickness
18
+ for element in root.findall(".//path"):
19
+ current_style = element.attrib.get("style", "")
20
+ new_style = f"stroke-width:{line_scale}px; fill-opacity:{brightness/5}; {current_style}"
21
+ element.attrib["style"] = new_style.strip()
22
+
23
+ # Adjust text size
24
+ for element in root.findall(".//text"):
25
+ # Check if the element has a 'font-size' attribute and scale it
26
+ font_size_attr = element.attrib.get("style", "")
27
+ if "font-size" in font_size_attr:
28
+ # Extract current font-size and apply the scale
29
+ current_font_size = extract_font_size(font_size_attr)
30
+ new_font_size = current_font_size * text_scale
31
+ new_style = font_size_attr.replace(
32
+ f"font-size:{current_font_size}px;", f"font-size:{new_font_size}px;"
33
+ )
34
+ element.attrib["style"] = new_style
35
+ else:
36
+ # If no font-size is specified, apply a default base font-size scaled by text_scale
37
+ new_style = f"font-size:{text_scale}em; {element.attrib.get('style', '')}"
38
+ element.attrib["style"] = new_style.strip()
39
+
40
+ # Save updated SVG
41
+ tree.write(output_svg)
42
+
43
+ def extract_font_size(style):
44
+ """Extracts the font-size in px from a style string. Defaults to 16px if not found."""
45
+ import re
46
+ match = re.search(r"font-size:(\d+(\.\d+)?)px;", style)
47
+ if match:
48
+ return float(match.group(1))
49
+ return 16.0 # Default font size if not specified