我有数百张宽度和高度可能不同的图像。我想用这个要求调整所有这些:
将它们缩小,使最大边应为2560px,或最短尺寸为1600px。现在我使用这段代码:
for ls
中的文件;转换$ file -resize" 2560>"新 - $文件;完成
for ls
中的文件;转换$ file -resize" 1600 ^>"新 - $文件;完成
最大的一面不应小于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。
答案 0 :(得分:2)
我建议您使用这样的脚本来获取每个图像的尺寸。然后你可以根据图像大小实现你想要的任何逻辑 - 并且还避免解析ls
的输出,这通常被认为是一个坏主意。
#!/bin/bash
# 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
echo $f is square at ${w}x${h}
elif [ $h -gt $w ]; then
echo $f is taller than wide at ${w}x${h}
else
echo $f is wider than tall at ${w}x${h}
fi
done
<强>输出:强>
lena.png is square at 128x128
lena_fft_0.png is square at 128x128
lena_fft_1.png is square at 128x128
m.png is wider than tall at 274x195
1.png is taller than wide at 256x276
2.png is taller than wide at 256x276
答案 1 :(得分:0)
似乎需要一个脚本。 这是我之前评论中的解决方案:
#!/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