Bash批量调整图像文件大小问题

发布于 2024-07-29 00:27:46 字数 488 浏览 5 评论 0原文

我有一个小脚本,用于调整目录中所有图像的大小。 我在 cygwin 中运行这个脚本,它使用“convert”来调整图像大小。 图像的分辨率改变得很好,但脚本运行后我遇到了文件大小问题。

我通常使用此脚本来调整从 Powerpoint 演示文稿中转储的图像大小,以便在我编写的小型 Web 演示应用程序中使用。 当我转储 gif 并运行脚本时,文件大小增加了一倍多(例如 8KB 到 18KB;14KB 到 50KB)

脚本的相关行如下:

/usr/bin/convert $holdfile -thumbnail x480 temp.GIF
mv temp.GIF $i

是否有一个开关可以防止文件大小增长很多? 我知道文件大小并不大,但是当我有很多人连接到演示文稿或不可避免的拨号用户时,我只想让他们的体验尽可能好。

编辑:我应该指定文件以 960px x 720px 分辨率开始,并调整为 640px x 480px。

I have a small script that I use to resize all of the images in a directory. I run this script in cygwin and it uses "convert" to do the image resizing. The images change their resolution just fine, but I am having problems with file sizes after the script is run.

I typically use this script to resize images dumped out from a Powerpoint presentation to use in a little web presentation app that I wrote. When I dump out gif's and run the script, the files more than double in size (ex. 8KB to 18KB; 14KB to 50KB)

The pertinent lines of the script are as follows:

/usr/bin/convert $holdfile -thumbnail x480 temp.GIF
mv temp.GIF $i

Is there a switch to prevent the file sizes from growing so much? I know that the file sizes are not huge, but when I have a good number of people connecting to a presentation or the unavoidable dialup users, I just want to make their experience as nice as possible.

Edit: I should have specified that the files start at a 960px x 720px resolution and are being resized to 640px x 480px.

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

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

发布评论

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

评论(2

清醇 2024-08-05 00:27:46

好吧,如果转换压缩比输入文件差,就会发生这种情况。 由于完全相同的压缩方案可能会产生不同的结果,具体取决于压缩代码的好坏,这种情况可能会发生。

这里另一个更可能的选择可能是您正在调整图像的大小,这可能会通过双三次调整大小来完成。 这会导致文本或绘图的边缘变得有点模糊。 这意味着它们会消耗更多的颜色并且压缩效果更差。

也可能是您的原始图像使用优化的调色板,可能只使用几种颜色,并且在调整大小后,由于调整大小所完成的平滑处理,它们需要单个 GIF 帧支持的完整 256 种颜色。

无论如何,您可能会看到使用 PNG 而不是 GIF 可以获得更好的性能。 PNG 被设计为 GIF 的现代替代品,目前使用的(图形)浏览器都不会出现显示 PNG(没有 Alpha 通道)的问题。 PNG 的压缩效果比 GIF 好得多,并且同时允许更多颜色。 还有像 optipng 这样的工具可以进一步压缩 PNG 图像。

Well, this can happen if convert compresses worse than the input files. Since the exact same compression scheme might yield different results depending how good the compressing code is this can happen.

Another, more likely option here would probably be that you are resizing the images which will probably be done with bicubic resizing. This causes the edges of text or drawings to become a little bit blurry. This means they use up more colors and compress worse.

Also likely would be that your original images use an optimized color palette, maybe just with a few colors and after resizing they need the full 256 colors which are supported by a single GIF frame, due to smoothing done by the resizing.

In any case, you probably should see better performance using PNG instead of GIF. PNG was designed as a modern replacement for GIF and no (graphical) browser in use today has problems displaying PNGs (without an alpha channel). PNG compresses much better than GIF, and allows more colors at the same time. Also there are tools like optipng which will compress PNG images even further.

薆情海 2024-08-05 00:27:46

转换会自动优化调色板,但由于调整大小期间混合颜色,调色板可能会增大。 您应该能够在图形程序中检查源图像和生成的图像并查看颜色的数量。

GIF 仅支持 LZW 压缩,但由于专利限制已过期(最后一次是 2004 年),一度需要手动启用 LZW 压缩。 我不确定情况是否仍然如此,但值得研究。

如果指定了 LZW 压缩但
LZW压缩尚未启用,
图像数据被写入
未压缩的 LZW 格式可以是
由LZW解码器读取。 这可能会导致
大于预期的 GIF 文件。
- imagemagick.org

Convert automatically optimizes the palette however the palette might be growing due to colors being blended during resize. You should be able to inspect the source and resultant images in a graphics program and see the number of colors.

GIF only supports LZW compression but due to patent restraints that have since expired (the last was 2004) it was once necessary to manually enable LZW compression. I'm not sure if that is still the case however it's worth looking into.

If LZW compression is specified but
LZW compression has not been enabled,
the image data is written in an
uncompressed LZW format that can be
read by LZW decoders. This may result
in larger-than-expected GIF files.
- imagemagick.org

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