golang图像文件类型检测插件库goimghdr的使用

golang图像文件类型检测插件库goimghdr的使用

Build Status GoDoc Go Report Card Coverage Status

简介

goimghdr是一个受Python的imghdr模块启发的Golang图像文件类型检测库。

安装

go get github.com/corona10/goimghdr

返回值列表

返回值 图像格式
“rgb” SGI ImgLib文件
“gif” GIF 87a和89a文件
“pbm” 便携式位图文件
“pgm” 便携式灰度图文件
“ppm” 便携式像素图文件
“tiff” TIFF文件
“rast” Sun光栅文件
“xbm” X位图文件
“jpeg” JFIF或Exif格式的JPEG数据
“bmp” BMP文件
“png” 便携式网络图形
“webp” WebP文件
“exr” OpenEXR文件

使用示例

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
	"os"
)

func main() {
	// 打开图像文件
	file, err := os.Open("test.jpg")
	if err != nil {
		fmt.Println("打开文件错误:", err)
		return
	}
	defer file.Close()

	// 检测文件类型
	fileType := goimghdr.What(file)
	if fileType == "" {
		fmt.Println("无法识别图像类型")
		return
	}

	fmt.Println("检测到的图像类型:", fileType)
	
	// 也可以直接通过文件路径检测
	fileType = goimghdr.WhatFromFile("test.png")
	fmt.Println("PNG文件类型:", fileType)
}

asciicast

特别感谢

  • Haeun Kim

参考

  • CPython imghdr模块
  • DSCN0029.jpg来自ianare/exif-samples

更多关于golang图像文件类型检测插件库goimghdr的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang图像文件类型检测插件库goimghdr的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


goimghdr - Golang图像文件类型检测库

goimghdr 是一个轻量级的 Golang 库,用于检测图像文件的类型(如 JPEG、PNG、GIF 等)。它类似于 Python 的 imghdr 模块,但专为 Go 语言设计。

安装

go get github.com/corona10/goimghdr

基本用法

1. 检测文件类型

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
)

func main() {
	// 检测文件类型
	fileType := goimghdr.What("/path/to/image.jpg")
	fmt.Println("File type:", fileType) // 输出: jpeg
}

2. 从字节切片检测

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
	"os"
)

func main() {
	// 读取文件内容
	data, err := os.ReadFile("/path/to/image.png")
	if err != nil {
		panic(err)
	}

	// 从字节切片检测
	fileType := goimghdr.WhatFromBytes(data)
	fmt.Println("File type:", fileType) // 输出: png
}

3. 从 Reader 检测

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
	"os"
)

func main() {
	// 打开文件
	file, err := os.Open("/path/to/image.gif")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	// 从 Reader 检测
	fileType := goimghdr.WhatFromReader(file)
	fmt.Println("File type:", fileType) // 输出: gif
}

支持的图像格式

goimghdr 支持检测以下图像格式:

  • jpeg
  • png
  • gif
  • bmp
  • webp
  • tiff
  • ico

高级用法

自定义检测函数

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
)

// 自定义检测函数
func myDetector(data []byte) string {
	if len(data) > 4 && string(data[:4]) == "MYIM" {
		return "myimg"
	}
	return ""
}

func main() {
	// 注册自定义检测器
	goimghdr.RegisterDetector("myimg", myDetector)

	// 现在可以检测自定义格式
	fileType := goimghdr.WhatFromBytes([]byte("MYIM..."))
	fmt.Println("File type:", fileType) // 输出: myimg
}

获取所有支持的格式

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
)

func main() {
	// 获取所有支持的格式
	formats := goimghdr.GetSupportedFormats()
	fmt.Println("Supported formats:", formats)
	// 输出类似: [jpeg png gif bmp webp tiff ico]
}

性能考虑

goimghdr 通过读取文件头部少量字节来检测类型,因此非常高效。对于大文件,建议使用 WhatFromReader 而不是先读取整个文件。

错误处理

当无法识别文件类型时,goimghdr 会返回空字符串:

fileType := goimghdr.What("/path/to/unknown.file")
if fileType == "" {
	fmt.Println("Unknown file type")
}

实际应用示例

package main

import (
	"fmt"
	"github.com/corona10/goimghdr"
	"os"
	"path/filepath"
)

func main() {
	// 扫描目录中的图像文件
	err := filepath.Walk("./images", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		
		if !info.IsDir() {
			fileType := goimghdr.What(path)
			if fileType != "" {
				fmt.Printf("%s: %s\n", path, fileType)
			} else {
				fmt.Printf("%s: Not an image\n", path)
			}
		}
		return nil
	})
	
	if err != nil {
		fmt.Println("Error walking directory:", err)
	}
}

与其他库的比较

相比于直接使用 net/http 的 DetectContentType 或完整的图像解码库,goimghdr 的优势在于:

  1. 更轻量级
  2. 更专注于图像类型检测
  3. 不需要完整解码图像

注意事项

  1. 某些图像可能有多种可能的扩展名(如 jpg 和 jpeg),goimghdr 会返回规范化的名称(如 jpeg)
  2. 对于损坏的图像文件,检测结果可能不准确
  3. 该库仅检测文件类型,不验证文件内容的完整性

goimghdr 是一个简单实用的工具,特别适合需要快速识别图像类型而不需要处理图像内容的场景。

回到顶部