ImageMagick:以最大文件大小缩放PNG图像 [英] ImageMagick: scale PNG image with a maximum file-size

查看:89
本文介绍了ImageMagick:以最大文件大小缩放PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何缩放/优化/最小化输出 PNG 图片,使其恰好低于某个最大文件大小?(输入源多种多样-PDF,JPEG,GIF,TIFF ...)

How would you scale/optimize/minimally output a PNG image so that it just falls below a certain maximum file size? (The input sources are various - PDF, JPEG, GIF, TIFF...)

我看过很多地方,但是找不到这个问题的答案.

I've looked in many places but can't find an answer to this question.

在ImageMagick中, JPEG 输出可以使用 extent 执行此操作(请参见例如

In ImageMagick a JPEG output can do this with extent (see e.g. ImageMagick: scale JPEG image with a maximum file-size), but there doesn't seem to be an equivalent for other file formats e.g. PNG.

我可以在一个循环中使用Wand或PIL(对python优先),直到文件大小低于某个值为止,但是对于1000幅图像,除非有一种预测/估计文件大小的方法,否则这将具有较大的I/O开销.不用先写出来也许这是唯一的选择.

I could use Wand or PIL in a loop (preference for python) until the filesize is below a certain value, but for 1000s of images this will have a large I/O overhead unless there's a way to predict/estimate the filesize without writing it out first. Perhaps this is the only option.

我还可以将各种(macOS)命令行工具包装在python中.

I could also wrap the various (macOS) command-line tools in python.

此外,我只想在绝对必要的地方进行任何压缩(来源主要是文本),因此可以选择压缩算法.

Additionally, I only want to do any compression at all where it's absolutely necessary (the source is mainly text), which leaves a choice of compression algorithms.

感谢所有帮助.

PS其他相关问题:

根据最大文件大小缩放图像

使用ImageMagick压缩PNG图片

python设置了最大值使用例如将(pdf)转换为jpeg时的文件大小魔杖

https://stackoverflow.com/a/40588202/1021819 是也非常接近-尽管那里的确切代码(不可避免地?)对如何减小文件大小(在这种情况下为 resize )进行了一些选择.如果没有多维搜索,也许没有通用的方法可以做到这一点.

https://stackoverflow.com/a/40588202/1021819 is quite close too - though the exact code there already (inevitably?) makes some choices about how to go about reducing the file size (resize in that case). Perhaps there is no generalized way to do this without a multi-dimensional search.

而且,由于输入文件是PDF,所以甚至可以用PIL做到这一点吗?首选是栅格化,我一直在使用Wand.

Also, since the input files are PDFs, can this even be done with PIL? The first choice is about rasterization, for which I have been using Wand.

https://stackoverflow.com/a/34618887/1021819 也是有用的,因为它使用了魔杖,因此,将该操作放入binary-chop循环中似乎是一种前进的方式.

https://stackoverflow.com/a/34618887/1021819 is also useful, in that it uses Wand, so putting that operation within the binary-chop loop seems to be a way forward.

推荐答案

使用PNG时,由于PNG无损,因此无法在压缩方法和外观上进行权衡.只需使用最小的文件我的"pngcrush"应用程序,"optipng","zopflipng"之类.

With PNG there is no tradeoff of compression method and visual appearance because PNG is lossless. Just go for the smallest possible file, using my "pngcrush" application, "optipng", "zopflipng" or the like.

如果您需要的文件比任何一个文件都小,请尝试将颜色数量减少到255个或更少,这将使PNG编解码器可以生成大约1的索引颜色PNG(颜色类型3)RGB PNG文件大小的/3.您可以使用ImageMagick的"-colors 255"选项来执行此操作.但是,我建议为此使用"pngquant"应用程序.在大多数情况下,它的工作要比IM更好.

If you need a smaller file than any of those can produce, try reducing the number of colors to 255 or fewer, which will allow the PNG codec to produce an indexed-color PNG (color-type 3) which is around 1/3 of the filesize of an RGB PNG. You can use ImageMagick's "-colors 255" option to do this. However, I recommend the "pngquant" application for this; it does a better job than IM does in most cases.

这篇关于ImageMagick:以最大文件大小缩放PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆