Skip to content

Read, write, and convert formats

Copy page

The input is input.jpg; the output is output.webp. These examples show the structure of each interface. The reference output was produced by govips and passed format and dimension checks.

Gate of the Orient outlined in red lights above a park walkway at night
Input: JPEG1938 × 1292, 610,191 bytes
The same night scene converted to WebP without changing its dimensions
Reference output: WebP1938 × 1292, 399,106 bytes, quality 82

All four interfaces have passed format and dimension checks; encoded bytes from different libvips versions are not expected to be identical.

package main
import (
"fmt"
"os"
"github.com/davidbyttow/govips/v2/vips"
)
func main() {
if err := vips.Startup(nil); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer vips.Shutdown()
image, err := vips.NewImageFromFile("input.jpg")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer image.Close()
params := vips.NewWebpExportParams()
params.Quality = 82
data, _, err := image.ExportWebp(params)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := os.WriteFile("output.webp", data, 0o644); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

Format conversion decodes and re-encodes pixels. Output quality, metadata, ICC profiles, alpha, animation pages, and codec availability can all change. Quality = 100 or Q=100 does not mean lossless; lossless encoding is a separate option.

The same 1938 × 1292 JPEG at WebP quality 82 produced four 1938 × 1292, 3-band, non-alpha files on 2026-10-12. Byte size differed: govips 399,106, sharp 398,898, and both pyvips and the CLI 402,162. sharp used its bundled libvips 8.18.7; the other three used system libvips 8.17.3. Matching dimensions are not the same encoding.