Skip to main content

stl2mask

Convert segmentations in mesh format (e.g. STL, OBJ) to binary masks in image format (e.g. NIfTI, MHA).

Installation

stl2mask is intended to be used as a standalone command-line tool.

Install with pipx:

pipx install stl2mask

Or with uv:

uv tool install stl2mask

If you are not familiar with uv, read this guide to get started.

Usage

stl2mask

Convert STL mesh files to binary mask images. A reference image is required to define the output image space. Based on this reference image, a distance volume is created from the mesh, and a binary mask is generated by thresholding the distance volume.

stl2mask path/to/mesh.stl path/to/image.nii.gz

Parameters:

  • -o, --output: Path to the output mask image file. If not provided, the output file will be saved in the current directory with the same name as the input mesh file but with a .nii.gz extension.
  • -s, --suffix: Suffix for the output file if --output is not provided. Default is .nii.gz.
  • -t, --threshold: Threshold value for converting the mesh to a binary mask. Default is 0.0.
  • -f, --offset: Offset to apply to the origin of the distance volume, in terms of voxel spacing. This can help with alignment issues. Default is 0.5.
  • -m, --mask-value: Value to assign to the mask voxels inside the mesh. Default is 255.
  • -l, --log-level: Set the logging level. Options are DEBUG, INFO, WARNING, or ERROR. Default is INFO.

mask2stl

Convert binary mask images to STL mesh files. The binary mask is converted to a mesh using the Marching Cubes algorithm. Optionally, a reference image can be provided to define the output mesh space. This is not required if the mask image already has the correct spacing, origin, and direction.

mask2stl path/to/mask.nii.gz

Parameters:

  • -i, --image: Path to a reference image file to define the output mesh space.
  • -o, --output: Path to the output mesh file. If not provided, the output file will be saved in the current directory with the same name as the input mask file but with a .stl extension.
  • -s, --suffix: Suffix for the output file if --output is not provided. Default is .stl.
  • -v, --iso-value: Iso-value for the Marching Cubes algorithm. Defaults to the midpoint between the minimum and maximum values in the mask image.
  • -f, --fill-holes: Whether to fill holes in the binary mask before converting to a mesh. Default is False.
  • -l, --log-level: Set the logging level. Options are DEBUG, INFO, WARNING, or ERROR. Default is INFO.

Examples

Converting a mesh to a mask

Basic conversion with default parameters:

stl2mask tumor.stl reference_image.nii.gz

Specify custom output location and mask value:

stl2mask tumor.stl reference_image.nii.gz -o output/tumor_mask.nii.gz -m 1

Adjust threshold for better mesh alignment:

stl2mask tumor.stl reference_image.nii.gz -t 0.5 -f 0.25

Converting a mask to a mesh

Basic conversion:

mask2stl tumor_mask.nii.gz

With a reference image for coordinate system transformation:

mask2stl tumor_mask.nii.gz -i reference_image.nii.gz -o tumor.stl

Troubleshooting

For detailed debugging information, use the --log-level DEBUG option:

stl2mask mesh.stl image.nii.gz --log-level DEBUG

This will show detailed information about image dimensions, spacing, and processing steps.

Metadata

Release files for stl2mask 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stl2mask 0.3.0
File Size Uploaded
stl2mask-0.3.0.tar.gz 804.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stl2mask 0.3.0
File Interpreter ABI Platform
stl2mask-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 814.6 kB

Release files / stl2mask-0.3.0.tar.gz

Download URL stl2mask-0.3.0.tar.gz
Size 804.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e6700aff9961b0ad96e0d0c692389dc1dd6165f762c6c5bac1a90c4c2f8a4ca4
BLAKE2b-256 checksum
How to use checksums
a6a99f8bba3f9cd8a15da396b56b120a8a1a31c3cd7632423cc977bac025c302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / stl2mask-0.3.0-py3-none-any.whl

Download URL stl2mask-0.3.0-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d45805d34b4994af0eff8a257ba79874d597e3bc5e6728f40dff45b94113b71
BLAKE2b-256 checksum
How to use checksums
8d1b310171caea62be26f183cf91fe560c5ce8933da7064f80f66867e3d94066
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page