autometa

package
v0.35.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package autometa provides support for embedded metadata and automatic detection of image formats.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(r io.Reader) (md *meta.Data, imgStream io.Reader, err error)

Load loads the metadata for an image stream, which may be one of the supported image formats.

Only as much of the stream is consumed as necessary to extract the metadata; the returned stream contains a buffered copy of the consumed data such that reading from it will produce the same results as fully reading the input stream. This provides a convenient way to load the full image after loading the metadata.

An error is returned if basic metadata could not be extracted. The returned stream still provides the full image data.

Example (BasicJPEGMetadata)
package main

import (
	"fmt"
	"image"
	"image/jpeg"
	"os"

	"github.com/mandykoh/prism/meta"
	"github.com/mandykoh/prism/meta/autometa"
)

func printMetadata(md *meta.Data, img image.Image) {
	fmt.Printf("Format: %s\n", md.Format)
	fmt.Printf("BitsPerComponent: %d\n", md.BitsPerComponent)
	fmt.Printf("PixelHeight: %d\n", md.PixelHeight)
	fmt.Printf("PixelWidth: %d\n", md.PixelWidth)

	fmt.Printf("Actual image height: %d\n", img.Bounds().Dy())
	fmt.Printf("Actual image width: %d\n", img.Bounds().Dx())
}

func main() {
	inFile, err := os.Open("../../test-images/pizza-rgb8-srgb.jpg")
	if err != nil {
		panic(err)
	}
	defer inFile.Close()

	md, imgStream, err := autometa.Load(inFile)
	if err != nil {
		panic(err)
	}

	img, err := jpeg.Decode(imgStream)
	if err != nil {
		panic(err)
	}

	printMetadata(md, img)

}
Output:

Format: JPEG
BitsPerComponent: 8
PixelHeight: 1200
PixelWidth: 1200
Actual image height: 1200
Actual image width: 1200
Example (BasicPNGMetadata)
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"github.com/mandykoh/prism/meta"
	"github.com/mandykoh/prism/meta/autometa"
)

func printMetadata(md *meta.Data, img image.Image) {
	fmt.Printf("Format: %s\n", md.Format)
	fmt.Printf("BitsPerComponent: %d\n", md.BitsPerComponent)
	fmt.Printf("PixelHeight: %d\n", md.PixelHeight)
	fmt.Printf("PixelWidth: %d\n", md.PixelWidth)

	fmt.Printf("Actual image height: %d\n", img.Bounds().Dy())
	fmt.Printf("Actual image width: %d\n", img.Bounds().Dx())
}

func main() {
	inFile, err := os.Open("../../test-images/pizza-rgb8-srgb.png")
	if err != nil {
		panic(err)
	}
	defer inFile.Close()

	md, imgStream, err := autometa.Load(inFile)
	if err != nil {
		panic(err)
	}

	img, err := png.Decode(imgStream)
	if err != nil {
		panic(err)
	}

	printMetadata(md, img)

}
Output:

Format: PNG
BitsPerComponent: 8
PixelHeight: 1200
PixelWidth: 1200
Actual image height: 1200
Actual image width: 1200
Example (BasicWebPMetadata)
package main

import (
	"fmt"
	"image"
	"os"

	"github.com/mandykoh/prism/meta"
	"github.com/mandykoh/prism/meta/autometa"
	"golang.org/x/image/webp"
)

func printMetadata(md *meta.Data, img image.Image) {
	fmt.Printf("Format: %s\n", md.Format)
	fmt.Printf("BitsPerComponent: %d\n", md.BitsPerComponent)
	fmt.Printf("PixelHeight: %d\n", md.PixelHeight)
	fmt.Printf("PixelWidth: %d\n", md.PixelWidth)

	fmt.Printf("Actual image height: %d\n", img.Bounds().Dy())
	fmt.Printf("Actual image width: %d\n", img.Bounds().Dx())
}

func main() {
	inFile, err := os.Open("../../test-images/pizza-rgb8-displayp3-vp8x.webp")
	if err != nil {
		panic(err)
	}
	defer inFile.Close()

	md, imgStream, err := autometa.Load(inFile)
	if err != nil {
		panic(err)
	}

	img, err := webp.Decode(imgStream)
	if err != nil {
		panic(err)
	}

	printMetadata(md, img)

}
Output:

Format: WebP
BitsPerComponent: 8
PixelHeight: 1200
PixelWidth: 1200
Actual image height: 1200
Actual image width: 1200

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL