【问题标题】:How to implement Custom cropping using golang如何使用 golang 实现自定义裁剪
【发布时间】:2022-01-16 21:13:03
【问题描述】:

我希望对一组图像进行自定义裁剪,而不是正常裁剪,使用高度和宽度我希望灵活地获取像多边形或六边形一样裁剪的输出图像,我正在使用库 @ 987654322@,以及内置模块“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或其他什么?
  • 没错,它是一个PNG文件,在像多边形一样裁剪后会有透明背景
  • 那么,你想要一个特定的形状?每个形状都必须以不同的方式计算
  • github.com/fogleman/gg 的文档说它的剪切功能适用于路径,并提到了一组创建此类路径的函数。所以,what have you tried?.
  • 添加了一个小解决方案,如果它不起作用,请告诉我:)

标签: image go image-processing image-manipulation imaging


【解决方案1】:

比预期的要长一点,但是在这里您可以将透明背景的 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")
}

【讨论】:

  • 我会对此进行测试并尽快回复您!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2020-12-15
  • 2011-08-14
  • 2019-10-31
  • 2017-06-14
相关资源
最近更新 更多