-
-
Notifications
You must be signed in to change notification settings - Fork 114
Open
Description
Thank you for maintaining this wonderful port from js lib
However I found the result sometimes is not the same as the original one. Not only the part of the image, but also the size of image is 512x512, even I use 300x300 in code.
package main
import (
"fmt"
"image"
"image/jpeg"
"math"
"os"
"github.com/muesli/smartcrop"
"github.com/muesli/smartcrop/nfnt"
)
func main() {
f, _ := os.Open("/tmp/origin.jpeg")
img, _, _ := image.Decode(f)
analyzer := smartcrop.NewAnalyzer(nfnt.NewDefaultResizer())
topCrop, _ := analyzer.FindBestCrop(img, 300, 300)
// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
fmt.Printf("Top crop: %+v\n", topCrop)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
croppedimg := img.(SubImager).SubImage(topCrop)
//save cropped image to file out.png
// Save cropped image to file
out, err := os.Create("/tmp/cropped.jpeg")
if err != nil {
fmt.Println("Failed to create the file:", err)
return
}
defer out.Close()
// Choose quality of the jpeg (1-100), higher is better quality
err = jpeg.Encode(out, croppedimg, &jpeg.Options{Quality: 90})
if err != nil {
fmt.Println("Failed to save the image:", err)
return
}
fmt.Println("Cropped image saved to /tmp/cropped.jpeg")
}Metadata
Metadata
Assignees
Labels
No labels


