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, 402,162 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 five 1938 × 1292, 3-band, non-alpha files in the locked Runner on 2026-10-17. govips v2.20.0, pyvips, the CLI, and Image each wrote 402,162 bytes; sharp wrote 398,898 bytes. The govips size change comes from retaining the source ICC profile by default. sharp used its bundled libvips 8.18.7; the other paths used system libvips 8.17.3. Equal byte counts still do not prove identical pixels or defaults.