Created
July 2, 2019 09:55
-
-
Save esimov/e3b011a08c7d3bfd29f897d43104aed1 to your computer and use it in GitHub Desktop.
Resize multiple images with Caire.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"math/rand" | |
"os" | |
"runtime" | |
"strconv" | |
"time" | |
"github.com/esimov/caire" | |
) | |
func main() { | |
// Below is an example of using our PrintMemUsage() function | |
// Print our starting memory usage (should be around 0mb) | |
PrintMemUsage() | |
files := []string{"waterfall.png", "sample-image.jpeg", "lighthouse.jpg", "landscape.jpg"} | |
for _, filename := range files { | |
f, err := os.OpenFile("../caire/input/"+filename, os.O_RDWR, 0666) | |
if err != nil { | |
log.Println("Read file error:", err.Error()) | |
log.Println(filename) | |
return | |
} | |
f_write, err := os.OpenFile("file_"+strconv.Itoa(rand.Int())+".jpg", os.O_RDWR|os.O_CREATE, 0666) | |
if err != nil { | |
log.Fatal("Read file error:", err.Error()) | |
} | |
p := &caire.Processor{ | |
NewWidth: 300, | |
Percentage: false, | |
Square: true, | |
} | |
err = p.Process(f, f_write) | |
if err != nil { | |
log.Println("Failed to apply seam carving to the image", err.Error()) | |
return | |
} | |
f.Close() | |
f_write.Close() | |
// Print our memory usage at each interval | |
PrintMemUsage() | |
runtime.GC() | |
PrintMemUsage() | |
fmt.Println("=============================") | |
time.Sleep(time.Second) | |
} | |
} | |
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number | |
// of garage collection cycles completed. | |
func PrintMemUsage() { | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
// For info on each, see: https://golang.org/pkg/runtime/#MemStats | |
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) | |
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) | |
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) | |
fmt.Printf("\tNumGC = %v\n", m.NumGC) | |
} | |
func bToMb(b uint64) uint64 { | |
return b / 1024 / 1024 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment