Skip to content

Instantly share code, notes, and snippets.

@nezort11
Created December 2, 2025 15:09
Show Gist options
  • Select an option

  • Save nezort11/980ac5325a5fdf1469867f0a5861e03a to your computer and use it in GitHub Desktop.

Select an option

Save nezort11/980ac5325a5fdf1469867f0a5861e03a to your computer and use it in GitHub Desktop.
#!/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