登录
首页 >  Golang >  Go问答

使用golang编写自定义的图像裁剪函数

来源:stackoverflow

时间:2024-03-04 18:36:24 258浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《使用golang编写自定义的图像裁剪函数》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我希望对一组图像进行自定义裁剪,而不是使用高度和宽度进行正常裁剪,我希望能够灵活地获取像多边形或六边形一样裁剪的输出图像,例如,我使用库 github .com/fogleman/gg,以及内置模块“image”和github.com/disintegration/imaging,但我没有找到自定义裁剪的方法,我还寻找了一个在线saas来做到这一点,例如imgix 或 imageresizer.io,但他们似乎没有提供这一点,我知道 golang 是正确的语言,也许我看起来不够努力,请帮助

我的示例代码如下所示:

var image image.Image
dc := NewContext(1000, 1000)
image = imaging.Fill(profile, 800, 750, imaging.Center, imaging.Lanczos)
// Cropping needs to happen here
dc.DrawImage(image, 123, 250)

正确答案


比预期的要长一些,但是这里有将带有透明背景的 png 图像裁剪为矩形。您可以通过更改 getpixalpha 函数来修改不同形状的代码。

只需添加包名称,它应该包含导入,然后添加图像 test.png 并且它应该创建一个 test-output.png

注意:您可能需要进行一些小的修改才能将其用作服务。

type Pixel struct {
    R int
    G int
    B int
    A int
}

func LogPanic(err error, msg string) {
    if err != nil {
        log.Printf("ERROR: %v %s", err, msg)
        panic(err)
    }
}

func getPixAlpha(x, y, halfWidth int) int {
    if x < halfWidth-y || x > halfWidth+y {
        return 0
    }
    if y > halfWidth+x {
        return 0
    }
    if x > halfWidth*3-y && y > halfWidth*3-x {
        return 0
    }
    return int(255)
}

func getPixels(file io.Reader) ([][]Pixel, error) {
    img, _, err := image.Decode(file)
    LogPanic(err, "error reading image")
    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y
    var pixels [][]Pixel
    for x := 0; x < width; x++ {
        var row []Pixel
        for y := 0; y < height; y++ {
            row = append(row, rgbaToPixel(img.At(x, y).RGBA()))
        }
        pixels = append(pixels, row)
    }
    return pixels, nil
}

func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) Pixel {
    return Pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)}
}

func getRgbaPic(pixels [][]Pixel) [][]Pixel {
    dx := len(pixels)
    dy := len(pixels[0])
    for x := 0; x < dx; x++ {
        for y := 0; y < dy; y++ {
            pixels[x][y].A = getPixAlpha(x, y, len(pixels)/2)
        }
    }
    return pixels
}

func main() {
    file, err := os.Open("./test.png")
    LogPanic(err, "Error opening file")
    defer file.Close()
    pixels, err := getPixels(file)
    LogPanic(err, "Error reading image")
    pixels = getRgbaPic(pixels)
    img := image.NewRGBA(image.Rect(0, 0, len(pixels), len(pixels[0])))
    for x := 0; x < len(pixels); x++ {
        for y := 0; y < len(pixels[0]); y++ {
            img.Set(x, y, color.RGBA{
                uint8(pixels[x][y].R),
                uint8(pixels[x][y].G),
                uint8(pixels[x][y].B),
                uint8(pixels[x][y].A),
            })
        }
    }
    buf := &bytes.Buffer{}
    err = png.Encode(buf, img)
    LogPanic(err, "Error encoding")
    err = ioutil.WriteFile("test-output.png", buf.Bytes(), 0666)
    LogPanic(err, "Error writing file")
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>