-2

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
Ben R.
  • 7
  • 2
  • 4
    You are ignoring errors. Do not ignore them, and they should tell what's wrong. – Burak Serdar Sep 18 '20 at 19:19
  • I just changed this; it turns out image.Decode was returning an error (image: unknown format). All the posts I've seen about this say to simply import "image/png", but I already did. – Ben R. Sep 18 '20 at 19:51

2 Answers2

1

You are trying to read from the same io.reader(file) twice. The first time in

image.DecodeConfig(file)

and the second time in

image.Decode(file)

The second time you try to read from the same io.reader, you will get EOF

When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.

Read more about it here https://golang.org/pkg/io/#Reader

Something quick and simple you can do is to open the file twice

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
}

file2, err := os.Open("test2.png")
if err != nil {
    fmt.Println("3 ", err)
    return
}
imageIn, _, err := image.Decode(file2)
if err != nil {
    fmt.Println("4 ", err)
    return
}

Alternatively, you can try reading multiple times from the same file. How to read multiple times from same io.Reader

KitchenSpoon
  • 169
  • 5
0

You need to make this call between these lines image.DecodeConfig(file) and image.Decode(file):

file.Seek(0, io.SeekStart)
rigon
  • 1,310
  • 4
  • 15
  • 37