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 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘