Skip to content

Instantly share code, notes, and snippets.

@rhaseven7h
Created May 24, 2017 18:38
Show Gist options
  • Save rhaseven7h/fd6fd178b80276f0fe16597797a5cc6b to your computer and use it in GitHub Desktop.
Save rhaseven7h/fd6fd178b80276f0fe16597797a5cc6b to your computer and use it in GitHub Desktop.
Crop and Resize a Directory with Images with Golang (Go)
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/disintegration/imaging"
)
func main() {
const inputPath = "public/product_images/"
const outputPath = "public/product_images/outputs"
files, err := filepath.Glob(inputPath + "/*.png")
if err != nil {
panic(err)
}
for _, file := range files {
fDir := filepath.Dir(file)
fBase := filepath.Base(file)
fOutput := filepath.Join(fDir, "outputs", fBase)
fmt.Printf("\033[1;37m=====PROCESSING=====\033[0m\nDir: %s\nBasename: %s\nFile: %s\nOutput: %s\n", fDir, fBase, file, fOutput)
// processing starts here
src, err := imaging.Open(file)
if err != nil {
log.Printf("\033[1;31mOpen failed: %v\033[0m\n", err)
continue
}
dst := imaging.Fill(src, 512, 512, imaging.Center, imaging.Lanczos)
err = imaging.Save(dst, fOutput)
if err != nil {
log.Printf("\033[1;31mSave failed: %v\033[0m\n", err)
continue
}
fmt.Printf("\033[1;32mSuccess!\033[0m\n")
// processing ends here
fmt.Println("\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment