Last active
August 7, 2017 08:02
-
-
Save jakecoffman/df4d10f4ae223de5a5ac to your computer and use it in GitHub Desktop.
Resize all images in a directory (Golang)
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" | |
"image/jpeg" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
"github.com/nfnt/resize" | |
) | |
const PATH = "resized" | |
func main() { | |
os.Mkdir(PATH, 0666) | |
files, err := ioutil.ReadDir(".") | |
check(err) | |
for _, file := range files { | |
if !strings.HasSuffix(file.Name(), ".JPG") { | |
continue | |
} | |
fmt.Println("Processing", file.Name()) | |
file, err := os.Open(file.Name()) | |
check(err) | |
defer file.Close() | |
img, err := jpeg.Decode(file) | |
check(err) | |
newSize := float64(img.Bounds().Size().X) * 0.3 | |
m := resize.Resize(uint(newSize), 0, img, resize.Lanczos3) | |
out, err := os.Create(PATH + "/" + file.Name()) | |
check(err) | |
defer out.Close() | |
jpeg.Encode(out, m, nil) | |
} | |
} | |
func check(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment