Created
May 24, 2017 18:38
-
-
Save rhaseven7h/fd6fd178b80276f0fe16597797a5cc6b to your computer and use it in GitHub Desktop.
Crop and Resize a Directory with Images with Golang (Go)
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" | |
"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