亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從url存儲圖像時如何編寫RIFF塊頭?

從url存儲圖像時如何編寫RIFF塊頭?

Go
當年話下 2023-06-12 17:22:25
我只是嘗試從 url 下載 webp 圖像,但是當我嘗試處理存儲的圖像時,我發現了一些不同的東西。如果我從瀏覽器下載圖像,它可以使用x/image/webp包解碼,但如果我使用存儲圖像http.Get()然后創建一個新文件然后io.Copy()圖像,它說:“缺少 RIFF 塊頭”我假設在使用 golang 代碼存儲它時需要編寫一些 RIFF 塊標頭。func main(){? ? response, e := http.Get(URL)? ? if e != nil {? ? ? ? log.Fatal(e)? ? }? ? defer response.Body.Close()? ? //open a file for writing? ? file, err := os.Create('tv.webp')? ? if err != nil {? ? ? ? log.Fatal(err)? ? }? ? defer file.Close()? ? // Use io.Copy to just dump the response body to the file. This supports huge files? ? _, err = io.Copy(file, response.Body)? ? if err != nil {? ? ? ? log.Fatal(err)? ? }? ? fmt.Println("Success!")? ? imgData, err := os.Open("tv.webp")? ? if err != nil {? ? ? ? fmt.Println(err)? ? ? ? return? ? }? ? log.Printf("%+v", imgData)? ? image, err := webp.Decode(imgData)? ? if err != nil {? ? ? ? fmt.Println(err)? ? ? ? return? ? }? ? fmt.Println(image.Bounds())}
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

下載文件不是webp類型。這是png。


package main


import (

    "fmt"

    "image"

    "io"

    "log"

    "net/http"

    "os"


    _ "image/png"

)


func main() {

    response, e := http.Get("https://www.sony.com/is/image/gwtprod/0abe7672ff4c6cb4a0a4d4cc143fd05b?fmt=png-alpha")

    if e != nil {

        log.Fatal(e)

    }

    defer response.Body.Close()


    file, err := os.Create("dump")

    if err != nil {

        log.Fatal(err)

    }

    defer file.Close()


    _, err = io.Copy(file, response.Body)

    if err != nil {

        log.Fatal(err)

    }

    fmt.Println("Success!")


    imageFile, err := os.Open("dump")

    if err != nil {

        panic(err)

    }


    m, name, err := image.Decode(imageFile)

    if err != nil {

        panic(err)

    }


    fmt.Println("image type is ", name, m.Bounds())

}


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號