image

The image module does pure-Go image processing (no CGO, so cross-compilation stays clean). It supports PNG, JPEG, GIF, TIFF, and BMP. Operations work on an integer handle returned by new, load, or decode; transforms return a new handle, so free the ones you no longer need. Import it with import image.

Loading & saving

FunctionSignatureDescription
newimage.new(w: int, h: int, r: int, g: int, b: int) → intCreate a solid-colour bitmap; returns a handle
loadimage.load(path: string) → intLoad from a file (-1 on error)
decodeimage.decode(bytes: string, format?: string) → intDecode raw image bytes (-1 on error)
saveimage.save(h: int, path: string, format?: string) → boolWrite to a file
encodeimage.encode(h: int, format: string) → stringEncode to bytes — handy for an HTTP response body
sizeimage.size(h: int) → objectDimensions as {w, h}
freeimage.free(h: int) → boolRelease the handle

Transforms

Each returns a new handle.

FunctionSignatureDescription
resizeimage.resize(h, w, h, filter?) → intResize to exact dimensions
fitimage.fit(h, maxW, maxH, filter?) → intScale down to fit, preserving aspect ratio
thumbnailimage.thumbnail(h, w, h) → intSmart crop-and-resize
cropimage.crop(h, x, y, w, h) → intCrop a rectangle
rotateimage.rotate(h, angle) → intRotate angle degrees counter-clockwise
flipH / flipVimage.flipH(h) → intFlip horizontally / vertically
blur / sharpenimage.blur(h, sigma) → intGaussian blur / sharpen
grayscale / invertimage.grayscale(h) → intDesaturate / invert colours

Example

import image

let src = image.load("photo.jpg")
let thumb = image.fit(src, 200, 200, "lanczos")
image.save(thumb, "thumb.png", "png")

image.free(thumb)
image.free(src)
Standard library · View as Markdown · llms-full.txt