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条)

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器