snap¶
Snapshot (static rendering) API for WhipperSnapPy.
- whippersnappy.snap.snap1(mesh, outpath=None, overlay=None, annot=None, bg_map=None, roi=None, view=ViewType.LEFT, viewmat=None, width=700, height=500, fthresh=None, fmax=None, caption=None, caption_x=None, caption_y=None, caption_scale=1, invert=False, colorbar=True, colorbar_x=None, colorbar_y=None, colorbar_scale=1, orientation=OrientationType.HORIZONTAL, color_mode=ColorSelection.BOTH, font_file=None, specular=True, brain_scale=1.5, ambient=0.0)[source]¶
Render a single static snapshot of a surface mesh.
This function opens an OpenGL context, uploads the provided surface geometry and colors (overlay or annotation), renders the scene for a single view, captures the framebuffer, and returns a PIL Image. When
outpathis provided the image is also written to disk.The mesh can be any triangular surface — not just brain surfaces. Supported file formats: FreeSurfer binary surface (e.g.
lh.white), ASCII OFF (.off), legacy ASCII VTK PolyData (.vtk), ASCII PLY (.ply), or a(vertices, faces)numpy array tuple.- Parameters:
- mesh
strortupleof(array_like, array_like) Path to a mesh file (FreeSurfer binary,
.off,.vtk, or.ply) or a(vertices, faces)tuple where vertices is (N, 3) float and faces is (M, 3) int.- outpath
strorNone,optional When provided, the resulting image is saved to this path.
- overlay
str, array_like,orNone,optional Overlay file path (
.mghor FreeSurfer morph) or a (N,) array of per-vertex scalar values. IfNone, coloring falls back to background shading / annotation.- annot
str,tuple,orNone,optional Path to a FreeSurfer .annot file or a
(labels, ctab)/(labels, ctab, names)tuple with per-vertex labels.- bg_map
str, array_like,orNone,optional Path to a per-vertex scalar file or a (N,) array whose sign determines light/dark background shading for non-overlay vertices.
- roi
str, array_like,orNone,optional Path to a FreeSurfer label file or a (N,) boolean array. Vertices with
Truereceive overlay coloring; others fall back to bg_map shading.- view
ViewType,optional Which pre-defined view to render (left, right, front, …). Default is
ViewType.LEFT.- viewmat4x4 matrix-like,
optional Optional view matrix to override the pre-defined view.
- width, height
int,optional Output canvas size in pixels. Defaults to (700×500).
- fthresh, fmax
floatorNone,optional Threshold and saturation values for overlay coloring.
- caption, caption_x, caption_y, caption_scalestr/float,
optional Caption text and layout parameters.
- invert
bool,optional Invert the color scale. Default is
False.- colorbar
bool,optional If True, render a colorbar when an overlay is present. Default is
True.- colorbar_x, colorbar_y, colorbar_scale
float,optional Colorbar positioning and scale. Scale defaults to 1.
- orientation
OrientationType,optional Colorbar orientation (HORIZONTAL/VERTICAL). Default is
OrientationType.HORIZONTAL.- color_mode
ColorSelection,optional Which sign of overlay to color (POSITIVE/NEGATIVE/BOTH). Default is
ColorSelection.BOTH.- font_file
strorNone,optional Path to a TTF font for captions; fallback to bundled font if None.
- specular
bool,optional Enable specular highlights. Default is
True.- brain_scale
float,optional Scale factor applied when preparing the geometry. Default is
1.5.- ambient
float,optional Ambient lighting strength for shader. Default is
0.0.
- mesh
- Returns:
PIL.Image.ImageRendered snapshot as a PIL Image.
- Raises:
RuntimeErrorIf the OpenGL/GLFW context cannot be initialized.
ValueErrorIf the overlay contains no values to display for the chosen color_mode.
FileNotFoundErrorIf a required file cannot be found.
Examples
FreeSurfer surface with overlay:
>>> from whippersnappy import snap1 >>> img = snap1('lh.white', overlay='lh.thickness', ... bg_map='lh.curv', roi='lh.cortex.label') >>> img.save('/tmp/lh.png')
Array inputs (any triangular mesh):
>>> import numpy as np >>> v = np.random.randn(100, 3).astype(np.float32) >>> f = np.array([[0, 1, 2]], dtype=np.uint32) >>> img = snap1((v, f))
OFF / VTK / PLY file:
>>> img = snap1('mesh.off', overlay='values.mgh')
- whippersnappy.snap.snap4(lh_overlay=None, rh_overlay=None, lh_annot=None, rh_annot=None, fthresh=None, fmax=None, sdir=None, caption=None, invert=False, roi_name='cortex.label', surfname=None, bg_map_name='curv', colorbar=True, outpath=None, font_file=None, specular=True, ambient=0.0, brain_scale=1.85, color_mode=ColorSelection.BOTH)[source]¶
Render four snapshot views (left/right hemispheres, lateral/medial).
This convenience function renders four views (lateral/medial for each hemisphere), stitches them together into a single PIL Image and returns it (and saves it to
outpathwhen provided). It is typically used to produce publication-ready overview figures composed from both hemispheres.- Parameters:
- lh_overlay, rh_overlay
str, array_like,orNone Left/right hemisphere overlay — either a file path (FreeSurfer morph or .mgh) or a per-vertex scalar array. Typically provided as a pair for a coherent two-hemisphere color scale.
- lh_annot, rh_annot
str,tuple,orNone Left/right hemisphere annotation — either a path to a .annot file or a
(labels, ctab)/(labels, ctab, names)tuple. Cannot be combined withlh_overlay/rh_overlay.- fthresh, fmax
floatorNone Threshold and saturation for overlay coloring. Auto-estimated when
None.- sdir
strorNone Subject directory containing
surf/andlabel/subdirectories. Falls back to$SUBJECTS_DIRwhenNone.- caption
strorNone Caption string to place on the final image.
- invert
bool,optional Invert color scale. Default is
False.- roi_name
str,optional Basename of the label file used to restrict overlay coloring (default
'cortex.label'). The full path is constructed as<sdir>/label/<hemi>.<roi_name>.- surfname
strorNone,optional Surface basename to load (e.g.
'white'); auto-detected whenNone.- bg_map_name
str,optional Basename of the curvature/morph file used for background shading (default
'curv'). The full path is constructed as<sdir>/surf/<hemi>.<bg_map_name>.- colorbar
bool,optional Whether to draw a colorbar on the composed image. Default is
True.- outpath
strorNone,optional If provided, save composed image to this path.
- font_file
strorNone,optional Path to a font to use for captions.
- specular
bool,optional Enable/disable specular highlights in the renderer. Default is
True.- ambient
float,optional Ambient lighting strength. Default is
0.- brain_scale
float,optional Scaling factor passed to geometry preparation. Default is
1.85.- color_mode
ColorSelection,optional Which sign of overlay to color (POSITIVE/NEGATIVE/BOTH). Default is
ColorSelection.BOTH.
- lh_overlay, rh_overlay
- Returns:
PIL.Image.ImageComposed image of the four views.
- Raises:
ValueErrorFor invalid argument combinations or when required overlay values are absent.
FileNotFoundErrorWhen required surface files are not found.
Examples
>>> from whippersnappy import snap4 >>> img = snap4( ... lh_overlay='fsaverage/surf/lh.thickness', ... rh_overlay='fsaverage/surf/rh.thickness', ... sdir='./fsaverage' ... ) >>> img.save('/tmp/whippersnappy_overview.png')
- whippersnappy.snap.snap_rotate(mesh, outpath, n_frames=72, fps=24, width=700, height=500, overlay=None, bg_map=None, annot=None, roi=None, fthresh=None, fmax=None, invert=False, specular=True, ambient=0.0, brain_scale=1.5, start_view=ViewType.LEFT, color_mode=ColorSelection.BOTH)[source]¶
Render a rotating 360° video of a surface mesh.
Rotates the view around the vertical (Y) axis in
n_framesequal steps, captures each frame via OpenGL, and encodes the result into a video file. An animated GIF can be produced by passing anoutpathending in.gif; in that caseimageio-ffmpegis not required.The mesh can be any triangular surface — not just brain surfaces. Supported file formats: FreeSurfer binary surface, ASCII OFF (
.off), legacy ASCII VTK PolyData (.vtk), ASCII PLY (.ply), or a(vertices, faces)numpy array tuple.- Parameters:
- mesh
strortupleof(array_like, array_like) Path to a mesh file (FreeSurfer binary,
.off,.vtk, or.ply) or a(vertices, faces)tuple.- outpath
str Destination file path. The extension controls the output format:
.mp4— H.264 MP4 (recommended, requiresimageio-ffmpeg)..webm— VP9 WebM (requiresimageio-ffmpeg)..gif— animated GIF (no ffmpeg required, but larger file).
- n_frames
int,optional Number of frames for a full 360° rotation. Default is
72(one frame every 5°).- fps
int,optional Output frame rate in frames per second. Default is
24.- width, height
int,optional Render resolution in pixels. Defaults are
700and500.- overlay
str, array_like,orNone,optional Per-vertex overlay file path or array (e.g. thickness).
- bg_map
str, array_like,orNone,optional Curvature/morph file path or array for background shading.
- annot
str,tuple,orNone,optional FreeSurfer
.annotfile path or(labels, ctab)tuple.- roi
str, array_like,orNone,optional Label file path or boolean array to restrict overlay coloring.
- fthresh
floatorNone,optional Overlay threshold value.
- fmax
floatorNone,optional Overlay saturation value.
- invert
bool,optional Invert the overlay color scale. Default is
False.- specular
bool,optional Enable specular highlights. Default is
True.- ambient
float,optional Ambient lighting strength. Default is
0.0.- brain_scale
float,optional Geometry scale factor. Default is
1.5.- start_view
ViewType,optional Pre-defined view to start the rotation from. Default is
ViewType.LEFT.- color_mode
ColorSelection,optional Which overlay sign to color (POSITIVE/NEGATIVE/BOTH). Default is
ColorSelection.BOTH.
- mesh
- Returns:
strThe resolved
outpaththat was written.
- Raises:
ImportErrorIf
imageioorimageio-ffmpegis not installed and a video format (.mp4,.webm) was requested.RuntimeErrorIf the OpenGL context cannot be initialised.
ValueErrorIf the overlay contains no values for the chosen color mode.
Examples
>>> from whippersnappy import snap_rotate >>> snap_rotate( ... 'fsaverage/surf/lh.white', ... '/tmp/rotation.mp4', ... overlay='fsaverage/surf/lh.thickness', ... ) '/tmp/rotation.mp4'