Registry / serialization / napari-svg

napari-svg

JSON →
library0.2.1pypypiunverified

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.

pip install napari-svg
INSTALL
IMPORT
SIG · NAPARI-SVG
N
napari-svg
serializationpythonv0.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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()
Debug
Known issues
gotchaImage and label layers are not fully converted into vector graphics; they are represented as filled rectangles in the exported SVG.
fix
Be aware of this limitation when expecting pixel-perfect image or label representation. This plugin is best suited for vector-like layers (e.g., shapes, points). For raster data, consider taking screenshots or using other napari export methods.
affects: All versions up to 0.2.1
gotchaText layers are currently not supported and will not be exported when saving to SVG using this plugin.
fix
Avoid using text layers if SVG export is critical, or add text annotations manually to the SVG after export using a vector graphics editor.
affects: All versions up to 0.2.1
gotchaThe 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.
fix
Ensure napari is updated to `0.4.19` or newer (`pip install --upgrade napari`). Verify `napari-svg` is installed in the same environment as napari (`pip list | grep napari-svg`). Restart napari after installation.
affects: <0.4.19
Errors
Common errors & fixes
SVG file is created, but images/labels are missing or appear as solid blocks.
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.
fix
Recognize 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'
The `napari-svg` package is not installed or not available in the current Python environment that napari is running in.
fix
Install 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.
The `napari-svg` plugin, which provides the SVG file writer, is either not installed, not correctly registered, or napari itself is too old.
fix
1. 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.
Upgrade
Version history
0.2.1latest on PyPI · released Jan 14, 2025
Audit
Dependencies
naparirequiredThis is a napari plugin and requires napari to run.
numpyrequiredRequired for array operations within napari.
Agent activity
2 hits · last 30 days
node
2
Resources
napari-svg — pip install napari-svg · libregistry