Skip to content

Animation keeps one frame or becomes a tall image

Copy page

A first-frame-only result, a tall strip, and misaligned frames after resizing require separate checks: loaded page count, page height, and timing metadata. This three-frame GIF makes frame order directly visible.

The frames have red, green, and blue backgrounds, one, two, and three white bars, and a moving marker. Each frame is 320×180 with delays of 200 / 400 / 600 ms and infinite looping. Animations initially show their exported first frame; use the button to play or stop. The tall strip and metadata also expose frame structure without playback.

govips v2.20.0 / libvips 8.17.3

Reference input · govips · Reference
Reference input320 × 180, 2,156 bytes{"Bitdepth":8,"Dither":0,"Effort":7,"NumPages":-1,"pageHeight":180,"resize":false}Frames: 3 · 200 / 400 / 600 ms · Loop: infiniteDownload generated output
Load first frame only · govips · Reference
Load first frame only320 × 180, 599 bytes{"Bitdepth":8,"Dither":0,"Effort":7,"NumPages":1,"pageHeight":180,"resize":false}Download generated output
Load every frame · govips · Reference
Load every frame320 × 180, 2,156 bytes{"Bitdepth":8,"Dither":0,"Effort":7,"NumPages":-1,"pageHeight":180,"resize":false}Frames: 3 · 200 / 400 / 600 ms · Loop: infiniteDownload generated output
Missing frame height: one tall image · govips · Reference
Missing frame height: one tall image320 × 540, 2,335 bytes{"Bitdepth":8,"Dither":0,"Effort":7,"NumPages":-1,"pageHeight":540,"resize":false}Download generated output
Resize with frame boundaries · govips · Reference
Resize with frame boundaries160 × 90, 1,025 bytes{"Bitdepth":8,"Dither":0,"Effort":7,"NumPages":-1,"pageHeight":90,"resize":true}Frames: 3 · 200 / 400 / 600 ms · Loop: infiniteDownload generated output
Visible symptom Controlled change Correction
Only the red first frame remains Load one page only Set govips NumPages=-1, pyvips/CLI n=-1, or sharp animated: true explicitly
Three frames become one 320×540 image Reconstruct all decoded pixels as a raw image without a per-frame height Set the correct page-height, delay, and loop when rebuilding the animation; sharp raw input uses raw.pageHeight
Frame boundaries are wrong after resizing Page height does not follow the per-frame geometry Here the resized result must contain three 160×90 frames; the CLI uses the save option page-height=90, while pyvips explicitly updates the image field

The libvips multipage model stacks frames vertically. Loading this entire source produces a total decoded height of 540 and a page-height of 180. Those dimensions describe different things. Copying raw pixels alone does not retain page count, frame height, or timing information.

The govips v2.20.0 example loads every frame before resizing and preserves frame boundaries. Sharp applies its own animation-aware resizing. Pyvips and the CLI explicitly correct the resized page height. A convenience wrapper in one binding does not establish defaults for the others.

The complete programs generate both the failure cases and corrected outputs. Reconstructing the raw tall image is a controlled failure, rather than a required step for ordinary animation conversion.

Run command

go run . three-frames.gif output/

Complete source

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/davidbyttow/govips/v2/vips"
	"github.com/webp-sh/vips-handbook/examples/internal/recipeutil"
)

func main() {
	if err := run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
func run(args []string) error {
	if len(args) != 3 {
		return fmt.Errorf("usage: main INPUT OUTPUT_DIRECTORY")
	}
	if err := os.MkdirAll(args[2], 0o755); err != nil {
		return err
	}
	if err := vips.Startup(nil); err != nil {
		return err
	}
	defer vips.Shutdown()
	outputs := []recipeutil.Output{}
	for _, id := range []string{"source", "first-frame", "all-frames", "tall-strip", "resized"} {
		output, err := makeVariant(args[1], args[2], id)
		if err != nil {
			return err
		}
		outputs = append(outputs, output)
	}
	return recipeutil.WriteResult(args[2], "animation-pages", args[1], outputs)
}

func makeVariant(input, dir, id string) (recipeutil.Output, error) {
	params := vips.NewImportParams()
	if id != "first-frame" {
		params.NumPages.Set(-1)
	}
	image, err := vips.LoadImageFromFile(input, params)
	if err != nil {
		return recipeutil.Output{}, err
	}
	defer func() { image.Close() }()
	if id == "tall-strip" {
		pixels, err := image.ToBytes()
		if err != nil {
			return recipeutil.Output{}, err
		}
		tall, err := vips.NewImageFromMemory(pixels, image.Width(), image.Height(), image.Bands(), vips.BandFormatUchar, vips.InterpretationSRGB)
		if err != nil {
			return recipeutil.Output{}, err
		}
		image.Close()
		image = tall
		// No SetPageHeight: the new raw image is a single tall page.
	}
	if id == "resized" {
		if err := image.Resize(0.5, vips.KernelNearest); err != nil {
			return recipeutil.Output{}, err
		}
	}
	if id == "all-frames" || id == "resized" || id == "source" {
		if err := image.SetPageDelay([]int{200, 400, 600}); err != nil {
			return recipeutil.Output{}, err
		}
		if err := image.SetLoop(0); err != nil {
			return recipeutil.Output{}, err
		}
	}
	export := vips.NewGifExportParams()
	export.Dither = 0
	export.Bitdepth = 8
	export.Effort = 7
	data, meta, err := image.ExportGIF(export)
	if err != nil {
		return recipeutil.Output{}, err
	}
	name := id + ".gif"
	if err := os.WriteFile(filepath.Join(dir, name), data, 0o644); err != nil {
		return recipeutil.Output{}, err
	}
	n := 1
	if id != "first-frame" {
		n = -1
	}
	return recipeutil.Output{Path: name, Format: "gif", Width: meta.Width, Height: meta.Height, Size: len(data), Parameters: map[string]any{"NumPages": n, "resize": id == "resized", "pageHeight": image.PageHeight(), "Dither": 0, "Bitdepth": 8, "Effort": 7}}, nil
}
  1. Reload every exported frame and check three pages, with total height equal to three times page height.
  2. Inspect the red, green, and blue backgrounds and bar counts in order; total dimensions alone cannot detect reversed frames.
  3. Confirm delays remain 200 / 400 / 600 ms and loop is 0, meaning infinite looping in this example.
  4. Confirm that the first-frame and tall-strip cases each contain one page, rather than relying on a viewer’s presentation.

Each entry checks complete decoded pixels at the original size, resized frame order and geometry, delays, and looping. This does not establish equivalence for every animation operation.

Reviewed on 2026-10-26. Reference: govips v2.20.0 / libvips 8.17.3. Sharp 0.35.5 uses libvips 8.18.7; pyvips 3.2.0 and the CLI use libvips 8.17.3.

This example verifies GIF with three distinct frames and an exact 0.5 scale. It does not reproduce defects in older versions or cover AVIF, PDF, multipage TIFF, duplicate-frame merging, fractional rounding, or every rotation. It provides no multipage guarantee for Similarity, SmartCrop, or Gravity.