如何使用 Bash 单行代码快速批量调整大小、压缩和转换图像
在撰写本文时,我的 Hugo 站点持续部署工作流程的一部分是处理 210 张图像。
这是我的一句话:
find public/ -not -path "*/static/*" \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) -print0 | xargs -0 -P8 -n2 mogrify -strip -thumbnail '1000>' -format jpg
我用它来find
只针对特定目录中的特定图像文件格式。使用mogrify
ImageMagick 的一部分,我只会调整大于特定尺寸的图像大小,对其进行压缩,并去除元数据。我添加format
标志位来创建图像的 jpg 副本。
以下再次是一行代码(为了便于阅读,已拆分):
# Look in the public/ directory
find public/ \
# Ignore directories called "static" regardless of location
-not -path "*/static/*" \
# Print the file paths of all files ending with any of these extensions
\( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) -print0 \
# Pipe the file paths to xargs and use 8 parallel workers to process 2 arguments
| xargs -0 -P8 -n2 \
# Tell mogrify to strip metadata, and...
mogrify -strip \
# ...compress and resize any images larger than the target size (1000px in either dimension)
-thumbnail '1000>' \
# Convert the files to jpg format
-format jpg
就是这样。这就是帖子。
文章来源:https://dev.to/victoria/how-to-quickly-batch-resize-compress-and-convert-images-with-a-bash-one-liner-a6p