Skip to content

image ​

Generate images from a text prompt. generateImage uses a hosted provider (OpenAI, Google, or an open-source model via LiteLLM / Together), and can also edit input images. generateImageLocal uses a model on this machine, served by agency local serve --image. Both return base64 you can persist with writeBinary() or send onward with std::thread's image(...).

ts
import { generateImage, generateImageLocal } from "std::image"

node main() {
  const r = generateImage("a red bicycle in the rain", size: "1024x1024")
  if (isFailure(r)) { print("failed: ${r.error}"); return }
  writeBinary("bike.png", r.value.base64)

  const local = generateImageLocal("a lighthouse in a storm", "z-image-turbo")
  if (isFailure(local)) { print("failed: ${local.error}"); return }
  writeBinary("lighthouse.png", local.value.base64)
}

Types ​

ImageBox ​

A region of an image, normalized to 0..1 with the origin at the top left, as std::ocr and std::vision return it.

ts
/** A region of an image, normalized to 0..1 with the origin at the top
left, as std::ocr and std::vision return it. */
export type ImageBox = {
  x: number;
  y: number;
  width: number;
  height: number
}

(source)

ImageSize ​

Width and height in pixels.

ts
/** Width and height in pixels. */
export type ImageSize = {
  width: number;
  height: number
}

(source)

GeneratedImage ​

Image bytes encoded as base64, with their MIME type.

ts
/** Image bytes encoded as base64, with their MIME type. */
export type GeneratedImage = {
  base64: string;
  mimeType: string
}

(source)

ImageQuality ​

ts
export type ImageQuality = "low" | "medium" | "high" | "auto"

(source)

LocalImage ​

Generated image data and the seed used for reproducibility.

ts
/** Generated image data and the seed used for reproducibility. */
export type LocalImage = {
  base64: string;
  mimeType: string;
  seed: number
}

(source)

Effects ​

std::uploadImage ​

ts
@alwaysUnder(dir)
@always(model)
effect std::uploadImage {
  dir: string;
  filename: string;
  provider: string;
  model: string;
  baseUrl: string
}

(source)

std::cropImage ​

ts
@alwaysUnder(dir, outDir)
effect std::cropImage {
  dir: string;
  filename: string;
  outDir: string;
  outFilename: string
}

(source)

std::pasteImages ​

ts
effect std::pasteImages {
  files: string[];
  outDir: string;
  outFilename: string
}

(source)

Functions ​

generateImage ​

ts
generateImage(
  prompt: string,
  model: string = "",
  provider: string = "",
  size: string = "",
  quality: ImageQuality = "auto",
  images: string[] = [],
  apiKey: string = "",
  baseUrl: string = "",
): Result<GeneratedImage> raises <std::uploadImage>

Generate or edit an image with a hosted provider. Local input images are uploaded.

Use a hosted image model for generation or editing. Each local input file requires upload approval. URL and data-URI inputs do not read a local file. Save the returned base64 with writeBinary when you need a file.

Parameters:

NameTypeDefaultDescription
promptstringWhat to generate or how to edit the inputs.
modelstring""Image model, or empty for the provider default.
providerstring""Override the provider derived from the model name.
sizestring""Provider-supported size such as "1024x1024".
qualityImageQuality"auto"
imagesstring[][]Input images as local PNG/JPEG/GIF/WebP paths, HTTP(S) URLs, or data URIs.
apiKeystring""Override the API key.
baseUrlstring""Custom endpoint for openai-compat or litellm.

Returns: Result<GeneratedImage>

Throws: std::uploadImage

Needs: fileRead, llm

(source)

generateImageLocal ​

ts
generateImageLocal(
  prompt: string,
  model: string,
  size: string = "",
  steps: number | null = null,
  guidance: number | null = null,
  seed: number | null = null,
  negativePrompt: string = "",
  format: string = "png",
  lora: string = "",
  loraScale: number | null = null,
  controlnet: string = "",
  controlImage: string = "",
  controlScale: number | null = null,
  invertControlImage: boolean = false,
  images: string[] = [],
  startImage: string = "",
  strength: number | null = null,
  mask: string = "",
): Result<LocalImage> raises <std::readImage>

Generate or edit an image with a running local model. Returns image data for saving with writeBinary.

Start the model with agency local serve --image <model>. Save the returned base64 with writeBinary.

For SDXL, a LoRA adapter changes the style or character. Put its .safetensors file in client.adaptersDir and pass the filename without that extension. A ControlNet uses a drawing to guide the composition. Put it in client.controlnetsDir and pair controlnet with controlImage. The drawing is scaled to fit and centered on black. Scribble ControlNets expect white lines on black; invert a dark-on-white drawing.

FLUX.2 [klein] and Qwen-Image-Edit accept reference pictures in images. Other models use startImage to redraw a picture. Its image is scaled to cover the requested size, cropping any overflow. Leave size empty to preserve the input's aspect ratio.

ts
generateImageLocal("a vase of sunflowers", "z-image-turbo",
  startImage: "photo.png", mask: "vase-mask.png")

A mask selects the part to redraw: white changes, black stays, and grey blends the two. Transparency counts as black. The mask must match the start image's dimensions. Strength defaults can differ with a mask; Z-Image Turbo uses 1.0 with one and 0.6 without one.

Parameters:

NameTypeDefaultDescription
promptstring
modelstringCatalog name, diffusers: URI, or model directory.
sizestring""WxH with each side a multiple of 16 from 256 to 2048 and at most 4 million pixels; empty uses the input aspect ratio or 1024x1024.
stepsnumber | nullnullRefinement passes, or null for the model default.
guidancenumber | nullnullPrompt adherence; leave null for the model default since some models reject overrides.
seednumber | nullnullRandom seed, or null to choose one and return it with the image.
negativePromptstring""Unwanted content; leave empty unless the model supports it.
formatstring"png"Output format: png, jpeg, or webp.
lorastring""SDXL adapter name in client.adaptersDir, without .safetensors.
loraScalenumber | nullnullAdapter strength from 0 to 2, or null for 1.
controlnetstring""SDXL ControlNet folder in client.controlnetsDir, paired with controlImage.
controlImagestring""Local conditioning drawing, paired with controlnet.
controlScalenumber | nullnullControlNet strength from 0 to 2, or null for 1.
invertControlImagebooleanfalseInvert dark lines on white for a scribble ControlNet.
imagesstring[][]Up to four local references for FLUX.2 [klein] or Qwen-Image-Edit; Qwen-Image-Edit requires at least one.
startImagestring""Local picture to redraw; unsupported by FLUX.2 [klein] and Qwen-Image-Edit.
strengthnumber | nullnullFraction of startImage to redraw, greater than 0 and at most 1, or null for the model default.
maskstring""Local mask matching startImage dimensions, with white to redraw and black to keep.

Returns: Result<LocalImage>

Throws: std::readImage

Needs: fileRead, subprocess, env, llm

(source)

cropImage ​

ts
cropImage(
  path: string,
  box: ImageBox,
  outPath: string,
  pad: number = 0,
  square: boolean = false,
): Result<string> raises <std::cropImage>

Crop an image into a new file without overwriting an existing output.

Parameters:

NameTypeDefaultDescription
pathstringInput PNG, JPEG, or WebP file.
boxImageBoxRegion normalized to 0..1 from the top left.
outPathstringOutput path whose extension selects the format.
padnumber0Expand each side by this fraction of the box size.
squarebooleanfalsePad to a square using the edge color.

Returns: Result<string>

Throws: std::cropImage

Needs: fileRead, fileWrite, subprocess, env

(source)

imageSize ​

ts
imageSize(path: string): Result<ImageSize> raises <std::readImage>

The width and height of an image in pixels.

Parameters:

NameTypeDefaultDescription
pathstringThe image: PNG, JPEG, WebP, or GIF

Returns: Result<ImageSize>

Throws: std::readImage

Needs: fileRead, subprocess, env, llm

(source)

pasteImages ​

ts
pasteImages(
  paths: string[],
  outPath: string,
  columns: number = 2,
): Result<string> raises <std::pasteImages>

Arrange images in rows and write a new file without overwriting existing output.

Use two columns for a before-and-after comparison, or more columns for a contact sheet. Images keep their original sizes on a white canvas.

Parameters:

NameTypeDefaultDescription
pathsstring[]Input images in reading order.
outPathstringOutput path whose extension selects the format.
columnsnumber2Number of images per row.

Returns: Result<string>

Throws: std::pasteImages

Needs: fileRead, fileWrite, subprocess, env

(source)