Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick: How to resize proportionally with mogrify without a background

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.

like image 637
cj5 Avatar asked Sep 14 '12 23:09

cj5


3 Answers

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
like image 114
Kurt Pfeifle Avatar answered Oct 19 '22 14:10

Kurt Pfeifle


I know this is an old thread, but by using the -write flag with the -set flag, one can write to files in the same directory without overwriting the original files:

mogrify -resize 80x80 \
-set filename:name "%t_small.%e" \
-write "%[filename:name]" \
*.jpg

As noted at http://imagemagick.org/script/escape.php, %t is the filename without extension and %e is the extension. So the output of image.jpg would be a thumbnail image_small.jpg.

like image 40
Nielsvh Avatar answered Oct 19 '22 16:10

Nielsvh


This is the command I use each time I want to batch resized everything to 1920x and keep aspect ratio.

mogrify -path . -resize 1920x1920 -format "_resized.jpg" -quality 70 *.jpg

like image 6
Layke Avatar answered Oct 19 '22 16:10

Layke