0

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?

I believe convert might work, but I can't figure how which flags I can use to do the job.

kvantour
  • 25,269
  • 4
  • 47
  • 72
user8469759
  • 2,522
  • 6
  • 26
  • 50
  • Possible duplicate of [ImageMagick: how to minimally crop an image to a certain aspect ratio?](https://stackoverflow.com/questions/21262466/imagemagick-how-to-minimally-crop-an-image-to-a-certain-aspect-ratio) – Samuel Kirschner Dec 06 '18 at 19:40
  • Should be the same as [crop an image to a certain aspect ratio](https://stackoverflow.com/questions/21262466/imagemagick-how-to-minimally-crop-an-image-to-a-certain-aspect-ratio) with the ratio being 1:1 – Samuel Kirschner Dec 06 '18 at 19:41

1 Answers1

3

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

So, if you start with this 800x400 image:

enter image description here

You'll get this 400x400 image:

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432