Linux jpegtran 优化而不更改文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5579183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:33:40  来源:igfitidea点击:

jpegtran optimize without changing filename

linuximageimage-processingimage-manipulation

提问by David

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.

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

采纳答案by so12311

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.

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

回答by ZurabWeb

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.

[编辑:] 一次只对一个文件有效。

回答by imos

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

我用这个脚本。完美运行。我希望这有帮助。

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

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

#! /bin/sh

EXTENSIONS="jpe?g"

if [ -z "" ]; then
    DIR="`pwd`"
else
    DIR=""
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 $'
optimize-images.sh /images/dir
' 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:

用法一:

cd /images/dir
optimize-images.sh 

Usage 2:

用法2:

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

回答by Mauro Colella

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

For comprehensive optimization: -copy nonetells jpegtran to suppress all comments and other excess baggage present in the source jpeg, progressivegenerates a progressive jpeg, -optimizeperforms the actual optimizations, and -outfilesets the output file name. The last parameter is the input file name: if they are the same, your file is optimized in place.

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

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

编辑:您可能还想尝试 mozjpeg,根据这篇关于无损 jpeg 压缩工具的文章http://blarg.co.uk/blog/comparison-of-jpeg-lossless-compression-tools

回答by Hymanson Pauls

Another option if -outfileis not supported:

-outfile不支持的另一种选择:

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

spongeis part of moreutils.

spongemoreutils 的一部分。

回答by Jay Pegg

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.

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

##代码##

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-$$.

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