photomator/util.go

64 lines
1.2 KiB
Go
Raw Normal View History

2024-06-10 22:19:30 +02:00
package main
import (
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"strings"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/tiff"
"golang.org/x/image/bmp"
"golang.org/x/image/webp"
)
func CheckFilePath(name string) string {
if _, err := os.Stat(name); err == nil {
return name
}
panic(fmt.Errorf("%v does not exist in any directory which contains in $GOPATH", name))
}
func ReadIMG(name string) (img image.Image, orientation *tiff.Tag, err error) {
file, err := os.Open(CheckFilePath(name))
if err != nil {
return nil, nil, err
}
x, err := exif.Decode(file)
if err != nil {
return nil, nil, err
}
orientation, err = x.Get(exif.Orientation)
if err != nil {
return nil, nil, err
}
file, err = os.Open(CheckFilePath(name))
if err != nil {
return nil, nil, err
}
parts := strings.Split(name, ".")
switch strings.ToLower(parts[len(parts)-1]) {
case "jpg":
fallthrough
case "jpeg":
img, err = jpeg.Decode(file)
case "png":
img, err = png.Decode(file)
case "bmp":
img, err = bmp.Decode(file)
case "webp":
img, err = webp.Decode(file)
}
if err != nil {
return nil, nil, err
}
return img, orientation, nil
}