tins2020/cmd/imadapt/crop.go

27 lines
635 B
Go
Raw Normal View History

2020-05-10 07:11:08 +00:00
package main
import (
"image"
"image/draw"
"github.com/nfnt/resize"
"opslag.de/schobers/tins2020/img"
2020-05-10 07:11:08 +00:00
)
func crop(path string, crop rect, size *point) error {
src, err := img.DecodeImage(path)
2020-05-10 07:11:08 +00:00
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 img.EncodePNG(path, dst)
2020-05-10 07:11:08 +00:00
}
resized := resize.Resize(uint(size.x), uint(size.y), dst, resize.Bilinear)
return img.EncodePNG(path, resized)
2020-05-10 07:11:08 +00:00
}