Skip to content

Read, write, and convert formats

Copy page

The input is input.jpg; the output is output.webp. These examples show the shape of each binding. 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 bindings 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 {
return err
}
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 choice.