我将 golang 中的图像加载到 image.Image 格式,但出现此错误:
"image: unknown format"
这是我的功能:
import "image"
url := "https://i.imgur.com/m1UIjW1.jpg"
func loadImageFromURL(URL string) (image.Image, error) {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, errors.New("received non 200 response code")
}
img, _, err := image.Decode(response.Body)
if err != nil {
return nil, err
}
return img, nil
}
缺少的是这个导入:
import _ "image/jpeg"
守着星空守着你
相关分类