golang read many images into one simegle image

// Create a struct to deal with pixel
type Pixel struct {
    Point image.Point
    Color color.Color
}

// Keep it DRY so don't have to repeat opening file and decode
func OpenAndDecode(filepath string) (image.Image, string, error) {
    imgFile, err := os.Open(filepath)
    if err != nil {
        panic(err)
    }
    defer imgFile.Close()
    img, format, err := image.Decode(imgFile)
    if err != nil {
        panic(err)
    }
    return img, format, nil
}

// Decode image.Image's pixel data into []*Pixel
func DecodePixelsFromImage(img image.Image, offsetX, offsetY int) []*Pixel {
    pixels := []*Pixel{}
    for y := 0; y <= img.Bounds().Max.Y; y++ {
        for x := 0; x <= img.Bounds().Max.X; x++ {
            p := &Pixel{
                Point: image.Point{x + offsetX, y + offsetY},
                Color: img.At(x, y),
            }
            pixels = append(pixels, p)
        }
    }
    return pixels
}

func main() {
    img1, _, err := OpenAndDecode("makey.png")
    if err != nil {
        panic(err)
    }
    img2, _, err := OpenAndDecode("sample.jpg")
    if err != nil {
        panic(err)
    }
    // collect pixel data from each image
    pixels1 := DecodePixelsFromImage(img1, 0, 0)
    // the second image has a Y-offset of img1's max Y (appended at bottom)
    pixels2 := DecodePixelsFromImage(img2, 0, img1.Bounds().Max.Y)
    pixelSum := append(pixels1, pixels2...)

    // Set a new size for the new image equal to the max width
    // of bigger image and max height of two images combined
    newRect := image.Rectangle{
        Min: img1.Bounds().Min,
        Max: image.Point{
            X: img2.Bounds().Max.X,
            Y: img2.Bounds().Max.Y + img1.Bounds().Max.Y,
        },
    }
    finImage := image.NewRGBA(newRect)
    // This is the cool part, all you have to do is loop through
    // each Pixel and set the image's color on the go
    for _, px := range pixelSum {
            finImage.Set(
                px.Point.X,
                px.Point.Y,
                px.Color,
            )
    }
    draw.Draw(finImage, finImage.Bounds(), finImage, image.Point{0, 0}, draw.Src)

    // Create a new file and write to it
    out, err := os.Create("./output.png")
    if err != nil {
        panic(err)
        os.Exit(1)
    }
    err = png.Encode(out, finImage)
    if err != nil {
        panic(err)
        os.Exit(1)
    }
}

Are there any code examples left?
New code examples in category Go
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source