Napari SVG Exporter

0.2.1 · active · verified Fri Apr 17

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

Warnings

Install

Quickstart

This quickstart demonstrates how to launch a napari viewer, add some sample layers, and then explains how to use the napari-svg plugin via the napari GUI's 'Save As...' dialog. The plugin automatically registers itself with napari's file writer system upon installation. While programmatic saving is also possible through `viewer.save()`, the GUI method ensures the plugin's writer is correctly invoked and provides direct user control over export options.

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()

view raw JSON →