Recursive AI
oneimageGoAvailable

oneimage for Go

A free, open-source image library for Go, MIT or Apache-2.0. It decodes and encodes PNG, JPEG, GIF, WebP, BMP, ICO and TIFF with codecs of its own, and resizes, filters, draws and composites with ICC color management.

Pure Go: no cgo and no third-party dependencies. SVG, rich text, Photoshop files, Windows metafiles and PDF pages render to pixels too.

go get recursiveai.org/go/oneimage@v1.0.0

A thumbnail and a PDF page

The README’s program, run with Go 1.26.8 against v1.0.0 from the module proxy. A 1920 × 1920 JPEG becomes a 400 × 400 WebP thumbnail, and page 1 of a PDF becomes a PNG at 150 dpi.

Decode, edit and encode
main.goGo
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()
    // decoded small, fitted, upright
    thumb, err := oneimage.Thumbnail(photo, 400, 400)
    if err != nil {
        log.Fatal(err)
    }
    out, err := os.Create("thumb.webp")
    if err != nil {
        log.Fatal(err)
    }
    defer out.Close()
    opts := &oneimage.WebPOptions{Quality: 80}
    if err := oneimage.EncodeWebP(out, thumb, opts); err != nil {
        log.Fatal(err)
    }
 
    report, err := os.Open("report.pdf")
    if err != nil {
        log.Fatal(err)
    }
    defer report.Close()
    // page 1 at 150 dpi
    page, err := pdf.RenderPage(report, 0, pdf.WithDPI(150))
    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)
    }
}
Shellsh
go mod init example.com/thumbs
go get recursiveai.org/go/oneimage@v1.0.0
go mod tidy
go run .
thumb.webp400 × 400, 21 KB
NASA’s Apollo 17 photograph of Earth, Africa and Antarctica under swirls of cloud, on black.
page1.png1275 × 1650, 282 KB
Page 1 of IRS Form W-9, “Request for Taxpayer Identification Number and Certification”, with its boxes, checkboxes and small print.

Speed

From the README at v1.0.0: medians of five runs on an AMD Ryzen 9 7950X3D with Go 1.26.8 on Linux, 8 threads. A ratio under 1 means oneimage took less time.

Method and machine Every row

“Medians of five runs on one machine (AMD Ryzen 9 7950X3D, Linux, Go 1.26.8, 8 threads), every row checked for the same result before it is timed. The comparison is the fastest pure-Go competitor for the task; where there is none, the C# library at the same degree of parallelism. A ratio under 1 means oneimage-go took less time.”

Taskoneimage-gocompared withtimeratio
PNG decode, photo27.35 msstandard library53.44 ms0.51
PNG encode, photo114.52 msstandard library422.65 ms0.27
JPEG decode, 24 MP 4:2:089.54 msstandard library168.14 ms0.53
JPEG encode, 24 MP 4:2:068.41 msstandard library322.02 ms0.21
GIF decode4.56 msstandard library8.68 ms0.53
GIF encode8.50 msstandard library12.60 ms0.68
WebP decode, lossy photo25.14 msgolang.org/x/image32.56 ms0.77
WebP encode, lossless photo247.50 msnativewebp447.31 ms0.55
WebP encode, lossy 2000×150039.43 msC# library49.10 ms0.80
BMP decode3.68 msgolang.org/x/image3.89 ms0.95
TIFF decode, LZW7.37 msgolang.org/x/image63.67 ms0.12
Thumbnail from a 24 MP JPEG66.74 msdisintegration/imaging215.97 ms0.31
Resize 6000×4000 to 400 wide, Lanczos319.49 msdisintegration/imaging39.10 ms0.50
Inflate, mixed data31.39 msstandard library37.56 ms0.84
Deflate level 6, mixed data18.96 msstandard library101.12 ms0.19
ICC conversion, sRGB to Display P3, 1 MP2.12 msmandykoh/prism9.54 ms0.22
ICC conversion, CMYK to sRGB, 4 MP7.10 msC# library17.81 ms0.40
SVG render, scene5.80 msoksvg43.54 ms0.13
Fill a 1000-point polygon, 2000×20000.76 msfogleman/gg7.68 ms0.10
Shaped text wrap, 24,000 characters3.29 msgo-text/typesetting3.68 ms0.89
PSD merged image, 3000×20003.04 msoov/psd4.49 ms0.68
Metafile render5.99 msC# library6.95 ms0.86
Flatten ten 24 MP layers34.61 msC# library40.85 ms0.85
PDF vector page, 150 dpi48.30 mspdfview69.99 ms0.69
PDF text page, 150 dpi13.94 mspdfview40.61 ms0.34
PDF image page, 150 dpi74.52 mspdfview105.62 ms0.71

“The PDF rows time bytes to pixels for page 1 against github.com/richardwilkes/pdfview, which renders a page on one thread; oneimage-go uses up to 8 workers per page. On one thread (GOMAXPROCS=1) the same pages take 169.79 ms, 36.84 ms and 132.78 ms (vector, text, image), so the vector and image pages are slower than pdfview there.”

README.md at v1.0.0

What it does