/
testImageFunctions.go
93 lines (68 loc) · 1.63 KB
/
testImageFunctions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"path/filepath"
"os"
"log"
"github.com/rwcarlsen/goexif/exif"
)
func main() {
output, err := ConvertToJpeg("test.png")
if err == nil {
extension := filepath.Ext(output)
name := output[0:len(output) - len(extension)]
newName := fmt.Sprintf("%v.jpg", name)
os.Rename(output, newName)
fmt.Println(newName)
FixOrientation(newName)
} else {
log.Fatal(err)
}
}
func FixOrientation(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
x, err := exif.Decode(f)
if err != nil {
log.Fatal("error decoding image")
log.Fatal(err)
}
camModel, _ := x.Get(exif.Model) // normally, don't ignore errors!
fmt.Println(camModel.StringVal())
return "", nil
}
func ConvertToJpeg(filename string) (string, error) {
pngImageFile, err := os.Open(filename)
if err != nil {
return "", err
}
defer pngImageFile.Close()
pngSource, err := png.Decode(pngImageFile)
if err != nil {
return "", err
}
jpegImage := image.NewRGBA(pngSource.Bounds())
draw.Draw(jpegImage, jpegImage.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
draw.Draw(jpegImage, jpegImage.Bounds(), pngSource, pngSource.Bounds().Min, draw.Over)
outfile := fmt.Sprintf("%s_jpg", filename)
jpegImageFile, err := os.Create(outfile)
if err != nil {
return "", err
}
defer jpegImageFile.Close()
var options jpeg.Options
options.Quality = 100
err = jpeg.Encode(jpegImageFile, jpegImage, &options)
if err != nil {
fmt.Printf("JPEG Encoding Error: %v\n", err)
os.Exit(1)
}
return outfile, nil
}