我有数百幅宽度和高度可能会变化的图像。我想通过以下要求来调整所有这些文件的大小:
使其更小,以便最大的一面应为2560px或最短的尺寸应为1600px。现在,我正在使用以下代码:
用于归档ls
; 转换$ file -resize“ 2560>” new- $ file; done
用于归档ls
; 转换$ file -resize“ 1600 ^>” new- $ file; done
绝不可以将最大边小于2560或将最短边小于1600px
裁剪多余的空间真是太棒了,所以我的最终图像应该是2560x1600(横向)或1600x2560(纵向)。
例如,如果我的图像是4000x3000,则可能会得到2560x1920或2133x1600。我想保留2560x1920并从其顶部和底部裁切160像素,以获得2560x1600。
我现在使用的代码是这样的:
for i in `ls`; do convert $i -resize '2560x1600^' -gravity center -crop '2560x1600+0+0' new-$i; done
但是,如果我的图像是3000x4000(人像模式),我可能会得到2560x3413,然后它会裁剪直到我得到2560x1600,而我想要1600x2560。
似乎需要一个脚本。这是基于先前评论的我的解决方案:
#!/bin/bash
# Variables for resize process:
shortest=1600;
longest=2560;
# Ignore case, and suppress errors if no files
shopt -s nullglob
shopt -s nocaseglob
# Process all image files
for f in *.gif *.png *.jpg; do
# Get image's width and height, in one go
read w h < <(identify -format "%w %h" "$f")
if [ $w -eq $h ]; then
convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
elif [ $h -gt $w ]; then
convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
else
convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
fi
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句