Created
December 2, 2025 15:09
-
-
Save nezort11/980ac5325a5fdf1469867f0a5861e03a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| from lxml import etree | |
| SVG_NS = {"svg": "http://www.w3.org/2000/svg", "xlink": "http://www.w3.org/1999/xlink"} | |
| PLACEHOLDER = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNgAAAAAgAB9HFkCg==" | |
| def shrink_svg(input_path, output_path): | |
| tree = etree.parse(input_path) | |
| root = tree.getroot() | |
| images = root.xpath(".//svg:image", namespaces=SVG_NS) | |
| for img in images: | |
| for attr in ['{http://www.w3.org/1999/xlink}href', 'href']: | |
| href = img.get(attr) | |
| if href and href.startswith('data:image'): | |
| img.set(attr, PLACEHOLDER) | |
| tree.write(output_path, pretty_print=True, xml_declaration=True, encoding="utf-8") | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: svg-shrink-images.py input.svg [output.svg]", file=sys.stderr) | |
| sys.exit(1) | |
| inp = sys.argv[1] | |
| outp = sys.argv[2] if len(sys.argv) > 2 else "shrunk-" + inp | |
| shrink_svg(inp, outp) | |
| print(f"[ OK ] Shrunk SVG saved as: {outp}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment