Golang如何串联/附加图像 [英] Golang how to concatenate/append images to one another

查看:90
本文介绍了Golang如何串联/附加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Go has great image manipulation and data libraries however I'm having trouble creating one big image from smaller ones. Does anyone know how to take two png or jpeg files in Golang and concatenate them to form one big image that encompasses the two (or more) files?

我目前正在读取png文件,如下所示:

i'm currently reading png files like so:

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数据连接和/或将其组合为空"图像感到困惑.

I'm confused on how to take this png RGBA data and concatenate with other RGBA data and/or combine that into an "empty" image.

推荐答案

创建一个新的空图像(NewRGBA),该图像的边界足以容纳两个图像.然后使用Draw方法在此新大图像的适当部分上绘制每个图像.

Create a new empty image (NewRGBA) that has bounds large enough to hold both images. Then use the Draw method to draw each image on appropriate parts of this new large image.

以下是带有代码的步骤.

Here are steps with code.

加载两个图像.

imgFile1, err := os.Open("test1.jpg")
imgFile2, err := os.Open("test2.jpg")
if err != nil {
    fmt.Println(err)
}
img1, _, err := image.Decode(imgFile1)
img2, _, err := image.Decode(imgFile2)
if err != nil {
    fmt.Println(err)
}

让我们在第一张图像的右边绘制第二张图像.因此,它的起点应该在(w, 0)处,其中w是第一张图像的宽度.第一张图片的右下角将是第二张图片的左下角.

Let's draw the second image to the right of the first image. So the starting point of it should be at (w, 0) where w is the width of the first image. The bottom right point of the first image will be the bottom left point of the second.

//starting position of the second image (bottom left)
sp2 := image.Point{img1.Bounds().Dx(), 0}

它应该是一个足够大的矩形以容纳它.

It should be in a rectangle large enough to hold it.

//new rectangle for the second image
r2 := image.Rectangle{sp2, sp2.Add(img2.Bounds().Size())}

现在创建一个大矩形,该矩形将足够容纳两个图像.

Now create a large rectangle that will be wide enough to hold both images.

//rectangle for the big image
r := image.Rectangle{image.Point{0, 0}, r2.Max}

注意此大图像将具有第二张图像的高度.如果第一张图像更高,则会被裁剪.

Note This large image will have the height of the second image. If the first image is higher it will be cropped.

创建新图像.

rgba := image.NewRGBA(r)

现在您可以将两个图像绘制到这个新图像中

Now you can draw the two images into this new image

draw.Draw(rgba, img1.Bounds(), img1, image.Point{0, 0}, draw.Src)
draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)

由于我们创建了r2,因此其位于第一张图像的右侧,因此第二张图像将被绘制在右侧.

Since we created r2 so its to the right of the first image, second image will be drawn to the right.

最后,您可以导出它.

out, err := os.Create("./output.jpg")
if err != nil {
    fmt.Println(err)
}

var opt jpeg.Options
opt.Quality = 80

jpeg.Encode(out, rgba, &opt)

这篇关于Golang如何串联/附加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆