Render and save an image headlessly in Rust
Current source-owned tutorial. API availability differs by language.
The former six-view augmentation tutorial has not been ported. These current programs demonstrate Rust headless image rendering and Python image operations; they are separate workflows.
Start the walkthrough · Jump to complete source
Decode an image to a GPU texture, copy and fill it on device, and write results to PNG without opening a window. This is the renderer/sink split: the producer is identical to the interactive viewer, only the terminal node differs.
Run
cargo run -p sdk --release --example tu_viewer_image_headless -- [input.jpg] [output.png]
Both arguments are optional: input defaults to asset/image/coverMl.jpg, output to /tmp/oa_viewport_batch.png. Expected output:
tu_viewer_image_headless: asset/image/coverMl.jpg → /tmp/oa_viewport_batch.png
OK: 1672×941 asset/image/coverMl.jpg → /tmp/oa_viewport_batch.png
OK: Blit → /tmp/oa_viewport_batch_blit.png
OK: clear(red) → /tmp/oa_viewport_batch_clear.png
1. Headless means a compute-only runtime
No surface, no VK_KHR_swapchain: Runtime::current() resolves the thread default compute runtime, and any GPU the driver admits works.
1let _runtime = Runtime::current()?;2
That is the whole bring-up. In the interactive tutorial the sink is a swapchain present inside oa::Viewer; here the sink is a filesystem write.
2. The producer is texture_from_file
oa::render::texture_from_file decodes the image and uploads it as a Texture, ready for device work:
1let tex = texture_from_file(&in_path)?;2
3. The sink is save_texture_file
save_texture_file reads the texture back, encodes it, and writes the file:
1save_texture_file(&tex, &out_path, 90)?;2
The last argument is the encoder quality. The file in this directory's asset/ was written by exactly this call:
4. blit and clear are device-side smoke tests
oa::render::blit copies one texture into another on the GPU; saving the result round-trips the original pixels. oa::render::clear fills a texture with a solid color:
1let clone = blit(&tex)?;2let red = clear(&clone, oa::Color::new(0.95, 0.10, 0.10, 1.0))?;3
What you built
One binary that runs on any admitted GPU with or without a display server: decode, GPU copy, GPU fill, and PNG write, all on a compute-only runtime. The full source is sdk/rs/tutorials/vision/tu_viewer_image_headless/tu_viewer_image_headless.rs; the Python image operations are at walkthrough-python.
Example
1// ═══════════════════════════════════════════════════════════════════════════2// OA Tutorial: oa::Viewer · Headless image sink3// Level-0 API · Headless runtime + oa::image save/blit/clear4// ═══════════════════════════════════════════════════════════════════════════5//6// The renderer/sink split (architecture/oaArchitecture.md §10).7//8// Same producer (image decode → GPU texture), but no window and no event9// loop. The output is an oa::Texture; the sink decides what happens to it.10// In the interactive tutorial the sink is a swapchain present. Here the11// sink is save_texture_file(). Identical producer, different terminal node.12//13// This is the batch/CI/render-farm shape: one binary can run on any GPU14// regardless of whether a display server or surface is reachable.15//16// Additionally exercises:17// - oa::render::blit · GPU copy of one texture into another18// - oa::render::clear · solid-color fill (red)19// - oa::render::save_texture_file · readback + encode + filesystem write20//21// usage:22// cargo run --example tu_viewer_image_headless -- [input.jpg] [output.png]23//24// Defaults:25// input → asset/image/coverMl.jpg26// output → /tmp/oa_viewport_batch.png27//28// Donor: sdk/cpp/tutorials/vision/tuViewerImageHeadless.cpp29// ═══════════════════════════════════════════════════════════════════════════3031use oa::render::{blit, clear, save_texture_file, texture_from_file};32use oa::{Result, Runtime};33use std::path::PathBuf;3435fn main() -> Result<()> {36 let args: Vec<String> = std::env::args().collect();37 let default_input = oa::Path::asset_rel("image/coverMl.jpg");38 let in_path: PathBuf = args39 .get(1)40 .map(PathBuf::from)41 .unwrap_or(default_input.into());42 let out_path: PathBuf = args43 .get(2)44 .map(PathBuf::from)45 .unwrap_or_else(|| PathBuf::from("/tmp/oa_viewport_batch.png"));4647 // ── Runtime bring-up: headless (pure compute) ──────────────────────────────48 // No surface, no VK_KHR_swapchain. Any GPU admitted by the driver works.49 let _runtime = Runtime::current()?;5051 eprintln!(52 "tu_viewer_image_headless: {} → {}",53 in_path.display(),54 out_path.display()55 );5657 // ── Producer: decode packed pixels and upload directly as a Texture ─────58 let tex = texture_from_file(&in_path)?;5960 // ── Sink: save to file (readback + encode + write) ────────────────────────61 save_texture_file(&tex, &out_path, 90)?;62 println!(63 "OK: {}×{} {} → {}",64 tex.width(),65 tex.height(),66 in_path.display(),67 out_path.display()68 );6970 // ── Smoke: blit (GPU copy · should round-trip to original) ───────────────71 let blit_path = PathBuf::from("/tmp/oa_viewport_batch_blit.png");72 let clone = blit(&tex)?;73 save_texture_file(&clone, &blit_path, 90)?;74 println!("OK: Blit → {}", blit_path.display());7576 // ── Smoke: clear (solid red) ──────────────────────────────────────────────77 let clear_path = PathBuf::from("/tmp/oa_viewport_batch_clear.png");78 let red = clear(&clone, oa::Color::new(0.95, 0.10, 0.10, 1.0))?;79 save_texture_file(&red, &clear_path, 90)?;80 println!("OK: clear(red) → {}", clear_path.display());8182 Ok(())83}84
Run Rust tutorials from the OA SDK checkout with cargo run --release --example <name>. Run Python with the environment containing the OA native extension. Consult each program's arguments and capability checks before running it.