jpegtran 优化而不更改文件名

发布于 2024-10-30 19:17:40 字数 161 浏览 0 评论 0原文

我需要优化一些图像,但不更改它们的名称。

jpegtran -copy none -optimize image.jpg > image.jpg 

但是,这似乎创建了 0 的文件大小。当我对不同的文件名执行此操作时,大小仍然完全相同。

I need to optimize some images, but not change their name.

jpegtran -copy none -optimize image.jpg > image.jpg 

However, this seems to create an filesize of 0. When i do it to a different filename, the size is still exactly the same.

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

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

发布评论

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

评论(6

够运 2024-11-06 19:17:40

怎么样:

jpegtran -copy none -optimize -outfile image.jpg image.jpg

我无论如何都不是 shell 专家,但我认为使用您的原始命令,一旦执行它,就会设置重定向,从而覆盖您的 image.jpg。然后,当 jpegtran 尝试读取它时,它会发现一个空文件。

how about:

jpegtran -copy none -optimize -outfile image.jpg image.jpg

I'm not a shell expert by any means, but I think that with your original command, as soon as it is executed, the redirect is set up which overwrites your image.jpg. Then when jpegtran tries to read it in it finds an empty file.

猫瑾少女 2024-11-06 19:17:40

我使用这个脚本。工作完美。我希望这有帮助。

https://gist.github.com/iimos/7424025

#! /bin/sh

EXTENSIONS="jpe?g"

if [ -z "$1" ]; then
    DIR="`pwd`"
else
    DIR="$1"
fi

# Optimize JPEG images
find "$DIR" -regextype posix-egrep -regex ".*\.($EXTENSIONS)\$" -type f | xargs -I{} jpegtran -optimize -progressive -outfile "{}.optimized" "{}"

# Rename xxx.jpg.optimized -> xxx.jpg
find "$DIR" -name '*.optimized' -print0 | while read -d 

用法 1:

optimize-images.sh /images/dir

用法 2:

cd /images/dir
optimize-images.sh 
\0' file; do chown $(stat -c "%U:%G" "${file%.optimized}") "$file" chmod $(stat -c "%a" "${file%.optimized}") "$file" mv -f "$file" "${file%.optimized}"; done

用法 1:

用法 2:

I use this script. Works flawlessly. I hope this helps.

https://gist.github.com/iimos/7424025

#! /bin/sh

EXTENSIONS="jpe?g"

if [ -z "$1" ]; then
    DIR="`pwd`"
else
    DIR="$1"
fi

# Optimize JPEG images
find "$DIR" -regextype posix-egrep -regex ".*\.($EXTENSIONS)\$" -type f | xargs -I{} jpegtran -optimize -progressive -outfile "{}.optimized" "{}"

# Rename xxx.jpg.optimized -> xxx.jpg
find "$DIR" -name '*.optimized' -print0 | while read -d 

Usage 1:

optimize-images.sh /images/dir

Usage 2:

cd /images/dir
optimize-images.sh 
\0' file; do chown $(stat -c "%U:%G" "${file%.optimized}") "$file" chmod $(stat -c "%a" "${file%.optimized}") "$file" mv -f "$file" "${file%.optimized}"; done

Usage 1:

Usage 2:

回首观望 2024-11-06 19:17:40
jpegtran -copy none -progressive -optimize -outfile filename filename

对于全面优化:-copy none 告诉 jpegtran 抑制源 jpeg 中存在的所有注释和其他多余包袱,progressive 生成渐进式 jpeg,-optimize 执行实际的优化,-outfile 设置输出文件名。最后一个参数是输入文件名:如果它们相同,您的文件就会就地优化。

编辑:您可能还想尝试一下 mozjpeg,根据这篇有关无损 jpeg 压缩工具的文章

jpegtran -copy none -progressive -optimize -outfile filename filename

For comprehensive optimization: -copy none tells jpegtran to suppress all comments and other excess baggage present in the source jpeg, progressive generates a progressive jpeg, -optimize performs the actual optimizations, and -outfile sets the output file name. The last parameter is the input file name: if they are the same, your file is optimized in place.

Edit: you might want to also try mozjpeg, according to this article on lossless jpeg compression tools http://blarg.co.uk/blog/comparison-of-jpeg-lossless-compression-tools

浮萍、无处依 2024-11-06 19:17:40

我用三行字做了:

jpegtran -optimize image.jpg > image.jpg-opt
cp image.jpg-opt image.jpg
rm image.jpg-opt

效果很好。

[编辑:] 这一次仅适用于一个文件。

I did it in three lines:

jpegtran -optimize image.jpg > image.jpg-opt
cp image.jpg-opt image.jpg
rm image.jpg-opt

Works well.

[edit:] This works only for one file at a time.

意中人 2024-11-06 19:17:40

如果不支持 -outfile,则另一种选择:

jpegtran -copy none -optimize image.jpg | sponge image.jpg

sponge更多实用程序

Another option if -outfile is not supported:

jpegtran -copy none -optimize image.jpg | sponge image.jpg

sponge is part of moreutils.

弃爱 2024-11-06 19:17:40

如果没有海绵,HDD 或 SSD 写入访问权限较低的另一种选择是使用 /dev/shm 保存临时文件,然后立即在本地覆盖它。

jpegtran -copy none -optimize image.jpg > /dev/shm/tmp.jpg &&  cat /dev/shm/tmp.jpg > ./image.jpg

这个概念可以很容易地适应任何脚本,也许需要注意临时文件名不相同,以避免同时运行多个实例时发生冲突。考虑一些独特的文件名模式生成方案可能很有趣,也许类似于 ${originalfilename}-jpgtrantmp-$$

Without sponge, another alternative with lower HDD or SSD writing access is to use the /dev/shm to save the temporary file and then overwrite it locally right away.

jpegtran -copy none -optimize image.jpg > /dev/shm/tmp.jpg &&  cat /dev/shm/tmp.jpg > ./image.jpg

The concept can be easily adapted into any script, perhaps with caveats about the temporary filename not being the same, in order to avoid collisions if there are multiple instances running at the same time. It's possibly interesting to think about some unique filename pattern generation scheme, perhaps something along the lines of ${originalfilename}-jpgtrantmp-$$.

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