Skip to content

Extract Images

huzpsb edited this page Oct 25, 2026 · 1 revision

Extract Images

Export all images embedded in a PDF — re-encoded to PNG where useful, with stable filenames.

Command line

pdfcpu2 extract -m image in.pdf outDir
pdfcpu2 extract -m image -p 1-3 in.pdf outDir     # selected pages only
cat in.pdf | pdfcpu2 extract -m image - outDir

Filenames follow <inBase>_<page>_<id>.<ext>, e.g. gallery_1_Im0.png. Soft masks and image masks are handled.

Go API

package main

import (
	"context"

	"github.com/huzpsb/pdfcpu2/pkg/api"
	"github.com/huzpsb/pdfcpu2/pkg/pdfcpu/model"
)

func main() {
	ctx := context.Background()
	conf := model.NewDefaultConfiguration()

	err := api.ExtractImagesFile(ctx, "in.pdf", "./images", nil, conf)
	if err != nil {
		panic(err)
	}
}

Custom sink — decide yourself what happens to each image:

err := api.ExtractImages(ctx, f, nil, func(img model.Image, singleImgPerPage bool, pageNr int) error {
	fmt.Printf("page %d: %dx%d %v\n", pageNr, img.Width, img.Height, img.Extension)
	return nil // or write, upload, re-encode...
}, conf)

Raw inventory without decoding: api.ExtractImagesRaw. See also Images (list + update).

Clone this wiki locally