Napari SVG Exporter
napari-svg is a plugin for the napari viewer that enables exporting layers as SVG (Scalable Vector Graphics) files. It integrates directly into napari's file saving mechanism, allowing users to save their viewer's contents as vector graphics. The current version is 0.2.1, and being a napari plugin, its release cadence often aligns with napari's ecosystem updates, offering stability for its core functionality.
Common errors
-
SVG file is created, but images/labels are missing or appear as solid blocks.
cause This is an intended limitation of the napari-svg plugin. It converts image and label layers into basic paths, causing them to appear as filled rectangles rather than detailed pixel data.fixRecognize that this plugin primarily handles vector-based layers (shapes, points) effectively. For raster data, consider taking a direct screenshot of the napari viewer or using other export methods designed for pixel-based output. -
ModuleNotFoundError: No module named 'napari_svg'
cause The `napari-svg` package is not installed or not available in the current Python environment that napari is running in.fixInstall the package using `pip install napari-svg`. Ensure you are running napari from the same environment where the plugin was installed. If using anaconda, activate the correct environment first. -
napari's 'Save As...' dialog does not show '.svg' as an option.
cause The `napari-svg` plugin, which provides the SVG file writer, is either not installed, not correctly registered, or napari itself is too old.fix1. Ensure `napari-svg` is installed (`pip install napari-svg`). 2. Update napari to a recent version (`pip install --upgrade napari`). 3. Restart napari after installation/update to allow plugin discovery.
Warnings
- gotcha Image and label layers are not fully converted into vector graphics; they are represented as filled rectangles in the exported SVG.
- gotcha Text layers are currently not supported and will not be exported when saving to SVG using this plugin.
- gotcha The napari-svg plugin may not be recognized by napari if `napari` is too old or if the plugin is not installed in the active Python environment.
Install
-
pip install napari-svg
Quickstart
import napari
import numpy as np
import os
# Create a napari viewer
viewer = napari.Viewer()
# Add some sample data to visualize
viewer.add_image(np.random.rand(100, 100), name='random_image')
viewer.add_labels(np.zeros((100, 100), dtype=int), name='empty_labels')
viewer.add_points(np.random.rand(10, 2) * 100, name='random_points')
viewer.add_shapes(np.array([[20, 20], [20, 80], [80, 80], [80, 20]]), name='square_shape')
print("Napari viewer is running with sample data.")
print("To save as SVG: In the napari GUI, go to 'File > Save Selected Layers As...' or 'File > Save All Layers As...'.")
print("Then, select '.svg' as the file type and choose a filename. napari-svg will handle the export.")
# Example of attempting a programmatic save (requires napari-svg to be registered)
# Note: GUI method is often more robust for plugins that register writers.
output_filename = 'napari_output.svg'
try:
# This attempts to use napari's internal writer selection based on extension
# The napari-svg plugin should be automatically detected if installed.
viewer.save(output_filename, selected=False) # Saves all layers
print(f"Attempted programmatic save to '{output_filename}'. Check if file exists.")
except Exception as e:
print(f"Programmatic save failed: {e}. The GUI method is generally more reliable for plugins.")
# Clean up the created file for demonstration purposes
# if os.path.exists(output_filename):
# os.remove(output_filename)
# print(f"Cleaned up '{output_filename}'.")
napari.run()