It seems that image.Decode (line 24) is returning an error when decoding the image. The image "test2.png" exists in the directory of the file, and is a function PNG image. For reference, this code is supposed to create a new image of the same resolution filled with one color randomly selected from test2.png. Any help here would be appreciated.
The code:
package main
import (
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"math/rand"
"os"
)
func main() {
file, err := os.Open("test2.png")
if err != nil {
fmt.Println("0 ", err)
return
}
imageInConfig, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Println("1 ", err)
return
}
imageIn, fileType, err := image.Decode(file)
fmt.Println("File type: ", fileType)
if err != nil {
fmt.Println("2 ", err)
return
}
outputWidth := imageInConfig.Width
outputHeight := imageInConfig.Height
imageOut := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{outputWidth, outputHeight}})
r, g, b, a := imageIn.At(int(rand.Intn(imageInConfig.Width)), int(rand.Intn(imageInConfig.Height))).RGBA()
pixelColor := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
for x := 0; x < outputWidth; x++ {
for y := 0; y < outputHeight; y++ {
imageOut.Set(x, y, pixelColor)
}
}
fileCreated, _ := os.Create("test2out.png")
png.Encode(fileCreated, imageOut)
}
The error outputted is:
image: unknown format