【问题标题】:How to check if an image file is animated in Golang?如何检查图像文件是否在 Golang 中动画?
【发布时间】:2022-09-30 18:19:05
【问题描述】:

我还是 Go 新手,我想将 Python 项目移植到它。

该项目有几个要求,其中之一是将大量文件的信息存储到 SQLite 数据库中,这些信息包括:

  1. 文件的哈希值。
  2. 文件类型(如果是图像、视频、文档、代码等,仅根据其扩展名)。

    如果文件类型为image我需要知道图像是否是动画的.我需要支持.gif.webp.avif 扩展。

    在 Python 中,我只是使用Pillowseek 方法来检查框架1 是否存在。我正在使用包pillow-avif-plugin 为 Pillow 添加 avif 支持。这适用于所有提到的图像格式。

    无论如何在 Go 中做同样的事情?

    我不希望为每个文件调用外部程序,因为我认为这会损害性能。

标签: image go


【解决方案1】:

镜像包中有一个类型Paletted。您可以使用标准类型断言来检查此类型。

例如(其中 fileread 实现 io.Reader):

import (
   "fmt"
   "log"
   "image"
    _ "image/png"
    _ "image/gif"
    _ "image/jpeg"
)
// ...
imageF, _, err = image.Decode(fileread)
if err != nil {
    log.Fatalf("Error with image decode")
}
switch imageF.(type) {
case *image.Paletted:
    fmt.Println("This can be a gif image")
// proceed with your code 
}

这绝对适用于 *.gif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2013-01-13
    • 1970-01-01
    • 2010-10-27
    • 2011-01-12
    相关资源
    最近更新 更多