ImageMagick:如何将图像最小化到一定的纵横比? [英] ImageMagick: how to minimally crop an image to a certain aspect ratio?

查看:28
本文介绍了ImageMagick:如何将图像最小化到一定的纵横比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 imagemagick,我想以最小的方式裁剪图像,使其适合给定的纵横比.

示例:给定一张 3038 x 2014 像素的图像,我想将其裁剪为 3:2 的纵横比.生成的图像将是 3021 x 2014 像素,从原始图像的中心裁剪.

所以寻找一个类似于 convert in.jpg -gravity center -crop_to_aspect_ratio 3:2 out.jpg 的命令.

解决方案

Imagemagick 7.0.7.22 及以上

-crop 3:2

警告/提醒:如果你不使用-gravity center,你会得到两个输出文件:

PNG

正如 fmw42 指出的,PNG 文件存储虚拟画布大小.推荐使用 +repage.

magick convert in.png -gravity center -crop 3:2 +repage out+repage.png

GIMP、IrfanView、Chrome 和 Windows Explorer 没有显示出任何区别,但 Imagemagick 知道:

magick 识别出来*pngout_stndrd.png PNG 252x168 314x168+31+0 8 位 sRGB 78557B 0.000u 0:00.000out+repage.png PNG 252x168 252x168+0+0 8 位 sRGB 78529B 0.000u 0:00.000

Imagemagick 6.9.9-34 及以上

JPG

convert in.jpg -gravity center -crop 3:2 out.jpg

PNG

convert in. -gravity center -crop 3:2 +repage out.png

Imagemagick 6.9.9-33/7.0.7.21 及以下

注意:您需要在 v7 的任何 convert 之前添加 magick.

1.特定目标分辨率

如果您最终的目标是获得特定分辨率(例如 1920x1080),那么使用 -geometry、抑扬符/帽子/屋顶/房屋符号(^) 和 -crop:

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

要循环多个 jpg 文件:

for i in *jpg转换$i"-geometry 1920x1080^ -gravity center -crop 1920x1080+0+0 out-$i"完毕

2.仅纵横比裁剪

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

aw=16 #所需的纵横比宽度...ah=9 #和高度in=in.jpg"out="out.jpg";wid=`转换$in"-格式%[w]"信息:`hei=`转换$in";-格式%[h]"信息:`tarar=`echo $aw/$ah |bc -l`imgar=`convert "$in";-格式%[fx:w/h]"信息:`if (( $(bc <<<<"$tarar > $imgar") ))然后nhei=`echo $wid/$tarar |公元前`转换$in"-重力中心 -crop ${wid}x${nhei}+0+0 "$out";elif (( $(bc <<< "$tarar < $imgar") ))然后nwid=`echo $hei*$tarar |公元前`转换$in"-重力中心 -crop ${nwid}x${hei}+0+0 "$out";别的cp$in"$出"菲

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

照片来源:Anders Krusberg/Peabody 奖

With imagemagick, I'd like to crop an image, in a minimal fashion, so that it fits a given aspect ratio.

Example: given an image of, say, 3038 x 2014 px, I want to crop it to have a 3:2 aspect ratio. The resulting image would then be 3021 x 2014 px, cropped from the, say, center of the original image.

So looking for a command looking something like convert in.jpg -gravity center -crop_to_aspect_ratio 3:2 out.jpg.

解决方案

Imagemagick 7.0.7.22 and above

-crop 3:2 works since January 6th, 2018.

JPG

magick convert in.jpg -gravity center -crop 3:2 out.jpg

Warning/reminder: if you don't use -gravity center, you will get two output files:

PNG

As fmw42 points out, PNG files store the virtual canvas size. +repage is recommended.

magick convert in.png -gravity center -crop 3:2 +repage out+repage.png

GIMP, IrfanView, Chrome and Windows Explorer don't show any difference, but Imagemagick knows:

magick identify out*png
out_stndrd.png PNG 252x168 314x168+31+0 8-bit sRGB 78557B 0.000u 0:00.000
out+repage.png PNG 252x168 252x168+0+0 8-bit sRGB 78529B 0.000u 0:00.000

Imagemagick 6.9.9-34 and above

JPG

convert in.jpg -gravity center -crop 3:2 out.jpg

PNG

convert in. -gravity center -crop 3:2 +repage out.png

Imagemagick 6.9.9-33 / 7.0.7.21 and below

Note: you need to add magick before any convert for v7.

1. Specific target resolution

If your goal at the end is to have a certain resolution (for example 1920x1080) then it's easy, using -geometry, the circumflex/hat/roof/house symbol (^) and -crop:

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

To loop over multiple jpg files:

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

2. Aspect ratio crop only

If you want to avoid scaling, you have to calculate the new length of the cropped side outside of Imagemagick. This is more involved:

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

I'm using 16:9 in the examples, expecting it to be more useful than 3:2 to most readers. Change both occurrences of 1920x1080 in solution 1 or the aw/ah variables in solution 2 to get your desired aspect ratio.

Photo credit: Anders Krusberg / Peabody Awards

这篇关于ImageMagick:如何将图像最小化到一定的纵横比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆