英文:
Check if png image is all transparent in golang
问题
我有多个类似这样的URL,它们总是返回一个PNG图像。
我想检查从URL返回的图像是否完全为空(透明),就像上面的URL一样,或者它是否包含一些实际的图像。我通过下面的函数进行请求,并检查HTTP状态是否为200,返回的内容类型是否为图像。我需要在这里添加功能来测试图像是否为空。
thumbnail := "https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/export?bbox=-106.6462299999999885,25.83722399999999908,-93.50780600000010168,36.50038700000000347&size=640,519&bboxSR=4326&layers=show:4&f=image&transparent=true"
resp, err := client.Get(thumbnail)
if err != nil {
fmt.Println(err)
} else if resp.StatusCode == 200 && strings.HasPrefix(resp.Header["Content-Type"][0], "image") {
return thumbnail
} else {
fmt.Println(thumbnail, resp.StatusCode, resp.Header["Content-Type"][0])
}
英文:
I have multiple urls like this one that always return a PNG image.
I want to check if the returned image from the url is all empty(transparent) like the above url or it has some actual image in there. I am making the request through below function and checking that the HTTP status is 200 and the returned content type is image. I need to add the functionality here to test if image is empty or not.
thumbnail := "https://hydro.nationalmap.gov/arcgis/rest/services/wbd/MapServer/export?bbox=-106.6462299999999885,25.83722399999999908,-93.50780600000010168,36.50038700000000347&size=640,519&bboxSR=4326&layers=show:4&f=image&transparent=true"
resp, err := client.Get(thumbnail)
if err != nil {
fmt.Println(err)
} else if resp.StatusCode == 200 && strings.HasPrefix(resp.Header["Content-Type"][0], "image") {
return thumbnail
} else {
fmt.Println(thumbnail, resp.StatusCode, resp.Header["Content-Type"][0])
}
答案1
得分: 2
标准库提供了功能强大的image
包和image/png
解码器,让我们可以很容易地实现这个功能。
我们知道透明度意味着alpha=0,我们只需要遍历图像的像素即可。
package main
import (
"image/png"
"io"
"log"
"net/http"
)
func main() {
transparentResp, _ := http.Get("https://upload.wikimedia.org/wikipedia/commons/3/3d/1_120_transparent.png")
defer transparentResp.Body.Close()
notTransparentResp, _ := http.Get("https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Pitch.png/640px-Pitch.png")
defer notTransparentResp.Body.Close()
println(isFullyTransparentPng(transparentResp.Body))
println(isFullyTransparentPng(notTransparentResp.Body))
}
func isFullyTransparentPng(reader io.Reader) bool {
img, _ := png.Decode(reader)
for x := img.Bounds().Min.X; x < img.Bounds().Dx(); x++ {
for y := img.Bounds().Min.Y; y < img.Bounds().Dy(); y++ {
_, _, _, alpha := img.At(x, y).RGBA()
if alpha != 0 {
return false
}
}
}
return true
}
英文:
The standard library offers the capable image
package and the image/png
decoder that lets us do this quite easily.
We know that transparency means alpha=0, all we need to do is iterate through the image's pixels.
package main
import (
"image/png"
"io"
"log"
"net/http"
)
func main() {
transparentResp, _ := http.Get("https://upload.wikimedia.org/wikipedia/commons/3/3d/1_120_transparent.png")
defer transparentResp.Body.Close()
notTransparentResp, _ := http.Get("https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Pitch.png/640px-Pitch.png")
defer notTransparentResp.Body.Close()
println(isFullyTransparentPng(transparentResp.Body))
println(isFullyTransparentPng(notTransparentResp.Body))
}
func isFullyTransparentPng(reader io.Reader) bool {
img, _ := png.Decode(reader)
for x := img.Bounds().Min.X; x < img.Bounds().Dx(); x++ {
for y := img.Bounds().Min.Y; y < img.Bounds().Dy(); y++ {
_, _, _, alpha := img.At(x, y).RGBA()
if alpha != 0 {
return false
}
}
}
return true
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论