Last active
August 29, 2015 14:05
-
-
Save hhatto/df7b64bcf2ece8512178 to your computer and use it in GitHub Desktop.
smartcrop.go cli
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" | |
"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