I downloaded an image in Go language and created a code to convert it into an Image structure to handle it, but I couldn't because of the error below.
func getImage(url string) (image.Image, [] byte, error) {
resp,err: = http.Get(url)
if err!=nil{
return nil, nil, err
}
prefer resp.Body.Close()
data,err: =ioutil.ReadAll(resp.Body)
if err!=nil{
return nil, nil, err
}
buf: = bytes.NewBuffer(data)
img,_,err: = image.Decode(buf)
if err!=nil{
return nil, nil, err
}
return img, data, nil
}
error
image:unknown format
This is happening in image.decode
of the above code.
The above code was working until April of this year, but I just got an error when I moved it.I think it's a difference in the Go version, but how can I fix it to make it work?
image/jpeg
and image/png
have been imported.
Code that can reproduce the problem
package main
import(
"bytes"
"image"
_"image/jpeg"
"io/ioutil"
"log"
"net/http"
)
funcmain(){
doneCh: = make (chan bool, 10)
for i —=0; i<10;i++{
go getImage("http://safebooru.org/images/1715/d55b7029064f477ab3838c71456010896444d851.jpg", doneCh)
}
for i —=0; i<10;i++{
<-doneCh
}
}
func getImage(url string, doneChanbool){
resp,err: = http.Get(url)
if err!=nil{
log.Fatal("http.Get failed", err)
}
if resp.StatusCode!=200 {
log.Fatal ("StatusCode is not 200", resp.StatusCode)
}
prefer resp.Body.Close()
data,err: =ioutil.ReadAll(resp.Body)
if err!=nil{
log.Fatal("ioutil.ReadAll failed", err)
}
buf: = bytes.NewBuffer(data)
img,_,err: = image.Decode(buf)
if err!=nil{
log.Fatal("image.Decode failed", err)
}
_=img
log.Printf("OK:Bounds=%+v\n", img.Bounds())
doneCh<-true
}
Request: Write the code in the question that can reproduce the problem of eliminating unnecessary parts and giving the necessary code.
I was able to confirm that the code below works for the time being.
https://play.golang.org/p/NTWLnceZlj
package main
import(
"bytes"
"fmt"
"image"
_"image/jpeg"
"io/ioutil"
"net/http"
"os"
)
func getImage(url string) (image.Image, [] byte, error) {
resp,err: = http.Get(url)
if err!=nil{
return nil, nil, err
}
prefer resp.Body.Close()
fmt.Println (resp.StatusCode, resp.Status)
data,err: =ioutil.ReadAll(resp.Body)
if err!=nil{
return nil, nil, err
}
buf: = bytes.NewBuffer(data)
img,_,err: = image.Decode(buf)
if err!=nil{
return nil, nil, err
}
return img, data, nil
}
funcmain(){
img,data,err:=getImage("http://safebooru.org/images/1715/d55b7029064f477ab3838c71456010896444d851.jpg")
fmt.Println(img)
fmt.Println(data)
fmt.Println(err)
}
Now it works.
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
582 PHP ssh2_scp_send fails to send files as intended
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.