title: Merge multiple images in Go
body: <p>I am trying to merge an array of N images (where N will be a dynamic value) with Go, but I always get a black image as result.</p>
  
  <p>Here you are my code:</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "image"
      "image/draw"
      "image/jpeg"
      "image/png"
      "log"
      "os"
  )
  
  func openAndDecode(imgPath string) image.Image {
      img, err := os.Open(imgPath)
      if err != nil {
          log.Fatalf("Failed to open %s", err)
      }
  
      decoded, err := png.Decode(img)
      if err != nil {
          log.Fatalf("Failed to decode %s", err)
      }
      defer img.Close()
  
      return decoded
  }
  
  func main () {
      var images = [4]string{"background", "shadow", "item1 ", "item2"}
      var decodedImages = [4]*image.RGBA{}
  
      for i, img := range images {
          decodedImage := openAndDecode("./imgs/" + img + ".png")
          bounds := decodedImage.Bounds()
          newImg := image.NewRGBA(bounds)
          decodedImages[i] = newImg
      }
  
      bounds := decodedImages[0].Bounds()
      newImage := image.NewRGBA(bounds)
  
      for _, img := range decodedImages {
          draw.Draw(newImage, img.Bounds(), img, image.ZP, draw.Src)
      }
  
      result, err := os.Create("result.jpg")
      if err != nil {
          log.Fatalf("Failed to create: %s", err)
      }
  
      jpeg.Encode(result, newImage, &amp;jpeg.Options{jpeg.DefaultQuality})
      defer result.Close()
  }
  </code></pre>
  
  <p>I am pretty new to Go and I can't find out where I am wrong.</p>