lmimg skill: edit, composite and measure images from LMScript
Use this page to write, run and verify an lmimg program on the first try. Every command and output below was produced with lmscript 0.2.482 and the released lmimg 0.1.4 on Linux.
Purpose
lmimg is a native lmcode host for raster image work: procedural canvases and patterns, geometry, tone and colour operations, filters, blend modes, layers with masks, curves, healing, Hald LUTs, drawing and text, montages, GIF animation, and numeric measurements such as PSNR and SSIM. You describe the edit with method calls on the img context inside an _lmcode_ block, run it once, assert the numbers and save image files. A static ImageMagick core does the raster and codec work; no ImageMagick, fonts or codec libraries need to be installed.
When to use it
- Use it to generate or edit PNG, JPEG, TIFF, WebP, GIF, BMP and PNM images from a script: resize, crop, grade, blur, sharpen, composite, label, tile, animate.
- Use it when an image result must be checked by numbers: mean, stddev, min, max, pixel values, PSNR and SSIM against a reference, histogram CSV export.
- Use it for before and after panels, contact sheets, sprite atlases and visual regression heat maps.
- Do not use it for colour-managed or high bit depth work (8 bits per channel, no colour management), non-ASCII text, non-rectangular selections (build an image mask instead), or texture-synthesis retouching (heal fits a plane to the surrounding ring).
- For charts use a plotting host such as lmplot, and for diagrams lmdraw; the repository has examples that combine them with lmimg.
Install
npm install -g @lmctl-ai/lmctl
lmscript --version
Nothing else is installed by hand. A block that starts with cli="lmimg" install="github:lmctl-ai/lmimg@latest" makes lmscript download the release binary into its tool cache on first use and read the embedded schema from it. The current release contains self-contained Linux x64 and arm64 binaries that need dynamic glibc 2.35 or newer; all third-party libraries, fonts and codecs are linked statically. The repository README asks for lmctl 0.2.401 or newer for autorun.
Commands
| Command | Use |
|---|---|
| lmscript file.lms | Compile the _lmcode_ block, run it in lmimg and continue the outer script with the result map |
| lmscript --check file.lms | Compile the block against the host schema without running it |
| lmctl autorun file.lms or lmctl autorun URL | Run a local script or a raw example URL, installing hosts as needed |
| lmimg --version | Print the host version, for example lmimg 0.1.4 |
| lmimg --notices | Print the third-party license notices |
| lmimg --lmcode-signatures | List every signature with exact parameter types (279 lines in 0.1.4) |
| lmimg --lmcode-schema --json | Print the compiler schema as JSON |
| lmimg --lmcode FILE [FILE ...] [--dry-run] | Host protocol used by lmscript: run compiled bytecode; a dry run verifies bytecode without running image operations |
Inputs and outputs
- Input: one .lms file with a block prog = _lmcode_ cli="lmimg" install="github:lmctl-ai/lmimg@latest"; ... end _lmcode_ followed by r = run_lmcode(prog);
- Image inputs: img.open(path) reads PNG, JPEG, TIFF, WebP, GIF, BMP and PNM (PPM, PGM, PBM) files; for an animated input it reads the first frame. Images can also be created with img.canvas, img.pattern and img.hald with no input file.
- Image outputs: image.save(path) writes a file in one of the supported formats; Animation.save(path) writes a GIF with centisecond delays; image.histogram(path) writes a 256-bin CSV.
- Result: run_lmcode returns a map with ok and result. Set result inside the block with lmcode_return_set(value) or lmcode_return_put(name, value). On failure ok is false and phase, code, message, line and column are set.
- Units: dimensions and coordinates are pixels; RGBA channel values are normalized to 0..1 and stored as 8 bits per channel.
- Diagnostics: img.print and error lines go to stderr.
Safety rules
- Every image edit mutates its receiver and returns it for chaining. Call copy() first when the original must survive.
- Create output directories in the outer script before the block, for example mkdir("images"), as the repository examples do.
- save and histogram overwrite the path they are given; write into a dedicated output directory.
- psnr and ssim need two images of equal dimensions.
- Text is ASCII only, in the embedded Roboto font. Use img.text_width(text, size) to centre it.
- Morphology radii are limited to 0..16 and img.hald levels to 2..16.
- The total memory budget defaults to 4096 MiB. Set LMIMG_MAX_MEMORY_MB to a positive whole number of MiB to change it; an invalid value exits 2 and exceeding the budget exits 3.
- Always check r.ok in the outer script (assert or require); a failed block does not by itself stop the outer script.
Worked examples
Before and after panel with a result map
mkdir("images");
prog = _lmcode_ cli="lmimg" install="github:lmctl-ai/lmimg@latest";
let original = img.pattern(440, 260, "x", "y", "0.2", "1");
let edited = original.copy().levels(0.1, 0.8, 0.9);
let panel = img.panel(original, edited, 4);
panel.save("images/comparison.png");
img.assert_close(original.ssim(original), 1.0, 0.000001);
lmcode_return_put("width", panel.width());
lmcode_return_put("height", panel.height());
end _lmcode_
r = run_lmcode(prog);
println(json_stringify(r));
require(r.ok, "comparison failed");
lmscript quick.lms
{"ok":true,"result":{"width":892,"height":268}}
The run writes images/comparison.png. lmscript --check quick.lms exits 0 for the same file.
Drawing, grading and text (from the repository example before_after_panels.lms)
mkdir("images");
p = _lmcode_ cli="lmimg" install="github:lmctl-ai/lmimg@latest";
let before = img.pattern(420, 480, "0.08 + 0.62*y", "0.12 + 0.38*x", "0.22 + 0.52*(1-y)", "1");
before.circle(210.0, 205.0, 122.0, 0.92, 0.28, 0.18, 1.0);
before.circle(210.0, 205.0, 78.0, 0.96, 0.72, 0.24, 1.0);
before.line(36.0, 352.0, 384.0, 352.0, 0.10, 0.88, 0.78, 1.0, 6.0);
let after = before.copy().levels(0.16, 0.84, 0.82).hsl(198.0, 1.16, 1.02).unsharp(1.4, 0.8, 0.7, 0.0);
before.text(18.0, 38.0, "ORIGINAL", 22.0, 1.0, 1.0, 1.0, 1.0);
after.text(18.0, 38.0, "DELIVERABLE", 22.0, 1.0, 1.0, 1.0, 1.0);
let panel = img.panel(before, after, 20);
panel.resize(900, 520);
panel.save("images/before_after_panels.png");
img.assert_true(panel.width() == 900);
end _lmcode_
r = run_lmcode(p);
assert(r.ok);
print("images/before_after_panels.png");
Run a gallery example from its URL
lmctl autorun https://raw.githubusercontent.com/lmctl-ai/lmimg/main/examples/animation_wave.lms
Each repository example compiles and executes itself, asserts its results, writes images/NAME.png in the current directory and prints its output path.
Errors and exit codes
| Exit code of lmimg | Meaning |
|---|---|
| 0 | Success |
| 2 | Usage error or invalid LMIMG_MAX_MEMORY_MB; the failure frame has phase cli and code CLI_USAGE |
| 3 | Bytecode-load or execution failure, including exceeding the memory budget; JSON error on stdout and a diagnostic line on stderr |
A host failure reaches the outer script as a result map. Opening a file that does not exist gives this:
lmscript bad.lms
lmimg: dsl line 2:13: cannot open image file: missing.png
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 2:13: cannot open image file: missing.png","line":2,"column":13}
In that run the outer script only printed r, so lmscript itself exited 0; add require(r.ok, message) or assert(r.ok) to fail the script.
lmimg --bogus
usage: lmimg --version | --notices | --lmcode-schema [--json] | --lmcode-signatures | [--dry-run] --lmcode FILE [FILE ...]
Resource limit: LMIMG_MAX_MEMORY_MB (positive whole MiB; default 4096).
LMCODE_RETURN_BEGIN
{"ok":false,"phase":"cli","code":"CLI_USAGE","message":"invalid lmimg arguments"}
LMCODE_RETURN_END
How to verify
- Run lmscript --check file.lms; exit 0 means the block compiles against the lmimg schema.
- Run the script and confirm r.ok is true and the expected files exist.
- Assert inside the block with img.assert_true(condition) and img.assert_close(a, b, tolerance) on width(), height(), pixel(x, y, channel), mean, stddev, psnr or ssim.
- Compare against a reference image with psnr (positive infinity for identical images) and ssim (1.0 for identical images).
- Check exact parameter types with lmimg --lmcode-signatures before using a method you have not used before.