【问题标题】:How to crop to at least 16:9 with imagemagick如何使用 imagemagick 裁剪到至少 16:9
【发布时间】:2016-02-11 00:50:27
【问题描述】:

我有未知大小和比例的图像。我希望采用居中的 16:9 裁剪。

如果所有图像都是 4:3,那么很容易:9/16 ÷ 3/4 = 0.75,所以我只需将高度设置为原始图像的 75%,如下所示:

convert photo.jpg -gravity center -crop '100%x75%' +repage photo16x9.jpg

但是,如果照片已经是 16:9(或更宽),我不想裁剪它,如果它比 16:9“高”,我只想裁剪必要的数量实现 16:9 的裁剪。

如果我想缩放到已知的宽度或高度 (example question for that),这也会更容易。但我希望尽可能多地保留原始图像数据。

因此,我正在寻找一种方法将高度裁剪为图像宽度的一个因子,并带有一个截断。

我希望这可能会工作

convert photo.jpg -gravity center \
  -crop '100%x[fx:9/16*w]' +repage photo16x9.jpg

但是很遗憾,-crop 参数中似乎不允许使用 [fx:...]

我发现了一些我一生都无法理解的地方(!)也失败了,但我会在这里列出它以展示研究成果!

convert photo.jpg -set option:distort:viewport \
  '[fx: w]x[fx: w*9/16 ]+0+0' -filter point \
  -distort SRT 0 +repage photo16x9.jpg

(我意识到上面的尝试都没有涵盖图像已经比 16:9 宽的情况,但是如果 [fx:...] 事情有效,我可以通过使用三元运算符来实现,所以我保持示例简单。)

【问题讨论】:

    标签: imagemagick imagemagick-convert


    【解决方案1】:

    也许您可以只计算纵横比并使用它来做出决定。创建两张测试图像,一张 15x9 和一张 17x9:

    convert -size 15x9 xc:black 15x9.png
    convert -size 17x9 xc:black 17x9.png
    

    现在让 ImageMagick 告诉你宽高比是大于 16:9 还是小于或等于 16:9:

    convert 15x9.png -format "%[fx:(w/h)>16/9]" info:
    0
    
    convert 17x9.png -format "%[fx:(w/h)>16/9]" info:
    1
    

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2018-03-06
      • 2012-12-11
      • 2016-12-03
      • 1970-01-01
      • 2013-08-06
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多