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