title: Converting RGBA image to Grayscale Golang
body: <p>I'm currently working on a program to convert and RGBA image to grayscale.</p>
  
  <p>I asked a question earlier and was directed to the following answer -
  <a href="https://stackoverflow.com/questions/36573413/change-color-of-a-single-pixel-go-lang-image">Change color of a single pixel - Go lang image</a></p>
  
  <p>Here is my original question - <a href="https://stackoverflow.com/questions/42513797/program-to-convert-rgba-to-grayscale-golang?noredirect=1#comment72166960_42513797">Program to convert RGBA to grayscale Golang</a></p>
  
  <p>I have edited my code so it now successfully runs - however the image outputted is not what I want. It is converted to grayscale however the pixels are all messed up making it look like noise on an old TV.</p>
  
  <pre><code>package main
  
  import (
  "image"
  "image/color"
  "image/jpeg"
  "log"
  "os"
  )
  
  type ImageSet interface {
  Set(x, y int, c color.Color)
  }
  
  
  func main() {
  file, err := os.Open("flower.jpg")
  if err != nil {
      log.Fatal(err)
  }
  defer file.Close()
  
  img, err := jpeg.Decode(file)
  if err != nil {
      log.Fatal(os.Stderr, "%s: %v\n", "flower.jpg", err)
  }
  
  b := img.Bounds()
  
  imgSet := image.NewRGBA(b)
  for y := 0; y &lt; b.Max.Y; y++ {
      for x := 0; x &lt; b.Max.X; x++ {
        oldPixel := img.At(x, y)
         r, g, b, a:= oldPixel.RGBA()
         r = (r+g+b)/3
         pixel := color.RGBA{uint8(r), uint8(r), uint8(r), uint8(a)}
         imgSet.Set(x, y, pixel)
       }
      }
  
      outFile, err := os.Create("changed.jpg")
      if err != nil {
        log.Fatal(err)
      }
      defer outFile.Close()
      jpeg.Encode(outFile, imgSet, nil)
  
  }
  </code></pre>
  
  <p>I know I haven't added in the <code>if else</code> statement for checking if the image can accept the <code>Set()</code> method, however the suggestion for simply making a new image seems to have solved this.</p>
  
  <p>Any help much appreciated.</p>
  
  <p>Edit:</p>
  
  <p>I've added in some suggested code from the answer below:</p>
  
  <pre><code>    package main
  
  import (
  //"fmt"
  "image"
  "image/color"
  "image/jpeg"
  "log"
  "os"
  )
  
  type ImageSet interface {
  Set(x, y int, c color.Color)
  }
  
  
  func main() {
  file, err := os.Open("flower.jpg")
  if err != nil {
      log.Fatal(err)
  }
  defer file.Close()
  
  img, err := jpeg.Decode(file)
  if err != nil {
      log.Fatal(os.Stderr, "%s: %v\n", "flower.jpg", err)
  }
  
  b := img.Bounds()
  imgSet := image.NewRGBA(b)
  for y := 0; y &lt; b.Max.Y; y++ {
      for x := 0; x &lt; b.Max.X; x++ {
        oldPixel := img.At(x, y)
        r, g, b, _ := oldPixel.RGBA()
        y := 0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)
        pixel := color.Gray{uint8(y / 256)}
        imgSet.Set(x, y, pixel)
       }
      }
  
      outFile, err := os.Create("changed.jpg")
      if err != nil {
        log.Fatal(err)
      }
      defer outFile.Close()
      jpeg.Encode(outFile, imgSet, nil)
  
  }
  </code></pre>
  
  <p>I currently get the following error.</p>
  
  <pre><code>.\rgbtogray.go:36: cannot use y (type uint32) as type int in argument to imgSet.Set
  </code></pre>
  
  <p>Am I missing something from the answer? Any tips appreciated.</p>