在 golang 中读取多页 tiff 并提取图像
admin 阅读:140 2024-03-22

如何在 Go 中将多页 tiff 分割成图像? image/tiff 的 DecodeAll 返回 TIFF,其中包含 image.image。但不知道如何将每个转换为图像?
正确答案
由于关于您在问题中使用的软件包的信息不多,我假设您使用了 github.com/chai2010/tiff 模块。它只包含 decodeall 方法。我使用了公开的多页tiff。然后我使用了包文档内的代码。无论如何,我在这里引用它
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/chai2010/tiff"
)
func main() {
b, err := ioutil.ReadFile("Multi_page24bpp.tif")
if err != nil {
panic(err)
}
// Decode tiff
m, errors, err := tiff.DecodeAll(bytes.NewReader(b))
if err != nil {
log.Println(err)
}
// Encode tiff
for i := 0; i < len(m); i++ {
for j := 0; j < len(m[i]); j++ {
newname := fmt.Sprintf("%s-%02d-%02d.tif", filepath.Base("Multi_page24bpp.tif"), i, j)
if errors[i][j] != nil {
log.Printf("%s: %vn", newname, err)
continue
}
var buf bytes.Buffer
if err = tiff.Encode(&buf, m[i][j], nil); err != nil {
log.Fatal(err)
}
if err = ioutil.WriteFile(newname, buf.Bytes(), 0666); err != nil {
log.Fatal(err)
}
fmt.Printf("Save %s okn", newname)
}
}
}它按照您的要求创建了多个 tif 图像。我希望这就是你的意思
声明
1、部分文章来源于网络,仅作为参考。 2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!



