Skip to content

Instantly share code, notes, and snippets.

@hhatto
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/df7b64bcf2ece8512178 to your computer and use it in GitHub Desktop.
Save hhatto/df7b64bcf2ece8512178 to your computer and use it in GitHub Desktop.
smartcrop.go cli
package main
import (
"fmt"
"image"
"image/jpeg"
_ "image/png"
"log"
"os"
"github.com/muesli/smartcrop"
"code.google.com/p/graphics-go/graphics"
)
func main() {
fi, _ := os.Open(os.Args[1])
defer fi.Close()
img, _, err := image.Decode(fi)
if err != nil {
log.Fatal(err)
}
topCrop, err := smartcrop.SmartCrop(&img, 300, 300)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Top crop: %+v\n", topCrop)
cropImage := image.NewRGBA(image.Rect(topCrop.X, topCrop.Y, 300+topCrop.X, 300+topCrop.Y))
//cropImage := image.NewRGBA(image.Rect(topCrop.X, topCrop.Y, topCrop.Width+topCrop.X, topCrop.Height+topCrop.Y))
graphics.Thumbnail(cropImage, img)
toImage, err := os.Create(os.Args[2])
if err != nil {
log.Fatal(err)
}
defer toImage.Close()
jpeg.Encode(toImage, cropImage, &jpeg.Options{jpeg.DefaultQuality})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment