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
| Function | Signature | Description |
|---|---|---|
new | image.new(w: int, h: int, r: int, g: int, b: int) → int | Create a solid-colour bitmap; returns a handle |
load | image.load(path: string) → int | Load from a file (-1 on error) |
decode | image.decode(bytes: string, format?: string) → int | Decode raw image bytes (-1 on error) |
save | image.save(h: int, path: string, format?: string) → bool | Write to a file |
encode | image.encode(h: int, format: string) → string | Encode to bytes — handy for an HTTP response body |
size | image.size(h: int) → object | Dimensions as {w, h} |
free | image.free(h: int) → bool | Release the handle |
Transforms
Each returns a new handle.
| Function | Signature | Description |
|---|---|---|
resize | image.resize(h, w, h, filter?) → int | Resize to exact dimensions |
fit | image.fit(h, maxW, maxH, filter?) → int | Scale down to fit, preserving aspect ratio |
thumbnail | image.thumbnail(h, w, h) → int | Smart crop-and-resize |
crop | image.crop(h, x, y, w, h) → int | Crop a rectangle |
rotate | image.rotate(h, angle) → int | Rotate angle degrees counter-clockwise |
flipH / flipV | image.flipH(h) → int | Flip horizontally / vertically |
blur / sharpen | image.blur(h, sigma) → int | Gaussian blur / sharpen |
grayscale / invert | image.grayscale(h) → int | Desaturate / 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)