sharp for Go

github.com/malcolmston/sharp

Fluent, chainable image processing for Go.

 GitHubports lovell/sharp

A from-scratch, standard-library-only Go take on the ergonomics of Node's sharp: build a Pipeline, chain operations, export to a format. Everything runs on the Go standard library (image, image/color, image/png, image/jpeg and math) over an in-memory RGBA buffer — no cgo, no third-party dependencies. Construct a pipeline from an image, file, byte slice or reader; the source is copied on construction and never mutated. Operations apply eagerly and chain by returning the same *Pipeline: resize with FitExact/FitContain/FitCover and Nearest/Bilinear sampling, crop/extract, extend, rotate and flip, a full colour suite (grayscale, negate, tint, brightness, contrast, gamma, saturation, threshold), separable-Gaussian blur, sharpen and generic convolution, alpha-blended compositing and flatten, plus PNG/JPEG in and out. A deferred error model retains the first failure and turns later steps into no-ops, so it surfaces once from the terminal export or from Err.

Install

shell
$ go get github.com/malcolmston/sharp

Quick start

main.go
import "github.com/malcolmston/sharp"

buf, _ := sharp.FromFile("input.jpg").
	Resize(sharp.ResizeOptions{Width: 800, Fit: sharp.FitContain}).
	Grayscale().
	Blur(2).
	ToJPEG(85)
_ = sharp.FromFile("input.jpg").ToFile("output.jpg", sharp.FormatJPEG, 85)

Features

  • Fluent Pipeline built with New, FromFile, FromBytes or FromReader — copy-on-construct, so the source is never mutated
  • Resize via Resize(ResizeOptions{…}) and ResizeToFitExact/FitContain/FitCover with Nearest or Bilinear sampling
  • Region & layout — Crop/Extract, Extend (pad with a fill colour), Rotate90/180/270, arbitrary Rotate, FlipVertical/FlipHorizontal (aka Flip/Flop)
  • Colour suite — Grayscale, Negate/Invert, Tint, Brightness, Contrast, Gamma, Saturation, Threshold
  • Convolution — separable-Gaussian Blur, Sharpen, and a generic Convolve over NewKernel
  • Composition — Composite with alpha blending plus Gravity/offset placement, and Flatten onto a solid background
  • PNG + JPEG I/O — ToPNG, ToJPEG, ToImage, ToFile with PNGOptions/JPEGOptions quality control
  • Deferred error model — the first failure is retained, later steps become no-ops, and it surfaces from the terminal call or Err
  • Introspection — Metadata (width/height/format) and Stats (per-channel means), plus Clone to branch a pipeline
  • Zero dependencies — pure Go standard library, nothing to audit but the toolchain
sharp