如何将多个图像合并为一个图像?

I have converted pdf into a jpeg using go-fitz which gives me a list of jpeg in a directory. I am reading a list of images from a directory and then trying to combine them into a single image. The image is of type jpeg and works partially if I just use page 1 and 2 using the index. I want to stitch the pdf page jpeg images back into a single jpeg image. The end result of the code generate a single image with the first page.

Golang how to concatenate/append images to one another

If I use image index 0 and 1 this code work. But not working in dynamic list with my code. But I will have a dynamic list of images that I need to put together into a single image. I am assuming it has something to do with the final image canvas size and adding it to the canvas. The code ends up having first and last page in a wide canvas and missing rest of the pages when I attempt the stack over flow example.

package converter

import (
    "fmt"
    "image"
    "image/draw"
    "image/jpeg"
    "os"
    "path/filepath"
    "strings"

    log "github.com/sirupsen/logrus"
)

func openAndDecode(imgPath string) image.Image {
    img, err := os.Open(imgPath)
    if err != nil {
        log.Fatalf("Failed to open %s", err)
    }

    decoded, _, err := image.Decode(img)
    if err != nil {
        log.Fatalf("Failed to decode %s", err)
    }
    defer img.Close()

    return decoded
}

// StichImages takes a directory of images and combine them into a single image
func StichImages(dirPath string) {
    fileList := []string{}
    decodedImages := []image.Image{}
    err := filepath.Walk(dirPath, func(path string, f os.FileInfo, err error) error {
        fileList = append(fileList, path)
        return nil
    })

    if err != nil {
        log.Fatal(err)
    }

    // If there is only one image in folder no need to stich
    if len(fileList) == 1 {
        return
    }

    for _, filePath := range fileList {
        if strings.Contains(filePath, ".jpg") {
            decodedImage := openAndDecode(filePath)
            decodedImages = append(decodedImages, decodedImage)
        }
    }

    outPutPath := filepath.Join(dirPath, "output.jpg")

    if len(decodedImages) == 0 {
        log.Error(fmt.Sprintf("No images found in: %s", dirPath))
    }

    //starting position of the second image (bottom left)
    startingPoint := image.Point{}
    finalImageCanvas := image.Rectangle{image.Point{0, 0}, decodedImages[0].Bounds().Max}
    rgba := image.NewRGBA(finalImageCanvas)

    for index, newImage := range decodedImages {
        if index == 0 {
            startingPoint = image.Point{newImage.Bounds().Dx(), 0}
            draw.Draw(rgba, newImage.Bounds(), newImage, image.Point{0, 0}, draw.Src)
        } else {
            newImageRect := image.Rectangle{startingPoint, startingPoint.Add(newImage.Bounds().Size())}
            finalImageCanvas = image.Rectangle{image.Point{0, 0}, newImageRect.Max}
            draw.Draw(rgba, newImageRect, newImage, image.Point{0, 0}, draw.Src)
            startingPoint = image.Point{newImageRect.Bounds().Dx(), 0}
        }
    }

    out, err := os.Create(outPutPath)
    if err != nil {
        fmt.Println(err)
    }

    var opt jpeg.Options
    opt.Quality = 80

    jpeg.Encode(out, rgba, &opt)
}

doulao2916
doulao2916 我只看到一个包含此代码的页面。他确切的第一页。
一年多之前 回复
dtcd27183
dtcd27183 您描述了两次“不起作用”的尝试-“不起作用”是什么意思?发生了什么?具体是什么问题?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐