images

package module
v0.0.0-...-0b28345 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

README

go-ruby-images/images

images — go-ruby-images

Docs License Go Coverage

A pure-Go (no cgo) image library that unifies three Ruby image toolkits behind one clean, Ruby-bindable Go API — with no ImageMagick, no libvips, no C QR/codec library. It is an image backend for go-embedded-ruby (rbgo), destined to be exposed as require "images", but is a standalone, reusable module — a sibling of go-ruby-rqrcode, go-ruby-regexp and go-ruby-erb.

It combines, in one coherent module:

  1. Processing — a MiniMagick / ruby-vips replacement: decode PNG, JPEG, GIF, BMP, TIFF and WebP, report dimensions / format / metadata, and resize, thumbnail, crop, rotate, flip, flop, grayscale, format convert and to_blob / write — over the standard library and golang.org/x/image.
  2. A chunky_png-style canvas (sub-package canvas) — per-pixel access, primitive drawing (points, lines, rectangles, circles), alpha compositing, colour helpers (packed 0xRRGGBBAA, hex parse), and PNG read/write.
  3. scikit-image operations — blur, sharpen, unsharp mask, median, Sobel / Prewitt / Scharr / Laplacian / Canny edges, threshold / Otsu, morphology (erode / dilate / open / close) and colour ops, reusing the go-images engine rather than reimplementing it.

Every operation is deterministic (fixed algorithms, no randomness), so results reproduce across the six supported 64-bit architectures and the js/wasm and wasip1/wasm targets.

Install

go get github.com/go-ruby-images/images

Usage

Processing (MiniMagick / ruby-vips style)
import "github.com/go-ruby-images/images"

img, _ := images.Open("photo.jpg")
w, h := img.Dimensions()          // dimensions
_ = img.Format()                  // "jpeg"
_ = img.Metadata()                // {Width, Height, Format, ColorModel}

thumb, _ := img.Thumbnail(200, 200)     // fit inside a box, keep aspect
crop, _  := img.Crop(10, 10, 100, 100)  // region
rot, _   := img.Rotate(90)              // multiples of 90
flipped  := img.Flip().Flop()           // vertical then horizontal mirror

png, _ := img.Convert("png")
blob, _ := png.ToBlob()                 // encoded bytes
_ = png.Write("out.png")                // format from the path extension
chunky_png-style canvas
import "github.com/go-ruby-images/images/canvas"

c := canvas.New(64, 64, canvas.White)
_ = c.Set(1, 1, canvas.RGB(255, 0, 0))
c.Line(0, 0, 63, 63, canvas.Black)
c.Rect(8, 8, 40, 40, canvas.Black, canvas.Blue)   // stroke, fill
c.Circle(32, 32, 16, canvas.Green)
red, _ := canvas.FromHex("#ff0000")               // packed 0xRRGGBBAA
_ = c.SavePNG("draw.png")
scikit-image operations
img, _ := images.Open("scan.png")
edges, _ := img.Canny(1.0, 30, 90)     // Canny edges
blurred, _ := img.GaussianBlur(1.5)
bw := img.Otsu()                        // automatic threshold
opened, _ := img.Open(2)                // morphological opening
_ = img.Sobel(); _ = img.Sharpen(); _ = img.Invert()

Tests & coverage

The suite is deterministic and self-contained (images are synthesised in memory), and holds 100% statement coverage including every error branch:

GOWORK=off go test ./... -covermode=count -coverprofile=cover.out
go tool cover -func=cover.out | grep -v 100.0%   # only the total remains

CI additionally builds and tests on the six supported 64-bit architectures (amd64, arm64, riscv64, loong64, ppc64le, s390x), builds js/wasm and wasip1/wasm, and runs on Linux, macOS and Windows.

License

BSD-3-Clause — see LICENSE. Copyright (c) 2026, the go-ruby-images/images authors.

Documentation

Overview

Package images is a pure-Go (no cgo) image library that backs the Ruby `images` gem for the go-embedded-ruby interpreter (rbgo). It unifies three capability sets behind one clean, Ruby-bindable Go API:

  • Processing (MiniMagick / ruby-vips replacement): the Image type decodes PNG, JPEG, GIF, BMP, TIFF and WebP, reports dimensions/format/metadata, and offers resize, thumbnail, crop, rotate, flip, flop, grayscale, format conversion, and blob/file output — with no ImageMagick or libvips.
  • A chunky_png-style canvas lives in the sub-package github.com/go-ruby-images/images/canvas, with per-pixel access, primitive drawing, alpha composition and PNG I/O.
  • scikit-image / go-images operations are exposed as methods on Image (blur, sharpen, edges, threshold, morphology, colour ops), reusing the github.com/go-images/images engine rather than reimplementing it.

Every operation is deterministic (fixed algorithms, no randomness) so the results are reproducible across the six supported 64-bit architectures and the js/wasm and wasip1/wasm targets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encodable

func Encodable() []string

Encodable reports the canonical format names this package can encode. WebP is intentionally absent: there is no pure-Go WebP encoder, so encoding to WebP returns an error rather than pulling in a cgo dependency.

func Supported

func Supported() []string

Supported reports the canonical format names this package can decode.

Types

type Image

type Image struct {
	// contains filtered or unexported fields
}

Image is the processing handle at the heart of the library — the pure-Go analogue of a MiniMagick::Image or a Vips::Image. It wraps an *image.RGBA together with the format it was decoded from (or last converted to). All transforming methods return a new Image and never mutate the receiver, so a handle can be shared safely.

func Decode

func Decode(r io.Reader) (*Image, error)

Decode reads and decodes an image from r, auto-detecting the format.

func FromImage

func FromImage(src image.Image, format string) *Image

FromImage wraps an existing image.Image, converting it to RGBA. The reported format defaults to PNG. It bridges the standard-library image world into the Image API.

func New

func New(width, height int, fill color.Color) *Image

New returns a fresh Image of the given size filled with fill. The default output format is PNG. It is the entry point for building an image from scratch, mirroring Vips::Image.black plus a fill.

func Open

func Open(path string) (*Image, error)

Open reads and decodes the image file at path.

func Read

func Read(data []byte) (*Image, error)

Read decodes an image from an in-memory byte slice, the equivalent of MiniMagick::Image.read(blob).

func (*Image) At

func (im *Image) At(x, y int) (color.RGBA, error)

At returns the colour at (x, y). It reports an error when the coordinates fall outside the image bounds.

func (*Image) Blur

func (im *Image) Blur(radius int) (*Image, error)

Blur returns a box blur of the given radius (radius must be positive).

func (*Image) Brightness

func (im *Image) Brightness(delta float64) *Image

Brightness returns the image with delta added to every channel (clamped).

func (*Image) Canny

func (im *Image) Canny(sigma, low, high float64) (*Image, error)

Canny returns the Canny edge map for the given Gaussian sigma and the low and high hysteresis thresholds. It errors on a non-positive sigma.

func (*Image) Clone

func (im *Image) Clone() *Image

Clone returns a deep copy of the image.

func (*Image) Close

func (im *Image) Close(radius int) (*Image, error)

Close returns the morphological closing (dilation then erosion).

func (*Image) Contrast

func (im *Image) Contrast(factor float64) *Image

Contrast returns the image with contrast scaled by factor about mid-grey.

func (*Image) Convert

func (im *Image) Convert(format string) (*Image, error)

Convert changes the output format used by Encode, ToBlob and Write (when the path carries no extension). It returns an error for formats this package cannot encode.

func (*Image) Convolve

func (im *Image) Convolve(width, height int, weights []float64) (*Image, error)

Convolve returns the image convolved with an arbitrary odd-sized kernel of width x height weights (row-major). It errors on non-positive, even, or mismatched dimensions.

func (*Image) Crop

func (im *Image) Crop(x, y, width, height int) (*Image, error)

Crop returns the rectangular region of size width x height anchored at (x, y). The region must lie fully within the image.

func (*Image) Dilate

func (im *Image) Dilate(radius int) (*Image, error)

Dilate returns the grayscale morphological dilation (radius must be positive).

func (*Image) Dimensions

func (im *Image) Dimensions() (int, int)

Dimensions returns the width and height in pixels.

func (*Image) Encode

func (im *Image) Encode(w io.Writer) error

Encode writes the image to w in its current format.

func (*Image) Erode

func (im *Image) Erode(radius int) (*Image, error)

Erode returns the grayscale morphological erosion over a square structuring element of the given radius (radius must be positive).

func (*Image) Flip

func (im *Image) Flip() *Image

Flip returns the image mirrored top-to-bottom (ImageMagick -flip).

func (*Image) Flop

func (im *Image) Flop() *Image

Flop returns the image mirrored left-to-right (ImageMagick -flop).

func (*Image) Format

func (im *Image) Format() string

Format returns the current canonical format name (for example "png").

func (*Image) GaussianBlur

func (im *Image) GaussianBlur(sigma float64) (*Image, error)

GaussianBlur returns a Gaussian blur with the given sigma (sigma must be positive).

func (*Image) Grayscale

func (im *Image) Grayscale() *Image

Grayscale returns the image converted to grayscale (Rec. 601 luminance replicated across the R, G and B channels).

func (*Image) Height

func (im *Image) Height() int

Height returns the image height in pixels.

func (*Image) Invert

func (im *Image) Invert() *Image

Invert returns the photographic negative of the image.

func (*Image) Laplacian

func (im *Image) Laplacian() *Image

Laplacian returns the Laplacian second-derivative edge map.

func (*Image) Median

func (im *Image) Median(radius int) (*Image, error)

Median returns a median-filtered image over a square window of the given radius (radius must be positive).

func (*Image) Metadata

func (im *Image) Metadata() Metadata

Metadata returns the descriptive properties of the image.

func (*Image) Open

func (im *Image) Open(radius int) (*Image, error)

Open returns the morphological opening (erosion then dilation).

func (*Image) Otsu

func (im *Image) Otsu() *Image

Otsu returns the image binarised at its automatically chosen Otsu threshold.

func (*Image) OtsuThreshold

func (im *Image) OtsuThreshold() uint8

OtsuThreshold returns the automatically chosen Otsu threshold value.

func (*Image) Prewitt

func (im *Image) Prewitt() *Image

Prewitt returns the Prewitt gradient-magnitude edge map.

func (*Image) RGBA

func (im *Image) RGBA() *image.RGBA

RGBA returns the underlying *image.RGBA. Callers that mutate it affect this Image; use Clone first to keep the original intact.

func (*Image) Resize

func (im *Image) Resize(width, height int) (*Image, error)

Resize returns the image scaled to width x height using bilinear interpolation. Both dimensions must be positive.

func (*Image) ResizeNearest

func (im *Image) ResizeNearest(width, height int) (*Image, error)

ResizeNearest returns the image scaled to width x height using nearest-neighbour sampling, which preserves hard pixel edges.

func (*Image) Rotate

func (im *Image) Rotate(degrees int) (*Image, error)

Rotate returns the image rotated clockwise by degrees, which must be a multiple of 90 (0, 90, 180 or 270 after normalisation).

func (*Image) Scharr

func (im *Image) Scharr() *Image

Scharr returns the Scharr gradient-magnitude edge map.

func (*Image) Sharpen

func (im *Image) Sharpen() *Image

Sharpen returns the image sharpened with a fixed 3x3 kernel.

func (*Image) Sobel

func (im *Image) Sobel() *Image

Sobel returns the Sobel gradient-magnitude edge map.

func (*Image) Threshold

func (im *Image) Threshold(t uint8) *Image

Threshold returns a binary image: pixels whose luminance is at least t become white, the rest black.

func (*Image) Thumbnail

func (im *Image) Thumbnail(maxWidth, maxHeight int) (*Image, error)

Thumbnail scales the image to fit inside a maxWidth x maxHeight box while preserving the aspect ratio, the equivalent of MiniMagick's `resize "WxH"`. The result is never enlarged beyond the original dimensions and each side is at least one pixel. Both bounds must be positive.

func (*Image) ToBlob

func (im *Image) ToBlob() ([]byte, error)

ToBlob encodes the image in its current format and returns the bytes, the equivalent of MiniMagick::Image#to_blob.

func (*Image) UnsharpMask

func (im *Image) UnsharpMask(radius, amount float64) (*Image, error)

UnsharpMask returns an unsharp-masked image (radius must be positive).

func (*Image) Width

func (im *Image) Width() int

Width returns the image width in pixels.

func (*Image) Write

func (im *Image) Write(path string) error

Write encodes the image to path. The format is taken from the path extension when present, otherwise the image's current format is used.

type Metadata

type Metadata struct {
	Width      int
	Height     int
	Format     string
	ColorModel string
}

Metadata bundles the read-only descriptive properties of an image.

Directories

Path Synopsis
Package canvas is a pure-Go, chunky_png-style RGBA canvas.
Package canvas is a pure-Go, chunky_png-style RGBA canvas.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL