缩放和减少颜色以减小扫描文件大小

发布于 2024-12-28 23:48:52 字数 1004 浏览 0 评论 0原文

我需要减小彩色扫描的文件大小。

到目前为止,我认为应该采取以下步骤:

  • 选择性模糊(或类似)以将噪点
  • 比例降低到约120dpi
  • 减少颜色

到目前为止,我们使用convert(imagemagick)和net-ppm工具。

扫描件是发票,不是照片。

任何提示表示赞赏。

更新

示例:

Bounty

example.png 最小且可读性好的缩减文件具有可重现的解决方案即可获得赏金。该解决方案只需要使用开源软件。

文件格式并不重要,只要你能再次将其转换为PNG即可。处理时间并不重要。稍后我可以优化。

更新

我得到了非常好的黑白输出结果(谢谢)。将颜色减少到大约 16 或 32 种颜色会很有趣。

I need to reduce the file size of a color scan.

Up to now I think the following steps should be made:

  • selective blur (or similar) to reduce noise
  • scale to ~120dpi
  • reduce colors

Up to now we use convert (imagemagick) and net-ppm tools.

The scans are invoices, not photos.

Any hints appreciated.

Update

example:

Bounty

The smallest and good readable reduced file of example.png with a reproduce-able solution gets the bounty. The solution needs to use open source software only.

The file format is not important, as long as you can convert it to PNG again. Processing time is not important. I can optimize later.

Update

I got very good results for black-and-white output (thank you). Color reducing to about 16 or 32 colors would be interesting.

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

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

发布评论

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

评论(3

幽梦紫曦~ 2025-01-04 23:48:52

这是一个相当开放式的问题,因为图像质量和图像大小之间仍然有可能的弹性空间......毕竟,将其设为黑白并使用 CCITT T.6 黑白(传真式)压缩进行压缩正在发展击败大多数(如果不是所有)支持颜色的压缩算法。

如果您愿意使用黑白(而不是灰度),那就这样做!它使文档变得非常小。

否则,我建议使用一系列较小的图像转换和自适应预测树(请参阅此处)。 APT 软件包是开源或公共领域的,非常易于编译和使用。它的优点是它在各种图像类型(尤其是文本)上表现良好,并且允许您更好地缩放图像大小与图像质量,而不会失去可读性。 (我发现自己在可读性的阈值上将 example_1000 大小的彩色版本压缩到 48KB,以及具有明显伪影但易读性的 64K。)

我将 APT 与 imagemagick 调整结合起来:

convert example.png -resize 50% -selective-blur 0x4+10% -brightness-contrast -5x30 -resize 80% example.ppm
./capt example.ppm example.apt 20  # The 20 means quality in the range [0,100]

并反转该过程

./dapt example.apt out_example.ppm
convert out_example.ppm out_example.png

解释 imagemagick 设置:

  • -resize 50% 将其缩小一半以提高处理速度。还隐藏了一些打印和扫描伪影。
  • -selective-blur 0x4+10%:锐化实际上会产生更多噪点。你真正想要的是选择性模糊(就像在 Photoshop 中一样),当没有“边缘”时就会模糊。
  • -brightness-contrast -5x30:这里我们将对比度增加了一点,以消除由页面轮廓引起的不良着色(导致可压缩数据较少)。我们还稍微变暗以使黑色更黑。
  • -resize 80% 最后,我们将大小调整为比您的 example_1000 图像尺寸大一点。 (足够接近了。)这也减少了明显伪像的数量,因为当像素合并在一起时它们在某种程度上被隐藏了。

此时,您将在本例中看到一个漂亮的图像——漂亮、平滑的颜色和清晰的文本。然后我们压缩。质量值 20 是一个相当低的设置,看起来不再那么漂亮,但文档非常清晰。即使质量值为 0,它仍然基本清晰。

再次强调,使用 ADT 并不一定会为该图像带来最佳结果,但它不会在类似摄影的内容(例如渐变)上变成完全无法识别的混乱,因此您应该更好地涵盖更多类型或意外情况文件类型。

结果:
88kb
76kb
64kb
48kb

压缩前处理过的图像

This is a rather open ended question since there's still possible room for flex between image quality and image size... after all, making it black and white and compressing it with CCITT T.6 black and white (fax-style) compression is going to beat the pants off most if not all color-capable compression algorithms.

If you're willing to go black and white (not grayscale), do that! It makes documents very small.

Otherwise I recommend a series of minor image transformations and Adaptive Prediction Trees (see here). The APT software package is opensource or public domain and very easy to compile and use. Its advantages are that it performs well on a wide variety of image types, especially text, and it will allow you to scale image size vs. image quality better without losing readability. (I found myself squishing a example_1000-sized color version down to 48KB on the threshold of readability, and 64K with obvious artifacts but easy readability.)

I combined APT with imagemagick tweakery:

convert example.png -resize 50% -selective-blur 0x4+10% -brightness-contrast -5x30 -resize 80% example.ppm
./capt example.ppm example.apt 20  # The 20 means quality in the range [0,100]

And to reverse the process

./dapt example.apt out_example.ppm
convert out_example.ppm out_example.png

To explain the imagemagick settings:

  • -resize 50% Make it half as small to make processing faster. Also hides some print and scan artifacts.
  • -selective-blur 0x4+10%: Sharpening actually creates more noise. What you actually want is a selective blur (like in Photoshop) which blurs when there's no "edge".
  • -brightness-contrast -5x30: Here we increase the contrast a good bit to clip the bad coloration caused by the page outline (leading to less compressible data). We also darken slightly to make the blacks blacker.
  • -resize 80% Finally, we resize to a little bigger than your example_1000 image size. (Close enough.) This also reduces the number of obvious artifacts since they're somewhat hidden when the pixels are merged together.

At this point you're going to have a fine looking image in this example -- nice, smooth colors and crisp text. Then we compress. The quality value of 20 is a pretty low setting and it's not as spiffy looking anymore, but the document is very legible. Even at a quality value of 0 it's still mostly legible.

Again, using ADT isn't going to necessarily lead to the best results for this image, but it won't turn into an entirely unrecognizable mess on photographic-like content such as gradients, so you should be covered better on more types or unexpected types of documents.

Results:
88kb
76kb
64kb
48kb

Processed image before compression

不爱素颜 2025-01-04 23:48:52

如果您确实不关心颜色的数量,我们不妨转向黑白并使用双层编码器。我最终使用了 DJVU 格式,因为它与 JBIG2 相比效果很好,并且具有开源编码器。在本例中,我使用了 didjvu 编码器,因为它取得了最佳结果。 (在 Ubuntu 上,你可以 apt-get install didjvu,也许在其他发行版上也可以。)

我最终得到的魔法看起来像这样进行编码:

convert example.png -resize 50% -selective-blur 0x4+10% -normalize -brightness-contrast -20x100 -dither none -type bilevel example_djvu.pgm
didjvu encode -o example.djvu example_djvu.pgm --lossless

请注意,这实际上是全分辨率下 0x2+10% 的卓越颜色模糊 --这最终将使图像在转换为双层图像之前尽可能地美好。

解码工作原理如下:

convert example.djvu out_example.png

即使分辨率较高(更容易阅读),大小仍为 24KB。当缩小到相同大小时,它仍然是24KB!最后,仅原始图像的 75% 缩小和 0x5+10% 模糊,其重量为 32KB。

请参阅此处的视觉结果: http://img29.imageshack.us/img29/687/示例djvu.png

If you truly don't care about the number of colors, we may as well go to black-and-white and use a bilevel coder. I ended up using the DJVU format because it compares well to JBIG2 and has open source encoders. In this case I used the didjvu encoder because it achieved the best results. (On Ubuntu you can apt-get install didjvu, perhaps on other distributions as well.)

The magic I ended up with looks like this to encode:

convert example.png -resize 50% -selective-blur 0x4+10% -normalize -brightness-contrast -20x100 -dither none -type bilevel example_djvu.pgm
didjvu encode -o example.djvu example_djvu.pgm --lossless

Note that this is actually a superior color blur to 0x2+10% at full resolution -- this will end up making the imagine about as nice as imaginable before it's converted to a bilevel image.

Decoding works as follows:

convert example.djvu out_example.png

Even with the larger resolution (which is much easier to read), the size weights in at 24KB. When reduced to the same size, it's still 24KB! Lastly, at only a 75% of the original image reduction and a 0x5+10% blur it weights in at 32KB.

See here for the visual results: http://img29.imageshack.us/img29/687/exampledjvu.png

空心↖ 2025-01-04 23:48:52

如果您已经使用 Imagemagick 实用程序“转换”完成了正确的操作,那么首先查看 Imagemagick 库可能是个好主意。

快速浏览一下我的 Ubuntu 软件包列表,显示了 perl、python、ruby、c++ 和 java 的绑定

If you already have it doing the right thing with the Imagemagick utility "convert" then it might be a good idea to look at the Imagemagick libraries first.

A quick look at my Ubuntu package lists shows bindings for perl,python,ruby,c++ and java

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