Skip to content

Instantly share code, notes, and snippets.

@mariomartinezsz
Forked from clsung/gist:6822574
Last active April 20, 2017 00:24
Show Gist options
  • Save mariomartinezsz/7405775 to your computer and use it in GitHub Desktop.
Save mariomartinezsz/7405775 to your computer and use it in GitHub Desktop.
"A simple crop image to 80x80 jpeg file script" with GoLang
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})
}
@jirfag
Copy link

jirfag commented Jan 30, 2017

Thanks!
And it would be better to check result of graphics.Thumbnail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment