Scour SVG Optimizer
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
- breaking Scour is designed to optimize SVG files, which involves changing their internal structure and potentially semantics. This can lead to unexpected visual changes or even broken files, especially for hand-edited SVGs or those relying on specific structural details. Always test the output and never overwrite your original files.
- gotcha Older applications that embed Python (like some versions of Inkscape) might use a specific Python interpreter (e.g., Python 2.x) and thus require `scour` to be installed for that specific environment, even if you have `scour` installed for your system's default Python 3.x. The `pip install scour` command typically targets the default Python environment.
- deprecated Installation via `setup.py install` (which `pip` might implicitly use for older packages) is being deprecated in pip versions 23.1 and later for packages that lack a `pyproject.toml`. While `scour` still installs, this might lead to future warnings or require `--use-pep517` if `wheel` is not installed.
- gotcha While Scour's optimizations are typically lossless, enabling certain 'aggressive cleaning' options or encountering specific SVG structures can potentially lead to information loss or subtle rendering differences. The project's goal is an 'identically rendered image', but this is not an absolute guarantee for all inputs and all options.
Install
-
pip install scour
Imports
- scour
from scour import scour
- sanitizeOptions
scour.sanitizeOptions(options)
- scourString
scour.scourString(svg_string, options=scour_options)
Quickstart
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')