首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定png“类型”(png8 / png24)

如何确定png“类型”(png8 / png24)
EN

Stack Overflow用户
提问于 2020-03-03 17:24:09
回答 1查看 156关注 0票数 0

我正在使用image包来解码图像并确定它们的格式(例如,jpg或png),但是我想深入一层,我想知道png实际上是png8还是png24

使用Go做这件事的正确方式是什么?

(更新1)

目前我正在阅读能够解码图像,我想知道如何从这里抓取颜色模型:

代码语言:javascript
复制
fname := "img.jpg"
f, err := os.Open(fname)
_, format, err := image.Decode(f)
if err != nil {
    log.Fatal(err)
}

fmt.Println(format, "format")
EN

回答 1

Stack Overflow用户

发布于 2020-03-07 05:11:21

试一下,只需记住它没有健全的检查

代码语言:javascript
复制
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以获取参考

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60503938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档