Scour SVG Optimizer

0.38.2 · active · verified Tue Apr 14

Scour is an SVG optimizer/cleaner written in Python that reduces the size of scalable vector graphics by optimizing their structure and removing unnecessary data. Its goal is to produce an identically rendered image at a significantly smaller file size. Maintained on GitHub, the current version is 0.38.2 and it is open-source under the Apache License 2.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically optimize an SVG string using Scour. It initializes options, customizes them (e.g., to remove metadata and comments), and then processes the SVG content. It's crucial to understand the available options to achieve the desired optimization level.

import os
from scour import scour

# Create a dummy SVG input string (replace with reading from a file)
input_svg_content = '<svg width="100" height="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /><!-- A comment --><metadata><dc:title>Test SVG</dc:title></metadata></svg>'

# Get a clean scour options object
scour_options = scour.sanitizeOptions(options=None)

# Customize options (example: remove metadata and comments)
scour_options.remove_metadata = True
scour_options.remove_comments = True
scour_options.enable_viewboxing = True # A common useful option
scour_options.indent = 'none' # Remove indentation for smaller size

# Scour the SVG string
optimized_svg_content = scour.scourString(input_svg_content, options=scour_options)

# Print or save the optimized SVG
print("Original SVG (snippet):", input_svg_content[:80], "...")
print("\nOptimized SVG (snippet):", optimized_svg_content[:80], "...")

# Example of saving to a file
# with open('output_optimized.svg', 'w') as f:
#     f.write(optimized_svg_content)
# print('\nOptimized SVG saved to output_optimized.svg')

view raw JSON →