tins2020/cmd/imadapt/crop.go

26 lines
588 B
Go

package main
import (
"image"
"image/draw"
"github.com/nfnt/resize"
)
func crop(path string, crop rect, size *point) error {
src, err := decodeImage(path)
if err != nil {
return err
}
srcRect := image.Rect(crop.min.x, crop.min.y, crop.max.x, crop.max.y)
dstRect := image.Rect(0, 0, crop.max.x-crop.min.x, crop.max.y-crop.min.y)
dst := image.NewRGBA(dstRect)
draw.Draw(dst, dstRect, src, srcRect.Min, draw.Src)
if size == nil {
return encodePNG(path, dst)
}
resized := resize.Resize(uint(size.x), uint(size.y), dst, resize.Bilinear)
return encodePNG(path, resized)
}