Skip to content

Instantly share code, notes, and snippets.

@clsung
Created October 4, 2013 08:10
Show Gist options
  • Save clsung/6822574 to your computer and use it in GitHub Desktop.
Save clsung/6822574 to your computer and use it in GitHub Desktop.
A simple crop image to 80x80 jpeg file script
package main
import (
"fmt"
"os"
"log"
"flag"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"code.google.com/p/graphics-go/graphics"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Input file is missing.");
os.Exit(1);
}
if len(args) < 2 {
fmt.Println("Output file is missing.");
os.Exit(1);
}
fmt.Printf("opening %s\n", args[0])
fSrc, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer fSrc.Close()
src, _, err := image.Decode(fSrc)
if err != nil {
log.Fatal(err)
}
dst := image.NewRGBA(image.Rect(0, 0, 80, 80))
graphics.Thumbnail(dst, src)
toimg, err := os.Create(args[1])
if err != nil {
log.Fatal(err)
}
defer toimg.Close()
jpeg.Encode(toimg, dst, &jpeg.Options{jpeg.DefaultQuality})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment