Skip to content

Instantly share code, notes, and snippets.

@0xm4ud
Created June 30, 2024 17:51
Show Gist options
  • Save 0xm4ud/54bbf691264ea8e5f32874d0feca478b to your computer and use it in GitHub Desktop.
Save 0xm4ud/54bbf691264ea8e5f32874d0feca478b to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
import sys
import re
# Register namespaces to ensure they are preserved in the output
ET.register_namespace('', 'http://www.w3.org/2000/svg')
ET.register_namespace('xlink', 'http://www.w3.org/1999/xlink')
def scale_svg_path(svg_file, output_file, scale_factor):
tree = ET.parse(svg_file)
root = tree.getroot()
def scale_coord(coord):
numbers = re.findall(r"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?", coord)
scaled_numbers = [str(float(number) * scale_factor) for number in numbers]
scaled_coord = re.sub(r"[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?", lambda match: scaled_numbers.pop(0), coord, count=len(numbers))
return scaled_coord
for path in root.findall('.//{http://www.w3.org/2000/svg}path'):
d_attr = path.get('d')
if d_attr:
commands = re.split('([A-Za-z])', d_attr)[1:]
scaled_commands = []
for i in range(0, len(commands), 2):
command = commands[i] + scale_coord(commands[i + 1])
scaled_commands.append(command)
path.set('d', ''.join(scaled_commands))
# Write the modified tree to the output file without using default_namespace
tree.write(output_file, encoding='utf-8', xml_declaration=True)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python scale_svg.py <input_svg> <output_svg> <scale_factor>")
sys.exit(1)
input_svg = sys.argv[1]
output_svg = sys.argv[2]
scale_factor = float(sys.argv[3])
scale_svg_path(input_svg, output_svg, scale_factor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment