dscuu86620 2014-08-12 18:35
浏览 816
已采纳

如何从golang中的url获得图像分辨率

How to get the resolution of an image from a URL in golang.

Below is the code i am trying.

resp, err := http.Get("http://i.imgur.com/Peq1U1u.jpg")
if err != nil {
    // handle error
}
defer resp.Body.Close()

m, _, err := image.Decode(resp.Body)
if err != nil {
    // handle error
}
g := m.Bounds()
fmt.Printf(g.String())

Can you guys tell me how to get the resolution in above situation

  • 写回答

2条回答 默认 最新

  • duanpao9781 2014-08-12 19:12
    关注

    You're almost there. Your g variable is of the image.Rectangle type, which has the Dx() and Dy() methods, which give width and height respectively. We can use these to compute the resolution.

    package main
    
    import (
        "fmt"
        "image"
        _ "image/gif"
        _ "image/jpeg"
        _ "image/png"
        "log"
        "net/http"
    )
    
    func main() {
        resp, err := http.Get("http://i.imgur.com/Peq1U1u.jpg")
        if err != nil {
            log.Fatal(err)
        }
        defer resp.Body.Close()
    
        m, _, err := image.Decode(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
        g := m.Bounds()
    
        // Get height and width
        height := g.Dy()
        width := g.Dx()
    
        // The resolution is height x width
        resolution := height * width
    
        // Print results
        fmt.Println(resolution, "pixels")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数