commit 45084fff7a40ba4e9cde8dbadf230e7e6f45db85
parent 6e2e5099c7001fcbfcc57940e9aae002384bb98e
Author: Vetle Haflan <vetle@haflan.dev>
Date: Mon, 13 Sep 2021 23:10:40 +0200
Merge branch 'master' of gl.haflan.dev:general/experiments
Diffstat:
4 files changed, 92 insertions(+), 0 deletions(-)
diff --git a/go.mod b/go.mod
@@ -0,0 +1,10 @@
+module gl.haflan.dev/general/experiments
+
+go 1.16
+
+require (
+ github.com/disintegration/imaging v1.6.2 // indirect
+ github.com/edwvee/exiffix v0.0.0-20190810152521-16aac9658f23
+ github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect
+ golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
+)
diff --git a/go.sum b/go.sum
@@ -0,0 +1,12 @@
+github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
+github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
+github.com/edwvee/exiffix v0.0.0-20190810152521-16aac9658f23 h1:cHT1oZQVzPQzq3Iz3CCISUA4X94kHdoOFJ/xcbwEtqs=
+github.com/edwvee/exiffix v0.0.0-20190810152521-16aac9658f23/go.mod h1:KoE3Ti1qbQXCb3s/XGj0yApHnbnNnn1bXTtB5Auq/Vc=
+github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
+github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
+golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
+golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
+golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
diff --git a/go/thumbnail/.gitignore b/go/thumbnail/.gitignore
@@ -0,0 +1,2 @@
+thumbnail
+*.jpg
diff --git a/go/thumbnail/main.go b/go/thumbnail/main.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "github.com/edwvee/exiffix"
+ "golang.org/x/image/draw"
+ "image"
+ "image/jpeg"
+ "os"
+ "time"
+)
+
+// scale image. Thanks: https://github.com/nfnt/resize/issues/63#issuecomment-540704731
+func scaleImg(src image.Image, rect image.Rectangle, scale draw.Scaler) image.Image {
+ dst := image.NewRGBA(rect)
+ scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
+ return dst
+}
+
+func thumbnail(src image.Image, maxW, maxH int, scale draw.Scaler) image.Image {
+ var (
+ scalar float64 = 1
+ w = float64(src.Bounds().Max.X)
+ h = float64(src.Bounds().Max.Y)
+ )
+ if w > float64(maxW) {
+ scalar = float64(maxW) / w
+ }
+ if scalar*h > float64(maxH) {
+ scalar = float64(maxH) / h
+ }
+ return scaleImg(src, image.Rect(0, 0, int(scalar*w), int(scalar*h)), draw.ApproxBiLinear)
+}
+
+func main() {
+ start := time.Now()
+ if len(os.Args) < 2 {
+ fmt.Println("No img file given")
+ os.Exit(1)
+ }
+ imgFile, err := os.Open(os.Args[1])
+ if err != nil {
+ fmt.Println("Error when reading:", err)
+ os.Exit(1)
+ }
+ defer imgFile.Close()
+ fmt.Println("Opened", time.Since(start))
+ start = time.Now()
+ img, _, err := exiffix.Decode(imgFile)
+ if err != nil {
+ fmt.Println("Error when decoding:", err)
+ os.Exit(1)
+ }
+ fmt.Println("Decoded", time.Since(start))
+ start = time.Now()
+ of, err := os.Create("output.jpg")
+ if err != nil {
+ fmt.Println("Error when opening output file:", err)
+ os.Exit(1)
+ }
+ newImg := thumbnail(img, 200, 200, draw.ApproxBiLinear)
+ fmt.Println("Processed", time.Since(start))
+ start = time.Now()
+ imageWriter := bufio.NewWriter(of)
+ err = jpeg.Encode(imageWriter, newImg, nil)
+ fmt.Println("Written", time.Since(start))
+}