Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang如何从矩形jpeg中裁剪出圆形图像。_Go_Jpeg - Fatal编程技术网

Golang如何从矩形jpeg中裁剪出圆形图像。

Golang如何从矩形jpeg中裁剪出圆形图像。,go,jpeg,Go,Jpeg,在Golang中,如何从矩形jpeg中裁剪出圆形图像。矩形的大小可以不同。如果你有一张图片,你会从图片的中心剪出一个圆圈,这个圆圈会占据尽可能多的空间吗?我想保留圆圈,去掉其余部分。这个例子使用了golang博客上的绘图软件包,应该大致满足您的要求 type circle struct { p image.Point r int } func (c *circle) ColorModel() color.Model { return color.AlphaModel }

在Golang中,如何从矩形jpeg中裁剪出圆形图像。矩形的大小可以不同。如果你有一张图片,你会从图片的中心剪出一个圆圈,这个圆圈会占据尽可能多的空间吗?我想保留圆圈,去掉其余部分。

这个例子使用了golang博客上的绘图软件包,应该大致满足您的要求

type circle struct {
    p image.Point
    r int
}

func (c *circle) ColorModel() color.Model {
    return color.AlphaModel
}

func (c *circle) Bounds() image.Rectangle {
    return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
}

func (c *circle) At(x, y int) color.Color {
    xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
    if xx*xx+yy*yy < rr*rr {
        return color.Alpha{255}
    }
    return color.Alpha{0}
}

    draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over)
类型圆形结构{
p图像点
r int
}
func(c*圆)ColorModel()color.Model{
返回颜色.AlphaModel
}
func(c*circle)Bounds()image.Rectangle{
返回image.Rect(c.p.X-c.r,c.p.Y-c.r,c.p.X+c.r,c.p.Y+c.r)
}
(x,y int)处的func(c*圆)颜色。颜色{
xx,yy,rr:=float64(x-c.p.x)+0.5,float64(y-c.p.y)+0.5,float64(c.r)
如果xx*xx+yy*yy
请注意,它采用矩形并遮罩所有内容,但以半径
r
从点
p
开始的圆除外。全文可以在这里找到

在您的情况下,您希望遮罩只是您的正常背景,src是您希望使用的当前矩形图像的一部分。

基于您的想法:更好: