package main
import (
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
)
func main(){
if err := Cut("./img/111.jpg","./img/111_part.jpg",500,500,1930,1080);err != nil{
panic(err)
}
}
func Cut(source,target string,x1,y1,x2,y2 int)error{
img,tt,err:= loadImage(source)
if err != nil {
return err
}
fmt.Println(tt)
if !rectIsInRect(x1,y1,x2,y2,img.Bounds()){
return fmt.Errorf("rectangele not in target image")
}
// 图片文件解码
rgbImg := img.(*image.YCbCr)
subImg := rgbImg.SubImage(image.Rect(x1, y1, x2, y2)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
return saveImage(target,subImg,100,tt)
}
func rectIsInRect(x1,y1,x2,y2 int,ret image.Rectangle)bool{
var p = image.Pt(x1,y1)
var p1 = image.Pt(x2,y2)
return pointInRect(p,ret) && pointInRect(p1,ret)
}
//判断点是否在图片像素的矩形框内
func pointInRect(p image.Point,ret image.Rectangle)bool{
return ret.Min.X <= p.X
GO-图片裁剪
最新推荐文章于 2024-04-19 08:45:45 发布