0

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

rak1n
  • 671
  • 1
  • 8
  • 17
  • 1
    You described two attempts that "don't work" - what does "not work" mean? What happened? What specifically was the issue(s)? – Adrian Apr 05 '19 at 19:57
  • I only see a single page with this code. The first page to he exact. – rak1n Apr 05 '19 at 20:09
  • https://stackoverflow.com/a/71195493/11185460 see my answer to this question, it can concat multiple images into a single image. – Sailist Feb 21 '22 at 06:19

0 Answers0