读取多页 tiff 文件并在 golang 中提取图像

huangapple go评论91阅读模式
英文:

Read multipage tiff and extract images in golang

问题

你可以使用Go语言中的image/tiff包来将多页TIFF文件拆分为多个图像。image/tiff包中的DecodeAll函数可以将整个TIFF文件解码为一个TIFF类型的对象,其中包含了多个图像。然后,你可以使用image包中的相关函数将每个图像转换为image.Image类型的对象,以便进行后续处理。

英文:

How can I split multi page tiff into images in go? image/tiff's DecodeAll return TIFF, which contains image.image. But don't know how to convert each into an image?

答案1

得分: 1

根据你的问题中提供的信息,我假设你使用了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)
	}

	// 解码tiff
	m, errors, err := tiff.DecodeAll(bytes.NewReader(b))
	if err != nil {
		log.Println(err)
	}

	// 编码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: %v\n", 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("保存 %s 成功\n", newname)
		}
	}
}

根据你的要求,它会创建多个tif图像文件。希望这符合你的意思。

英文:

As there is not much information regarding the package you used in your question, I am going to assume that, you used github.com/chai2010/tiff module. It only contains DecodeAll method. I used a multipage tiff which was publically available. Then I used the code inside the package documentation. Anyway I'm quoting it here

package main

import (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;path/filepath&quot;

	&quot;github.com/chai2010/tiff&quot;
)

func main() {
	b, err := ioutil.ReadFile(&quot;Multi_page24bpp.tif&quot;)
	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 &lt; len(m); i++ {
		for j := 0; j &lt; len(m[i]); j++ {
			newname := fmt.Sprintf(&quot;%s-%02d-%02d.tif&quot;, filepath.Base(&quot;Multi_page24bpp.tif&quot;), i, j)
			if errors[i][j] != nil {
				log.Printf(&quot;%s: %v\n&quot;, newname, err)
				continue
			}

			var buf bytes.Buffer
			if err = tiff.Encode(&amp;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(&quot;Save %s ok\n&quot;, newname)
		}
	}
}

It created multiple tif images as you asked. I hope that's what you meant

huangapple
  • 本文由 发表于 2023年6月10日 06:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76443898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定