sharp for Go
Fluent, chainable image processing for Go.
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
$ go get github.com/malcolmston/sharpQuick start
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
Pipelinebuilt withNew,FromFile,FromBytesorFromReader— copy-on-construct, so the source is never mutated - Resize via
Resize(ResizeOptions{…})andResizeTo—FitExact/FitContain/FitCoverwithNearestorBilinearsampling - Region & layout —
Crop/Extract,Extend(pad with a fill colour),Rotate90/180/270, arbitraryRotate,FlipVertical/FlipHorizontal(akaFlip/Flop) - Colour suite —
Grayscale,Negate/Invert,Tint,Brightness,Contrast,Gamma,Saturation,Threshold - Convolution — separable-Gaussian
Blur,Sharpen, and a genericConvolveoverNewKernel - Composition —
Compositewith alpha blending plusGravity/offset placement, andFlattenonto a solid background - PNG + JPEG I/O —
ToPNG,ToJPEG,ToImage,ToFilewithPNGOptions/JPEGOptionsquality 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) andStats(per-channel means), plusCloneto branch a pipeline - Zero dependencies — pure Go standard library, nothing to audit but the toolchain