ImageMagick:如何将图像最小化为特定宽高比?

问题描述 投票:21回答:5

使用imagemagick,我想以最小的方式裁剪图像,以便它适合给定的宽高比。

示例:给出3038 x 2014 px的图像,我想将其裁剪为3:2的宽高比。然后生成的图像将是3021 x 2014 px,从原始图像的中心裁剪出来。

所以寻找像convert in.jpg -gravity center -crop_to_aspect_ratio 3:2 out.jpg这样的命令。

imagemagick crop aspect-ratio
5个回答
18
投票

1. Specific target resolution

如果你最终的目标是具有一定的分辨率(例如1920x1080),那么使用-geometry,circumflex / hat / roof / house符号(^)和-crop很容易:

convert in.jpg -geometry 1920x1080^ -gravity center -crop 1920x1080+0+0 out.jpg

循环遍历多个jpg文件:

for i in *jpg
  do convert "$i" -geometry 1920x1080^ -gravity center -crop 1920x1080+0+0 out-"$i"
done

2. Aspect ratio crop only

如果你想避免缩放,你必须计算Imagemagick之外的裁剪边的新长度。这涉及更多:

aw=16 #desired aspect ratio width...
ah=9 #and height
in="in.jpg"
out="out.jpg"

wid=`convert "$in" -format "%[w]" info:`
hei=`convert "$in" -format "%[h]" info:`

tarar=`echo $aw/$ah | bc -l`
imgar=`convert "$in" -format "%[fx:w/h]" info:`

if (( $(bc <<< "$tarar > $imgar") ))
then
  nhei=`echo $wid/$tarar | bc`
  convert "$in" -gravity center -crop ${wid}x${nhei}+0+0 "$out"
elif (( $(bc <<< "$tarar < $imgar") ))
then
  nwid=`echo $hei*$tarar | bc`
  convert "$in" -gravity center -crop ${nwid}x${hei}+0+0 "$out"
else
  cp "$in" "$out"
fi

我在示例中使用的是16:9,期望它对大多数读者来说比3:2更有用。改变溶液1中1920x1080的出现或溶液2中的aw / ah变量,以获得所需的纵横比。


11
投票

Imagemagick的最新版本(自6.9.9-34开始)具有方面裁剪。所以你可以这样做:

输入:

enter image description here

convert barn.jpg -gravity center -crop 3:2 +repage barn_crop_3to2.png

enter image description here

输出为400x267 + 0 + 0。但请注意,需要+ repage才能删除400x299 + 0 + 16的虚拟画布,因为PNG输出支持虚拟画布。 JPG输出不需要+ repage,因为它不支持虚拟画布。


3
投票

随着ImageMagick 7的出现,您可以使用FX表达式在单个命令中根据宽高比实现最大图像尺寸。

唯一的技巧是你需要在同一个命令的四个不同的地方输入所需的方面,所以我发现最容易为那个位做一个变量。方面可以是十进制数或分数作为fx表达式可以解析的字符串。

aspect="16/9"

magick input.png -gravity center \
    -extent  "%[fx:w/h>=$aspect?h*$aspect:w]x" \
    -extent "x%[fx:w/h<=$aspect?w/$aspect:h]" \
    output.png

一旦方面正确,您可以使用-extent跟踪两个-resize操作,将完成的图像输出到您的输出大小。上面的示例使其保持与输入图像一样大。


1
投票

您需要计算出所需的尺寸,然后进行裁剪。这是一个函数,给定图像的widthheight加上所需的纵横比aspect_xaspect_y,将输出一个可以与Imagemagick一起使用的裁剪字符串。

def aspect(width, height, aspect_x, aspect_y)

  old_ratio = width.to_f / height
  new_ratio = aspect_x.to_f / aspect_y

  return if old_ratio == new_ratio

  if new_ratio > old_ratio
    height = (width / new_ratio).to_i # same width, shorter height
  else
    width = (height * new_ratio).to_i # shorter width, same height
  end

  "#{width}x#{height}#" # the hash mark gives centre-gravity

end

我在使用Dragonfly Gem的应用程序中使用类似的东西。


0
投票

我需要使用A4(1x1.414)纸张纵横比垂直分割非常长的图像。所以我提出了以下解决方案。假设图像文件名是ch1.jpg:

convert -crop $(identify -format "%w" ch1.jpg)x$(printf "%.0f" $(echo $(identify -format "%w" ch1.jpg) \* 1.414|bc)) +repage ch1.jpg ch1.jpg
© www.soinside.com 2019 - 2024. All rights reserved.