46


I have over 1000 images on different resolutions, (for example 1234x2122, 4400x5212 , etc) and I want to convert all of them to fixed 100x100 size, so.

  1. first I need to resize the images keeping proportions, and get 100xA or Ax100, where A > 100 (it depends width and height of image, for some images width > height, and for some images height > width).

  2. Crop this image to 100x100 from center

Is there a simple convert command, that I can use for all my images?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Gor
  • 2,808
  • 6
  • 25
  • 46
  • seems like what you want to do is similar to this http://www.imagemagick.org/discourse-server/viewtopic.php?t=18545 – phuclv May 18 '17 at 16:03

2 Answers2

72

You would use the area-fill (^) geometry modifier on the -resize operation to unify the down-scale. For cropping the center, -extent with -gravity Center will work.

convert input.jpg -resize 100x100^ \
                  -gravity Center  \
                  -extent 100x100  \
        output.jpg

Update

As Mark Setchell pointed out in the comments, the mogrify utility can be leveraged to batch convert items.

mogrify -path ./path/to/write/results/ \
        -resize 100x100^ \
        -gravity Center  \
        -extent 100x100  \
        ./path/to/source/files/*

reminder Reminder: Mogrify will overwrite original file with resulting image(s), unless you set the -path parameter.

Community
  • 1
  • 1
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Have my vote:-) Maybe mention `mogrify` if OP has 1000+ images? – Mark Setchell Sep 08 '15 at 22:07
  • @emcconville, is there way to accomplish this without loosing transparent background of an png? – tukusejssirs Jun 15 '19 at 07:28
  • I’ve found the solution ([src](https://makandracards.com/makandra/46467-imagemagick-how-to-auto-crop-and-or-resize-an-image-into-a-box)): `convert input.png -resize 100x100^ -gravity Center -background transparent -extent 100x100 output.png` – tukusejssirs Jun 15 '19 at 07:36
  • Is the answer exactly as Gor asked for? I.e., Gor asked for the central part without white space on either dimension, but reading the answer over and over, I have the feeling that the answer given as of Sep 8 '15 might have filled-blanks on either dimension. Am I right, anyone? – xpt Jul 24 '20 at 20:49
  • It doesn't work for me in 2022. The resulting image is not cropped around the centre (and it's sad that there are so few options for the rotation centre). I managed to rotate images in Pillow (Python), it allows for an arbitrary rotation centre. – Yaroslav Nikitenko Dec 11 '22 at 15:08
4

To keep the aspect ratio and don't fill anything (extent), rather crop from the longer edge which is out of aspect you could do

convert input.jpg -resize 100x100^ \
                  -gravity Center  \
                  -crop 100x100+0+0 +repage  \
        output.jpg

with option to crop more from one side if you like

DenisZ
  • 171
  • 1
  • 11