Categorygithub.com/snapas/imageorient
modulepackage
0.0.0-20210611154254-8051c9a710af
Repository: https://github.com/snapas/imageorient.git
Documentation: pkg.go.dev

# README

imageorient

GoDoc

Package imageorient provides image decoding functions similar to standard library's image.Decode and image.DecodeConfig with the addition that they also handle the EXIF orientation tag (if present).

License: MIT.

See also: http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/

Install / Update

go get -u github.com/snapas/imageorient

Documentation

https://godoc.org/github.com/snapas/imageorient

Usage example

package main

import (
	"image/jpeg"
	"log"
	"os"

	"github.com/snapas/imageorient"
)

func main() {
	// Open the test image. This particular image have the EXIF
	// orientation tag set to 3 (rotated by 180 deg).
	f, err := os.Open("testdata/orientation_3.jpg")
	if err != nil {
		log.Fatalf("os.Open failed: %v", err)
	}

	// Decode the test image using the imageorient.Decode function
	// to handle the image orientation correctly.
	img, _, err := imageorient.Decode(f)
	if err != nil {
		log.Fatalf("imageorient.Decode failed: %v", err)
	}

	// Save the decoded image to a new file. If we used image.Decode
	// instead of imageorient.Decode on the previous step, the saved
	// image would appear rotated.
	f, err = os.Create("testdata/example_output.jpg")
	if err != nil {
		log.Fatalf("os.Create failed: %v", err)
	}
	err = jpeg.Encode(f, img, nil)
	if err != nil {
		log.Fatalf("jpeg.Encode failed: %v", err)
	}
}

# Functions

Decode decodes an image and changes its orientation according to the EXIF orientation tag (if present).
DecodeConfig decodes the color model and dimensions of an image with the respect to the EXIF orientation tag (if present).
readOrientation reads the EXIF orientation tag from the given image.