Created
October 30, 2019 15:55
-
-
Save aeter/62648b7717eebff11a4113307fca6599 to your computer and use it in GitHub Desktop.
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 ( | |
"os" | |
"path/filepath" | |
"runtime" | |
"strings" | |
"sync" | |
"github.com/disintegration/imaging" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
dir := parse_args() | |
// use all CPU cores for maximum performance | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
filepath.Walk(dir, visit) | |
wg.Wait() | |
} | |
func visit(p string, info os.FileInfo, err error) error { | |
if err != nil { | |
panic(err) | |
} | |
if strings.HasSuffix(p, ".jpg") { | |
wg.Add(1) | |
go make_thumbnail(p) | |
} | |
return nil | |
} | |
func make_thumbnail(fname string) { | |
defer wg.Done() | |
img, err := imaging.Open(fname) | |
if err != nil { | |
panic(err) | |
} | |
dstImage128 := imaging.Thumbnail(img, 120, 120, imaging.Lanczos) | |
err = imaging.Save(dstImage128, fname+"_thumb.jpg") | |
if err != nil { | |
panic(err) | |
} | |
} | |
func parse_args() string { | |
dir := "." | |
if len(os.Args) == 2 { | |
dir = os.Args[1] | |
} | |
return dir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment