26 Oct 2015
Resizing Images With ImageMagick
(Full examples at bottom of page)
I was unable to find a good summary of this information on Google so I decided
to collect it an post it here. Most of what I found were askubuntu threads where
someone posted a specific form of the
mogrify command without
explaining the options and how things worked. Teach people to fish, damnit! I also
man pages lacking.
By the way, OSX users have it easy. The command
sips ships with your OS so
you can do
sips -Z 640 *.jpg to shrink every image with a dimension
larger than 640px while maintaining the aspect ratio.
An image modification package. Install on Ubuntu with
sudo apt-get install imagemagick
Resizing Commands Provided by ImageMagick
There are (at least) two. The command
mogrify edits images in place
while the command
convert saves a copy of the image. Their options
are nearly (exactly?) identical.
Image Geometry Parameter
mogrify -resize geometry <--- What is this?
This is a string that dictates the rules for how an image should be resized. For example you may want to are resize to an exact size, scale by a percentage, stretch the image, set the largest dimension to a certain size while maintaining the aspect ratio etc…
A detailed description of this parameter is given on the online documentation for ImageMagick. A detailed description is not given in the help output or the man page as far as I could find…
The basic form is
- Width and height are numbers of pixels.
- Modifier is one of
^, !, >, <, %.
The simple form
640x480 without a modifier tells
mogrify to change the
image so that it fits into a rectangle of this size while also keeping the same
aspect ratio. That is, it sets the image’s larger dimension equal to the
corresponding dimension in this geometry parameter. You can also leave out one
640 (width only)
x480 height only.
Now for the modifiers:
^, !, >, <. If you use a modifier you must include
both a height and a width.
^results in the opposite of the default behavior. That is, the image’s smaller dimension is set to the corresponding dimension from the geometry parameter while maintaining the aspect ratio.
!forces the image to have exactly the width and height specified, while ignoring the aspect ratio. Distorted images result from this command.
>means shrink an image with one dimension larger than the width or height from the geometry parameter. (Aspect ratio preserved)
<means enlarge an image with one dimension larger than the width or height from the geometry parameter. (Aspect ratio preserved)
And finally, percentages. The units of
height are changed to
percents if you include the
% modifier after a full
specification. You can also use any of theses other forms to the same effect:
width%xheight%. If you pass
75% alone, both dimensions are
scaled by 75%.
# Resize height to 480, aspect ratio preserved mogrify -resize x480 image.png # Resize to fit inside a 640x480 rectangle, aspect # ratio preserved mogrify -resize 640x480 image.png # Scale both dimensions by 75% mogrify -resize 75% image.png # Shrink an image with dimension(s) larger than # the parameters mogrify -resize 640x480> # Enlarge an image with dimension(s) smaller than # the parameters mogrify -resize 640x480>