2014-12-28 109 views
0

我有数以百计的图像,其宽度和高度可能会有所不同。我想调整所有的人都用这个要求:使用imagemagick调整图像大小

  1. 让他们小,所以最大的侧应该得到2560px或最短的大小应该是1600像素。现在我正在使用此代码:

    for file in ls;转换$ file -resize“2560>”new- $文件;完成

    for file in ls;做转换$文件调整大小“1600 ^>”新建 - $文件;完成

  2. 决不最大方应小于2560或最短的尺寸小于1600像素

  3. 这将是真棒裁剪多余所以我的最终图像应该是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,然后作物,我拿2560×1600,我想1600x2560。

+0

这对我来说不是很清楚你想要做什么。例如,如果您的图像是4000x3000,则可能会获得2560x1920或2133x1600。你想保持2560x1920并从顶部和底部剪下160像素以获得2560x1600,或者是否希望保留2133x1600并在左侧和右侧添加213和214像素的空白空间(黑色?),以便你以这种方式得到2560x1600? – Palo

+0

编辑我的问题。 – user2670996

+0

谢谢。你如何看待它“收获太多”?你是否裁剪了太多的原始图像?产生的图像不是2560x1600?它是从另一个维度收割还是不成比例? – Palo

回答

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