Qu

save_image

Builtin index · Images

save_image(path, img, [quality=90])

Input: path, img, quality · Output: no output

What it is

path is a string destination; img is the Image to write. Encodes img and writes it to path, dispatching on the path's own extension: .png writes a real (if uncompressed) PNG; .jpg/.jpeg writes a baseline JPEG; .tif/.tiff writes an uncompressed 8-bit RGB TIFF; any other extension, or none, writes a BMP. (Extension dispatch on write, magic-byte sniffing on read, is not an inconsistency: writing has no bytes to sniff yet, so the extension is the only statement of what the caller wants.) quality is an optional named number 1–100 (default 90) that is only meaningful for JPEG — it is accepted and ignored for the other formats, and a value outside 1–100 is an error rather than silently clamped. JPEG is lossy: a save_image/load_image round trip through .jpg does not return the pixels that went in. On the gradient images the test suite measures, the RMS difference is about 1.3–1.4 levels out of 255 at the default quality 90, and about 0.6 at quality 100 — small, but not zero, and it compounds if you re-save repeatedly. PNG, BMP and TIFF as written here are lossless, so their round trips are pixel-exact.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
save_image(path, img, [quality=90])
path stringpath is a string destination.
img Imageimg is the Image to write.
quality number, optional (default 90)(Extension dispatch on write, magic-byte sniffing on read, is not an inconsistency: writing has no bytes to sniff yet, so the extension is the only statement of what the caller wants.) quality is an optional named number 1–100 (default 90) that is only meaningful for JPEG — it is accepted and ignored for the other formats, and a value outside 1–100 is an error rather than silently clamped.
no outputJPEG is lossy: a save_image/load_image round trip through .jpg does not return the pixels that went in. PNG, BMP and TIFF as written here are lossless, so their round trips are pixel-exact.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

# Make a small gradient picture first, so the example has a file to load.

g = linspace(0, 255, 32)
save_image("photo.png", image_from_matrix(ones(32, 1) * reshape(g, 1, 32)))

with image
    img = .load("photo.png") |> .blur |> .sobel(direction="x")
end

from stdlib-overview

# A gradient with a bright bar in it, so the rotation below is visible:

# a symmetric test image would look identical afterwards.

n = 48
g = zeros(n, n)
for i in 0 to n - 1
    for j in 0 to n - 1
        g[i, j] = 40 + 3 * j
        if i > 10 and i < 22 and j > 6 and j < 30
            g[i, j] = 240
        end if
    end for
end for
save_image("sample.bmp", image_from_matrix(g))

img = load_image("sample.bmp")
gray = grayscale(img)
imshow(gray)

from images

img = image_new(300, 200, r=20, g=20, b=20)
img = draw_line(img, 10, 10, 290, 10, color="yellow", thickness=2)
img = draw_rect(img, 20, 30, 60, 40, color="cyan", thickness=2)
img = draw_circle(img, 150, 100, 30, color="magenta", filled=true)
img = draw_arrow(img, 200, 150, 260, 180, color="white", head_size=12)

# A 100 px bar labeled "50 um" when pixel_size = 0.5 um/px.

img = draw_scale_bar(img, 50, pixel_size=0.5, unit="um", position="bottom-right", color="white")
save_image("/tmp/annotated.bmp", img)

from images

canvas = image_new(40, 30, r=200, g=200, b=200)

m = [10, 40, 250; 90, 140, 190; 220, 255, 30]
gray = image_from_matrix(m)
save_image("/tmp/gray.bmp", gray)

imagesc(m, colormap="viridis")          # false-color view of the same matrix


big = resize(canvas, 80, 60, method="bilinear")
piece = crop(big, 10, 10, 30, 20)
print(piece.width, piece.height)
imshow(piece)

from images

How it works

save_image(path, img) — dispatches on the path's extension: .bmp (default, including no/unknown extension, to match this builtin's original BMP-only behavior) uses image::encode_bmp; .png uses the real, spec-correct (if uncompressed) image::encode_png (BACKLOG.md item [25]'s PNG half — see that function's own doc for the hand-rolled DEFLATE-stored- block approach); .jpg/.jpeg and .tif/.tiff go through jpeg-encoder/tiff (2026-09-23). This is raster-pixel-data export only, NOT savefig's full-figure export -- see save_figure's own .png rejection for that distinction. Extension-dispatch on WRITE, magic-byte sniffing on READ, is not an inconsistency: writing has no bytes to sniff yet, and the extension is the only thing that says what the caller wants. Reading has the real bytes, which are never wrong.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.