使用从中心开始适合的最大正方形裁切图像 [英] Cropping an image using the largest square that fits starting from the center
本文介绍了使用从中心开始适合的最大正方形裁切图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个尺寸为X*Y
的图像,称为D = min(X,Y)
,是否有一个Ubuntu命令可以裁剪一个以X/2,Y/2
为中心的尺寸为D
的正方形的图像?
Suppose I have an image of size X*Y
, calling D = min(X,Y)
, is there an Ubuntu command that allows cropping an image with a square of size D
centered at X/2,Y/2
?
我相信convert
可能有效,但是我不知道如何使用哪些标志来完成这项工作.
I believe convert
might work, but I can't figure how which flags I can use to do the job.
推荐答案
您可以将D
作为宽度和高度中的较小者,如下所示:
You can get D
as the lesser of the width and height like this:
D=$(convert input.jpg -format "%[fx:w<h?w:h]" info:)
然后像这样从中心裁剪该尺寸的正方形:
and then crop that size square from the centre like this:
convert input.jpg -gravity center -extent "${D}x${D}" result.jpg
因此,如果您以这张800x400的图片开头:
So, if you start with this 800x400 image:
您将获得此400x400的图片:
You'll get this 400x400 image:
这篇关于使用从中心开始适合的最大正方形裁切图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文