imodfile
Decode, encode, and inspect IMOD model files — from Rust and Python.
Read and write .mod files used by the IMOD
electron microscopy toolkit, in both binary and ASCII formats.
Install
Rust crate
# or add to Cargo.toml: imodfile = "0.1"
Python package
# Or build from source:
CLI tool
Quick start (Rust)
use Imod;
let model = load.unwrap;
println!;
// Iterate over every point with indices
for in model.points
// Dump all points as text
model.save_points.unwrap;
// Save back as binary or ASCII (chosen by extension)
model.save.unwrap;
model.save.unwrap;
Quick start (Python)
=
= # (N, 3) float32 array
, , = , ,
# Create new objects and contours
=
=
=
=
=
# ASCII format
# point dump
Key types
| Type | Description |
|---|---|
[Imod] / Model |
Top-level model — image dimensions, transforms, views, objects |
[Iobj] / Object |
A named object with colour, material, contours, and meshes |
[Icont] / Contour |
A contour — open or closed series of [Ipoint]s |
[Imesh] / Mesh |
Triangle/quad mesh with vertex and index lists |
[Ipoint] |
Single 3-D coordinate { x, y, z } |
Rust API
| Use case | API |
|---|---|
| Load a file | Imod::load(path) — auto-detects binary vs ASCII |
| Save a file | model.save(path) — binary, or ASCII if path ends with .txt |
| Iterate points | model.points() → (obj, cont, pt, &Ipoint) |
| Format as text | model.to_points() → String |
| Write to writer | model.write_points(&mut writer) |
| Low-level read | read_binary(&mut reader) / read_ascii(&mut reader) |
| Low-level write | write_binary(&mut writer, &model) / write_ascii(...) |
Python API
| Python | What it does |
|---|---|
imodfile.load(path) → Model |
Load a file (auto-detect format) |
model.save(path) |
Save (binary, or ASCII if .txt) |
model.objects → list[Object] |
All objects |
obj.contours → list[Contour] |
All contours in an object |
cont.points → ndarray(N, 3) |
Point coordinates (read/write) |
cont.surface, cont.time, cont.flags |
Contour metadata (read/write) |
obj.add_contour() → Contour |
Create a new contour |
model.add_object() → Object |
Create a new object |
Build
The .pyi stub is bundled automatically for IDE autocomplete.
Note: Set
PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1if your Python is newer than PyO3's max supported version.
CLI
imodfile <input.mod> # dump model info
imodfile <input.mod> <output.mod> # copy / convert
imodfile <input.mod> output.txt # convert to ASCII
imodfile <input.mod> output.txt --points # extract point coordinates
imodfile <input.mod> --points # print points to stdout
Format support
| Format | Read | Write |
|---|---|---|
| Binary (big-endian) | ✓ | ✓ |
| ASCII text | ✓ | ✓ |
| Legacy V0.1 | ✓ | — |
All chunk types are supported: objects, contours, meshes, materials, clip planes, labels, point sizes, views, slicer angles, reference transforms, meshing parameters, and general-purpose storage.
Modules
| Crate module | Contents |
|---|---|
[model] |
Data types — Imod, Iobj, Icont, Imesh, Ipoint, ... |
[binary] |
Binary format reader and writer |
[ascii] |
ASCII format reader and writer |
[chunk_ids] |
4-byte magic constants |
[error] |
ImodError / ImodResult |
[store] |
General-purpose storage helpers |
License
MIT