Golang 如何将图像相互连接/附加

Go 有很棒的图像处理和数据库,但是我无法从较小的图像创建一个大图像。有谁知道如何在 Golang 中获取两个 png 或 jpeg 文件并将它们连接起来形成一个包含两个(或更多)文件的大图像?


我目前正在阅读这样的 png 文件:


imgFile, err := os.Open(path)

if err != nil {

    return Image{}, err

}

img, _, err := image.Decode(imgFile)

if err != nil {

    return Image{}, err

}


rgba := image.NewRGBA(img.Bounds())

if rgba.Stride != rgba.Rect.Size().X*4 {

    return Image{}, fmt.Errorf("unsupported stride")

}

draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)

我对如何获取此 png RGBA 数据并与其他 RGBA 数据连接和/或将其组合成“空”图像感到困惑。


千万里不及你
浏览 246回答 3
3回答

饮歌长啸

创建一个新的空图像 (NewRGBA),其边界足够大以容纳两个图像。然后使用该Draw方法在这个新的大图像的适当部分绘制每个图像。以下是代码步骤。加载两个图像。imgFile1, err := os.Open("test1.jpg")if err != nil {    fmt.Println(err)}imgFile2, err := os.Open("test2.jpg")if err != nil {    fmt.Println(err)}img1, _, err := image.Decode(imgFile1)if err != nil {    fmt.Println(err)}img2, _, err := image.Decode(imgFile2)if err != nil {    fmt.Println(err)}让我们在第一个图像的右侧绘制第二个图像。所以它的起点应该是第一个图像的宽度在(w, 0)哪里w。第一个图像的右下角将是第二个图像的左下角。//starting position of the second image (bottom left)sp2 := image.Point{img1.Bounds().Dx(), 0}它应该在一个足以容纳它的矩形中。//new rectangle for the second imager2 := image.Rectangle{sp2, sp2.Add(img2.Bounds().Size())}现在创建一个大矩形,其宽度足以容纳两个图像。//rectangle for the big imager := image.Rectangle{image.Point{0, 0}, r2.Max}注意此大图像将具有第二个图像的高度。如果第一张图像更高,它将被裁剪。创建一个新图像。rgba := image.NewRGBA(r)现在您可以将这两个图像绘制到这个新图像中draw.Draw(rgba, img1.Bounds(), img1, image.Point{0, 0}, draw.Src)draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)由于我们在r2第一张图片的右侧创建了它,因此第二张图片将被绘制到右侧。最后就可以导出了。out, err := os.Create("./output.jpg")if err != nil {    fmt.Println(err)}var opt jpeg.Optionsopt.Quality = 80jpeg.Encode(out, rgba, &opt)

jeck猫

如果你把一些东西变成函数并创建一个结构来理解每个像素,你的生活会容易得多。// Create a struct to deal with pixeltype Pixel struct {&nbsp; &nbsp; Point image.Point&nbsp; &nbsp; Color color.Color}// Keep it DRY so don't have to repeat opening file and decodefunc OpenAndDecode(filepath string) (image.Image, string, error) {&nbsp; &nbsp; imgFile, err := os.Open(filepath)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer imgFile.Close()&nbsp; &nbsp; img, format, err := image.Decode(imgFile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; return img, format, nil}// Decode image.Image's pixel data into []*Pixelfunc DecodePixelsFromImage(img image.Image, offsetX, offsetY int) []*Pixel {&nbsp; &nbsp; pixels := []*Pixel{}&nbsp; &nbsp; for y := 0; y <= img.Bounds().Max.Y; y++ {&nbsp; &nbsp; &nbsp; &nbsp; for x := 0; x <= img.Bounds().Max.X; x++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p := &Pixel{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point: image.Point{x + offsetX, y + offsetY},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color: img.At(x, y),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixels = append(pixels, p)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return pixels}

慕森卡

我正是为此目的建立了一个图书馆。您可以按如下方式使用它;import gim "github.com/ozankasikci/go-image-merge"grids := []*gim.Grid{&nbsp; &nbsp; {ImageFilePath: "test1.jpg"},&nbsp; &nbsp; {ImageFilePath: "test2.png"},}// merge the images into a 2x1 gridrgba, err := gim.New(grids, 2, 1).Merge()// save the output to jpg or pngfile, err := os.Create("file/path.jpg|png")err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 80})https://github.com/ozankasikci/go-image-merge
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go