如何在Golang中通过URL获取图像分辨率

huangapple go评论79阅读模式
英文:

how to get image resolution from url in golang

问题

如何在golang中从URL获取图像的分辨率。

以下是我尝试的代码。

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

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

你们能告诉我如何在上述情况下获取分辨率吗?

英文:

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

答案1

得分: 13

你离成功就差一点了。你的g变量是image.Rectangle类型,它具有Dx()Dy()方法,分别用于获取宽度和高度。我们可以使用这些方法来计算分辨率。

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

	// 获取高度和宽度
	height := g.Dy()
	width := g.Dx()

	// 分辨率为高度乘以宽度
	resolution := height * width

	// 打印结果
	fmt.Println(resolution, "像素")
}

希望对你有帮助!

英文:

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")
}

答案2

得分: 3

image.Decode解码整个图像,使用image.DecodeConfig来解析图像头部:

package main

import (
    "image"
    _ "image/jpeg"
    "net/http"
)

func main() {
    resp, err := http.Get("http://i.imgur.com/Peq1U1u.jpg")
    if err != nil {
        return // 处理错误
    }
    defer resp.Body.Close()

    img, _, err := image.DecodeConfig(resp.Body)
    if err != nil {
        return // 处理错误
    }

    fmt.Println(img.Width * img.Height, "像素")
}
英文:

image.Decode decodes the entire image, use image.DecodeConfig instead to parse only the image header:

package main

import (
    "image"
    _ "image/jpeg"
    "net/http"
)

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

    img, _, err := image.DecodeConfig(resp.Body)
    if err != nil {
        return // handle error somehow
    }

    fmt.Println(img.Width * img.Height, "pixels")
}

huangapple
  • 本文由 发表于 2014年8月13日 02:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/25271654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定