ImageMagick: How to resize proportionally with mogrify without a background

cj5 picture cj5 · Sep 15, 2012 · Viewed 26.5k times · Source

I was following this example http://cubiq.org/create-fixed-size-thumbnails-with-imagemagick, and it's exactly what I want to do with the image, with the exception of having the background leftovers (i.e. the white borders). Is there a way to do this, and possibly crop the white background out? Is there another way to do this? The re-size needs to be proportional, so I don't just want to set a width re-size limit or height limit, but proportionally re-size the image.

Answer

Kurt Pfeifle picture Kurt Pfeifle · Sep 15, 2012

The example you link to uses this command:

mogrify             \
  -resize 80x80     \
  -background white \
  -gravity center   \
  -extent 80x80     \
  -format jpg       \
  -quality 75       \
  -path thumbs      \
   *.jpg

First, mogrify is a bit dangerous. It manipulates your originals inline, and it overwrites the originals. If something goes wrong you have lost your originals, and are stuck with the wrong-gone results. In your case the -path thumbs however alleviates this danger, because makes sure the results will be written to sub directory thumbs

Another ImageMagick command, convert, can keep your originals and do the same manipulation as mogrify:

convert             \
   input.jpg        \
  -resize 80x80     \
  -background white \
  -gravity center   \
  -extent 80x80     \
  -quality 75       \
   thumbs/output.jpg

If want the same result, but just not the white canvas extensions (originally added to make the result a square 80x80 image), just leave away the -extent 80x80 parameter (the -background white and gravity center are superfluous too):

convert             \
   input.jpg        \
  -resize 80x80     \
  -quality 75       \
   thumbs/output.jpg

or

mogrify             \
  -resize 80x80     \
  -format jpg       \
  -quality 75       \
  -path thumbs      \
   *.jpg