{"id":6236,"library":"scour","title":"Scour SVG Optimizer","description":"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.","status":"active","version":"0.38.2","language":"en","source_language":"en","source_url":"https://github.com/scour-project/scour","tags":["svg","optimizer","cleaner","graphics","web","vector"],"install":[{"cmd":"pip install scour","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for Python 2/3 compatibility, though potentially less critical for modern Python 3.4+ environments. An open issue exists to remove it.","package":"six","optional":true}],"imports":[{"symbol":"scour","correct":"from scour import scour"},{"note":"Used to get and modify a clean options object for optimization.","symbol":"sanitizeOptions","correct":"scour.sanitizeOptions(options)"},{"note":"The primary function for optimizing an SVG string.","symbol":"scourString","correct":"scour.scourString(svg_string, options=scour_options)"}],"quickstart":{"code":"import os\nfrom scour import scour\n\n# Create a dummy SVG input string (replace with reading from a file)\ninput_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>'\n\n# Get a clean scour options object\nscour_options = scour.sanitizeOptions(options=None)\n\n# Customize options (example: remove metadata and comments)\nscour_options.remove_metadata = True\nscour_options.remove_comments = True\nscour_options.enable_viewboxing = True # A common useful option\nscour_options.indent = 'none' # Remove indentation for smaller size\n\n# Scour the SVG string\noptimized_svg_content = scour.scourString(input_svg_content, options=scour_options)\n\n# Print or save the optimized SVG\nprint(\"Original SVG (snippet):\", input_svg_content[:80], \"...\")\nprint(\"\\nOptimized SVG (snippet):\", optimized_svg_content[:80], \"...\")\n\n# Example of saving to a file\n# with open('output_optimized.svg', 'w') as f:\n#     f.write(optimized_svg_content)\n# print('\\nOptimized SVG saved to output_optimized.svg')","lang":"python","description":"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."},"warnings":[{"fix":"Always save optimized output to a new file and visually inspect for unintended changes before replacing originals. Use version control for SVG assets.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Ensure `scour` is installed for the exact Python interpreter used by the embedding application (e.g., `python2 -m pip install scour` if the application uses Python 2, or use the application's built-in Python package manager if available). For programmatic use, ensure your environment uses Python 3.4+.","message":"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.","severity":"gotcha","affected_versions":"<=0.37 (Python 2.x support dropped after 0.35, Python 3.3 after 0.37, current requires 2.7 or 3.4+)"},{"fix":"Ensure `wheel` is installed (`pip install wheel`). While `scour` doesn't currently have a `pyproject.toml`, future `pip` versions might handle this more gracefully or the project might eventually adopt PEP 517 metadata.","message":"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.","severity":"deprecated","affected_versions":"All versions of `scour` with `pip >= 23.1`"},{"fix":"Carefully review the documentation for each optimization option. Always visually inspect optimized SVG files and consider generating a diff against the original if precise fidelity is critical. Debian's `scour` package even performs an image comparison to avoid significant visual changes.","message":"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.","severity":"gotcha","affected_versions":"All versions, especially when using aggressive optimization flags."}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}