Recursive AI

Getting started with oneimage for Go

All oneimage Go docs

Install oneimage for Go, make a WebP thumbnail from a JPEG and a PNG from a PDF page, then decode, resize, sharpen and encode an image, convert in one call, and check for input that isn’t an image.

oneimage for Go is a pure-Go image library: PNG, JPEG, GIF, WebP, BMP, ICO and TIFF decoded and encoded by its own codecs, with ICC color management, resize, filters, drawing and compositing, and SVG, rich text, Photoshop files, Windows metafiles and PDF pages rendered to pixels. Every sample on this page was run with Go 1.26.8 against recursiveai.org/go/oneimage v1.0.0 from the Go module proxy; the output under each one is what it printed, and the images are the files it wrote.

Install

oneimage needs Go 1.24 or newer.

Terminal
mkdir thumbs && cd thumbs
go mod init example.com/thumbs
go get recursiveai.org/go/oneimage@v1.0.0

The license is MIT or Apache-2.0, at your choice. There is no cgo and no unsafe. The only modules it requires are Recursive AI’s own pure-Go recursiveai.org/go/onefont v1.2.0 and recursiveai.org/go/onepdf v1.0.1 (with its fonts module).

A thumbnail and a PDF page

The README’s program, saved as main.go. It reads photo.jpg and report.pdf from the current folder; here they were NASA’s Apollo 17 photograph of Earth (1920 × 1920) and the IRS Form W-9.

GO
package main
 
import (
	"log"
	"os"
 
	"recursiveai.org/go/oneimage"
	"recursiveai.org/go/oneimage/pdf"
)
 
func main() {
	photo, err := os.Open("photo.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer photo.Close()
	thumb, err := oneimage.Thumbnail(photo, 400, 400) // decoded small, fitted, upright
	if err != nil {
		log.Fatal(err)
	}
	out, err := os.Create("thumb.webp")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()
	if err := oneimage.EncodeWebP(out, thumb, &oneimage.WebPOptions{Quality: 80}); err != nil {
		log.Fatal(err)
	}
 
	report, err := os.Open("report.pdf")
	if err != nil {
		log.Fatal(err)
	}
	defer report.Close()
	page, err := pdf.RenderPage(report, 0, pdf.WithDPI(150)) // page 1 at 150 dpi
	if err != nil {
		log.Fatal(err)
	}
	png, err := os.Create("page1.png")
	if err != nil {
		log.Fatal(err)
	}
	defer png.Close()
	if err := oneimage.EncodePNG(png, page, nil); err != nil {
		log.Fatal(err)
	}
}

The pdf package pulls in onepdf, so run go mod tidy once to record its modules, then run the program:

Terminal
go mod tidy
go run .

It prints nothing and writes two files: thumb.webp, 400 × 400 and 21 KB, and page1.png, 1275 × 1650 and 282 KB.

thumb.webp: the Apollo 17 photograph of Earth, Africa and Antarctica under swirls of cloud, on black, 400 × 400

page1.png: page 1 of Form W-9, “Request for Taxpayer Identification Number and Certification”, with its boxes, checkboxes and small print, rendered at 150 dpi

Thumbnail decodes the JPEG at a reduced size when it can, fits it inside the box and turns it upright by its Exif orientation. RenderPage takes a zero-based page index.

Decode, edit, encode

In a new module (go mod init example.com/edit and the same go get), with photo.jpg next to main.go:

GO
package main
 
import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"os"
 
	"recursiveai.org/go/oneimage"
)
 
func main() {
	data, err := os.ReadFile("photo.jpg")
	if err != nil {
		log.Fatal(err)
	}
 
	// Size and metadata, without decoding pixels.
	info, err := oneimage.Identify(bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("photo.jpg: %s, %dx%d, %d-bit\n", info.Format, info.Width, info.Height, info.BitDepth)
 
	// Decode, resize to 800 px wide, sharpen.
	img, err := oneimage.Decode(bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}
	ops := []oneimage.Op{
		oneimage.Resize(800, 0),
		oneimage.Sharpen(0.6, 1, 0),
	}
	small, err := oneimage.Apply(img, ops...)
	if err != nil {
		log.Fatal(err)
	}
	steps, _ := oneimage.Describe(img, ops...)
	for _, s := range steps {
		fmt.Println(s)
	}
 
	// Encode as JPEG.
	out, err := os.Create("photo-800.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()
	if err := oneimage.EncodeJPEG(out, small, &oneimage.JPEGOptions{Quality: 85}); err != nil {
		log.Fatal(err)
	}
 
	// Decode, edit and encode in one call.
	gray, err := os.Create("photo-gray.webp")
	if err != nil {
		log.Fatal(err)
	}
	defer gray.Close()
	err = oneimage.Convert(gray, "webp", bytes.NewReader(data),
		oneimage.Resize(400, 0), oneimage.Grayscale())
	if err != nil {
		log.Fatal(err)
	}
 
	// Bytes that aren't an image.
	_, err = oneimage.Decode(bytes.NewReader([]byte("not an image")))
	fmt.Println(err)
	fmt.Println(errors.Is(err, oneimage.ErrFormat))
}
Terminal
go run .
Text
photo.jpg: jpeg, 1920x1920, 8-bit
sharpen(amount=0.6, radius=1, threshold=0, linear=True, edge=clamp) -> 800x800
  resize(width=800, height=0, fit=contain, anchor=center, upscale=True, filter=auto, linear=True, background=[0, 0, 0, 0]) -> 800x800
    decode(format=jpeg, frames=1) -> 1920x1920
oneimage: unrecognised image format
true

It wrote photo-800.jpg (800 × 800, 118 KB) and photo-gray.webp (400 × 400, 17 KB):

photo-800.jpg: the Earth photograph resized to 800 × 800 and sharpened

photo-gray.webp: the Earth photograph at 400 × 400 in grayscale

  • Identify reads the size and metadata from the header without decoding pixels.
  • Decode detects the format from the first bytes and returns an *Image, which implements image.Image.
  • Resize(800, 0) leaves the height free, so the aspect ratio is kept. Resizing is done in linear light by default.
  • Operations are values. Apply runs them in order and never changes its input. Describe lists what ran, last operation first, with each result’s size.
  • Convert decodes, applies operations and encodes to the format you name.
  • Errors are values: errors.Is matches ErrFormat, ErrLimit, ErrUnsupported and ErrOperation.

Every encoder and operation accepts any image.Image, so pixels from the standard library or another package go straight in. A blank import of recursiveai.org/go/oneimage/stdreg makes the standard library’s image.Decode read every format oneimage does.

Next