压缩 TIF 文件

发布于 2024-07-04 22:58:30 字数 100 浏览 6 评论 0原文

我正在尝试将多页彩色 tiff 文件转换为 C# 中的 ac# CompressionCCITT3 tiff。 我意识到我需要确保所有像素都是 1 位。 我在网上没有找到有用的例子。

I'm trying to convert a multipage color tiff file to a c# CompressionCCITT3 tiff in C#. I realize that I need to make sure that all pixels are 1 bit. I have not found a useful example of this online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肤浅与狂妄 2024-07-11 22:58:31

我建议在深入编码之前首先使用 tiff 和图像实用程序尝试所需的结果。 我发现 VIPS 是一个方便的工具。 下一个选择是研究 LibTIFF 可以做什么。 我使用 C# 使用免费的 LibTiff.NET 取得了良好的效果(另请参阅 stackoverflow)。 我对 GDI tiff 功能感到非常失望,尽管您的情况可能会有所不同(我需要缺少的 16 位灰度)。
您还可以使用 LibTiff 实用程序(即参见 LibTiff 实用程序)。 org/man/tiffcp.1.html" rel="nofollow noreferrer">http://www.libtiff.org/man/tiffcp.1.html)

I suggest to experiment with the desired results first using tiff and image utilities before diving into the coding. I found VIPS to be a handy tool. The next option is to look into what LibTIFF can do. I've had good results with the free LibTiff.NET using c# (see also stackoverflow). I was very disappointed by the GDI tiff functionality, although your milage may vary (I need the missing 16-bit-grayscale).
Also you can use the LibTiff utilities (i.e. see http://www.libtiff.org/man/tiffcp.1.html)

獨角戲 2024-07-11 22:58:31

拉皮条免责声明:我在 Atalasoft 工作,这是一家生产 .NET 成像软件的公司。

使用 dotImage,此任务变得如下所示:

FileSystemImageSource source = new FileSystemImageSource("path-to-your-file.tif", true); // true = loop over all frames
// tiff encoder will auto-select an appropriate compression - CCITT4 for 1 bit.
TiffEncoder encoder = new TiffEncoder();
encoder.Append = true;

// DynamicThresholdCommand is very good for documents.  For pictures, use DitherCommand
DynamicThresholdCommand threshold = new DynamicThresholdCommand();

using (FileStream outstm = new FileStream("path-to-output.tif", FileMode.Create)) {
    while (source.HasMoreImages()) {
        AtalaImage image = source.AcquireNext();
        AtalaImage finalImage = image;
        // convert when needed.
        if (image.PixelFormat != PixelFormat.Pixel1bppIndexed) {
            finalImage = threshold.Apply().Image;
        }
        encoder.Save(outstm, finalImage, null);
        if (finalImage != image) {
            finalImage.Dispose();
        }
        source.Release(image);
    }
}

Bob Powell 示例就其本身而言,它很好,但它有很多问题,其中最重要的是它使用了一个简单的阈值,如果您想要速度并且实际上并不关心您的输出是什么样子或您的结果,那么这是非常棒的输入域实际上已经几乎是黑白的——只是用颜色表示。 二值化是一个棘手的问题。 当你的任务是将可用信息减少 1/24 时,如何保留正确的信息并丢弃其余的信息是一个挑战。 DotImage 有六种不同的二值化工具 (IIRC)。 从我的角度来看,SimpleThreshold 是最底层的。

Pimping disclaimer: I work for Atalasoft, a company that makes .NET imaging software.

Using dotImage, this task becomes something like this:

FileSystemImageSource source = new FileSystemImageSource("path-to-your-file.tif", true); // true = loop over all frames
// tiff encoder will auto-select an appropriate compression - CCITT4 for 1 bit.
TiffEncoder encoder = new TiffEncoder();
encoder.Append = true;

// DynamicThresholdCommand is very good for documents.  For pictures, use DitherCommand
DynamicThresholdCommand threshold = new DynamicThresholdCommand();

using (FileStream outstm = new FileStream("path-to-output.tif", FileMode.Create)) {
    while (source.HasMoreImages()) {
        AtalaImage image = source.AcquireNext();
        AtalaImage finalImage = image;
        // convert when needed.
        if (image.PixelFormat != PixelFormat.Pixel1bppIndexed) {
            finalImage = threshold.Apply().Image;
        }
        encoder.Save(outstm, finalImage, null);
        if (finalImage != image) {
            finalImage.Dispose();
        }
        source.Release(image);
    }
}

The Bob Powell example is good, as far as it goes, but it has a number of problems, not the least of which is that it's using a simple threshold, which is terrific if you want speed and don't actually care what your output looks like or your input domain is such that really is pretty much black and white already - just represented in color. Binarization is a tricky problem. When your task is to reduce available information by 1/24th, how to keep the right information and throw away the rest is a challenge. DotImage has six different tools (IIRC) for binarization. SimpleThreshold is bottom of the barrel, from my point of view.

两仪 2024-07-11 22:58:31

我看到上面的代码,看起来它正在用手动逻辑转换每个像素。

这对你有用吗?

Imports System.Drawing.Imaging

'获取颜色 tif 文件

Dim bmpColorTIF As New Bitmap("C:\color.tif")

'选择 tif 的一个区域(将抓取所有帧)

Dim rectColorTIF As New Rectangle(0, 0 , bmpColorTIF.Width, bmpColorTIF.Height )

'将矩形克隆为 1 位颜色 tif

Dim bmpBlackWhiteTIF As Bitmap = bmpColorTIF.Clone(rectColorTIF, PixelFormat.Format1bppIndexed)

'使用新位图执行您想要的操作(保存等)

..注意

:有大量的像素格式可供选择。

I saw the above code, and it looked like it was converting every pixel with manual logic.

Would this work for you?

Imports System.Drawing.Imaging

'get the color tif file

Dim bmpColorTIF As New Bitmap("C:\color.tif")

'select the an area of the tif (will grab all frames)

Dim rectColorTIF As New Rectangle(0, 0, bmpColorTIF.Width, bmpColorTIF.Height )

'clone the rectangle as 1-bit color tif

Dim bmpBlackWhiteTIF As Bitmap = bmpColorTIF.Clone(rectColorTIF, PixelFormat.Format1bppIndexed)

'do what you want with the new bitmap (save, etc)

...

Note: there are a ton of pixelformats to choose from.

翻身的咸鱼 2024-07-11 22:58:30

您需要进行此转换,因为 CCITT3 和 CCITT4 不支持颜色(如果我没记错的话)。

You need this conversion as CCITT3 and CCITT4 don't support color (if I remember right).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文