-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_png.py
40 lines (31 loc) · 1.29 KB
/
convert_to_png.py
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
import os
from lxml import etree
import cairosvg
# Path to the directory containing SVG files
svg_directory = './collectioncopy'
# Iterate through all SVG files in the directory
for filename in os.listdir(svg_directory):
if filename.endswith(".svg"):
filepath = os.path.join(svg_directory, filename)
# Parse the SVG
tree = etree.parse(filepath)
root = tree.getroot()
# Read width and height attributes
width = root.attrib.get('width', '1122') # default to 1122 if not present
height = root.attrib.get('height', '1122') # default to 1122 if not present
# Set those values as the viewBox
desired_viewbox = f"0 0 {width} {height}"
root.attrib['viewBox'] = desired_viewbox
# Remove width and height attributes
if 'width' in root.attrib:
del root.attrib['width']
if 'height' in root.attrib:
del root.attrib['height']
# Save the modified SVG
with open(filepath, 'wb') as f:
f.write(etree.tostring(root))
# Convert SVG to PNG
png_filename = os.path.splitext(filename)[0] + '.png'
png_filepath = os.path.join(svg_directory, png_filename)
cairosvg.svg2png(url=filepath, write_to=png_filepath)
print("Processing complete!")