我正在使用image包来解码图像并确定它们的格式(例如,jpg或png),但是我想深入一层,我想知道png实际上是png8还是png24。
使用Go做这件事的正确方式是什么?
(更新1)
目前我正在阅读能够解码图像,我想知道如何从这里抓取颜色模型:
fname := "img.jpg"
f, err := os.Open(fname)
_, format, err := image.Decode(f)
if err != nil {
log.Fatal(err)
}
fmt.Println(format, "format")发布于 2020-03-07 05:11:21
试一下,只需记住它没有健全的检查
package main
import (
"errors"
"fmt"
_ "image/png"
"os"
)
func pngType(f *os.File) (string, error) {
f.Seek(24, 0)
b := make([]byte, 1)
f.Read(b)
c := make([]byte, 1)
f.Read(c)
bitDepth := b[0]
colorType := c[0]
if bitDepth == 8 && colorType == 3 {
return "PNG8", nil
}
if bitDepth == 8 && colorType == 2 {
return "PNG24", nil
}
if bitDepth == 8 && colorType == 6 {
return "PNG32", nil
}
return "", errors.New("unknown_type")
}
func main() {
f, _ := os.Open("img.png")
t, _ := pngType(f)
fmt.Printf("The type is `%s`.\n", t)
}您可以查看specs以获取参考
https://stackoverflow.com/questions/60503938
复制相似问题