duankuixi1930 2019-04-05 19:35
浏览 239

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

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)
}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog