ImageMagick Examples --
Resize or Scaling (General Techniques)
Index
The foremost thing you should consider when specifying an image to resize is...
Do you really want to modify the image? Resizing will cause drastic changes to an image, and avoiding or minimizing unwanted 'artifacts' is of greatest importance. Perhaps just a slight Shave of the edges, or a more general Crop of the image will produce a better and more desirable outcome than a wholesale resize of the image. It generally will look better and the area left will be a perfect copy of the original. Because it is often better not to resize images...
As you can see a 64x64 square image was NOT produced by "
If you want you can force "
Only Shrink Larger Images ('
Another commonly used option is to restrict IM so that it will only shrink images to fit into the size given. Never enlarge. This is the '
This option is often very important for saving disk space for images, or in thumbnail generation, when enlarging images generally may not desirable as it tends to produce 'fuzzy' enlargements.
Only Enlarge Smaller Images ('
The inverse to the previous flag is '
Fill Area Flag ('
As of IM v6.3.8-3 IM now has a new geometry option flag '
As it stands this option does not seem very useful, but when combined with either a centered (or uncentered) "
Also "
Percentage Resize ('
Adding a percent sign, '%', to the "
Resize using a Pixel Area Count Limit ('
There is one final "
Note that the final image size is not limited to 64 pixels in height or width, but will have an area that is as close to (but smaller than) this size as IM can manage. That means one dimension will generally be slightly larger than 64 pixels and one will be slightly smaller. In some ways this is an ideal compromise for thumbnailing images. See Area Fit Thumbnail Size. You can also add the '>' flag to only shrink images that have more than the calculated number of pixels, while leaving images that are already smaller than that size.
Resize During Image Read
The resize operator can also be applied to images immediately after being read, before it is added to the current image sequence and the next image is read. That way a minimal amount of memory is needed to read in a lot of images. See Image Read Modifiers for more details. For example... The only problem with this technique is that no special resize options can be used, during the image read process.
Generally a single percentage value that is a multiple of 100% is used for the image enlargement so as to ensure all pixels are enlarged by the same amount, otherwise you can have different size pixel rows and columns producing large scale Moiré pattern.
The images above have been greatly enlarged
The above shows the result you get for 'purely averaged', vs, 'pixel mixed', vs, 'linear interpolation'. It also shows how Scale Operator is actually similar to that of a Triangle Filter, but only when doing very small downsizing of images. In other cases (strong downsize, upsizing, or exact interger sizing) it will produce results more like that of a Box Filter. Basically it generates something that is more like a mix between Box and Triangle filters depending on exactly how much the image is reduced in size. You see similar effects when enlarging.
Resizing Images
The most obvious and common way to change the size of an image is to resize or scale an image. The content of the image is then enlarged or more commonly shrunk to fit the desired size. But while the actual image pixels and colors are modified, the content represented by the image is essentially left unchanged. However resizing images can be a tricky matter. It can modify images in very detremental ways, and there is no 'best way' as what is best is subjective as to what you actually want out of the resize process. Because there is no 'best' or 'perfect' way, there are a lot of options that you may like to consider. IM has always tried to provide options to give you the maximum scope of control in image resizing. There are hundreds of posiblilties, styles, and techniques, and even resize experts are consantally trying to find new and different ways of changing an images size. Of course for most people, the normal default options is good enough as they were designed with general use in mind. The resize operator has been very carefully designed to try to produce a very good result for real world images. That is, not to say you can't use it for diagrams, or line drawings, though for that type of image you may need to use some of the more advanced options we'll look at later.The foremost thing you should consider when specifying an image to resize is...
Do you really want to modify the image? Resizing will cause drastic changes to an image, and avoiding or minimizing unwanted 'artifacts' is of greatest importance. Perhaps just a slight Shave of the edges, or a more general Crop of the image will produce a better and more desirable outcome than a wholesale resize of the image. It generally will look better and the area left will be a perfect copy of the original. Because it is often better not to resize images...
Resize will do nothing at all if the resized image is the same size.
The exception to this (there is always an exception) is if you actually specify a Resampling Filter using a "-filter
" setting. In that case the normal "do nothing if image isn't resized" is overridden and the filter will be applied. However, many filters (even the default filters) can blur images slightly. It is part of their nature. So normally this 'short-circuit' for a no-op resize a good thing. The argument to the resize operator is the area into which the image should be fitted. This area is not the final size of the image but the maximum size of the area into which the image is to be fitted. That is, because IM tries to preserve the aspect ratio of the image more than the final (unless a '!' flag is given), but at least one (if not both) of the final dimensions should match the argument given image. So let me be clear...
Resize will fit the image into the requested size.
It does NOT fill, the requested box size.
Aspect ratio is preserved basically so that any circles in the input image will remain a circle in the output image. That is, the image will not be squashed or squeezed, only resized, unless you tell it otherwise. For example, here I attempt to fit two source images, one larger image and one smaller image into a square box 64x64 pixels in size.
It does NOT fill, the requested box size.
magick dragon_sm.gif -resize 64x64 resize_dragon.gif magick terminal_sm.gif -resize 64x64 resize_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](resize_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](resize_terminal.gif)
-resize
". In fact the images were only enlarged or reduced just enough so as to best fit into the given size. Ignore Aspect Ratio ('!
' flag)If you want you can force "
-resize
" to ignore the aspect ratio and distort the image so it always generates an image exactly the size specified. This is done by adding the character '!
' to the size. Unfortunately this character is also sometimes used for special purposes by various UNIX command line shells. So you may have to escape the character somehow to preserve it.
magick dragon_sm.gif -resize 64x64\! exact_dragon.gif magick terminal.gif -resize 64x64\! exact_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](exact_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](exact_terminal.gif)
>
' flag)Another commonly used option is to restrict IM so that it will only shrink images to fit into the size given. Never enlarge. This is the '
>
' resize option. Think of it only applying the resize to images 'greater than' the size given (its a little counter intuitive).
magick dragon_sm.gif -resize 64x64\> shrink_dragon.gif magick terminal.gif -resize 64x64\> shrink_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](shrink_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](shrink_terminal.gif)
![]() ![]() |
The Only Shrink Flag ('> ' flag) is a special character in both UNIX Shell and in Window batch scripts, and you will need to escape that character (using backslash '\> ' in shell, and '^> ' in windows batch). It is also special in and HTML web pages, so PHP scripts also may need some special handling. |
<
' flag)The inverse to the previous flag is '
<
', which will only enlarges images that are smaller than the given size, is rarely used. The most notable use is with an argument such as '1x1<
'. This resize argument will never actually resize any image. In other words it's a no-op, which will allow you to short circuit a resize operation in programs and scripts which always uses "-resize
". Other than that you probably do not actually want to use this feature. One such example of using this 'short circuit' argument is for the "-geometry
" setting of "magick montage
". See Montage and Geometry, caution needed for more details.
![]() ![]() |
The Only Enlarge Flag ('< ' flag) is a special character in both UNIX Shell and in Window batch scripts, and you will need to escape that character (using backslash '\< ' in shell, and '^< ' in windows batch). It is also special in and HTML web pages, so PHP scripts also may need some special handling. |
^
' flag)As of IM v6.3.8-3 IM now has a new geometry option flag '
^
' which is used to resize the image based on the smallest fitting dimension. That is, the image is resized to completely fill (and even overflow) the pixel area given.
magick dragon_sm.gif -resize 64x64^ fill_dragon.gif magick terminal.gif -resize 64x64^ fill_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](fill_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](fill_terminal.gif)
-crop
" or "-extent
" to remove the excess parts of the image, you can fit the image so as to completely fill the area specified. Both the resize and the final image size arguments should be the same values. Though the "-crop
" is most logical, it may require an extra "+repage
" to remove virtual canvas layering information. The "-extent
" does not require this cleanup, but still allows the use of "-gravity
" for positioning. See Cutting and Bordering for more information.
magick dragon_sm.gif -resize 64x64^ \ -gravity center -extent 64x64 fill_crop_dragon.gif magick terminal.gif -resize 64x64^ \ -gravity center -extent 64x64 fill_crop_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](fill_crop_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](fill_crop_terminal.gif)
-extent
" can be used to pad out images that use the normal resize (with a "-background
" color setting). See Thumbnails, Fit to a Given Space Summary, for more on this type of operation.
![]() ![]() |
Remember this requires IM v6.3.8-3 or greater to make use of it. Otherwise use the older Resizing to Fill a Given Space technique below. |
![]() ![]() |
The Fill Area Flag ('^ ' flag) is a special character in Window batch scripts and you will need to escape that character by doubling it. For example '^^ ', or it will not work. See Windows Batch Scripting for this and other windowing particularities. |
%
' flag)Adding a percent sign, '%', to the "
-resize
" argument causes resize to scale the image by the amount specified.
Be warned however that the final pixel size of the image will be rounded to the nearest integer. That is, you will not generate partial pixels along the edge of the image! As a result the actual scale may not exactly match the scaling factor you provide, and may even be slightly different for X and Y directions, but it will be very close. (See Resize using Distort below).
![]() ![]() |
If you really want to resize image such that the final size looks like it has a partial pixel size differences, you can use the General Distortion Operator and specifically the Scale-Rotation-Translate (see Distort Resizing below). |
![]() ![]() |
The Percentage Resize Flag ('% ' flag) is a special character in Window batch scripts and you will need to escape that character by doubling it. For example '%% ', or it will not work. See Windows Batch Scripting for this and other windowing particularities. |
![]() ![]() |
All these 'flag' options '! ', '< ', '> ', '^ ', '% ', and '@ ' are just on/off switches for the "-resize " operator. Just the character's presence (or absence) in the resize argument is what matters, not their position. They can appear at the start or end of the argument, or before or after individual numbers (though not in the middle of a number).That is, ' %50 ' has exactly the same effect as '50% ' though the latter is preferred for readability. Also '50%x30 ' actually means '50%x30% ' and NOT 50% width and 30 pixel high as you might think.This is the case for all IM arguments using a 'geometry' style ('WxH' or '+X+Y') of argument. However offsets such as '+X+Y' are never treated as a percentage. |
@
' flag)There is one final "
-resize
" option flag. The "at" symbol '@
', will resize an image to contain no more than the given number of pixels. This can be used for example to make a collection of images of all different sizes roughly the same size. For example, here we resize both our images to a rough 64x64 size, or 4096 pixels in size.
magick dragon_sm.gif -resize 4096@ pixel_dragon.gif magick terminal.gif -resize 4096@ pixel_terminal.gif |
![[IM Output]](dragon_sm.gif)

![[IM Output]](pixel_dragon.gif)

![[IM Output]](../images/terminal.gif)

![[IM Output]](pixel_terminal.gif)
![]() ![]() |
Unfortunatally the '<', enlarge smaller images, flag is currently ignored when using 'Area Resize'. |
The resize operator can also be applied to images immediately after being read, before it is added to the current image sequence and the next image is read. That way a minimal amount of memory is needed to read in a lot of images. See Image Read Modifiers for more details. For example... The only problem with this technique is that no special resize options can be used, during the image read process.
![]() ![]() |
Resize and transparency posed a problem for ImageMagick before v6.2.4, producing a black halo effect around light colored objects on transparency. This was researched and finally fixed from that version onward. For more detail of this old bug see Resize Halo Bug |
Other Resize Operators
Geometry - Resize just the last image
Geometry is a very special option. The operator behaves slightly differently in every IM command, and often in special and magical ways. The reasons for this is mostly due to legacy use and should be avoided if at all possible. First, in "magick display
" it is used to size and position the window of the image being displayed. This was its original usage and meaning when IM was first started. It was from this that its other 'resize' capabilities came about. For "montage" "-geometry
" is a setting that is saved until all the arguments have been read in. At this point it then defines the final tile (cell) size (or leaves it up to "magick montage
" to work out) while the position arguments are used to specify the space surrounding the tile cells. See Montage Control Settings. In "composite
", "-geometry
" is also saved until the end of arguments have been reached. Then it is used to resize and position the overlay image (the first image given) before it is overlaid onto the background image (the second image). For example see Composite Multiple Images. As you can see it is used as a 'setting' in most IM commands, but in "magick
" "-geometry
" is both a special image resizing operator and a positioning setting. What it does is to "-resize
" just the last image in the current image sequence. This is the only image processing operator that is designed specifically to effect just the one image (the last one), in the current image sequence. To complicate this special option further, the positional parts of the "-geometry
" option is saved by "magick
" command, just as it is in "composite
". That is, any position is preserved for later use by the "-composite
", to position the 'overlay' image, (the second last image in the current image sequence) over the 'background' image (the first image in the image sequence). For this reason, you should limit the use of "-geometry
" in "magick
" commands to just before a "-composite
" or "-layers composite
" operations. To summarize, this operator is only really useful after reading or creating a second image, just before you perform some type of Alpha Composition to process with those images. For practical examples of using "-geometry
" to resize/position images see Compositing Multiple Images.
Thumbnail - Resize with profile stripping
The "-thumbnail" operator is a variation of "-resize
" designed specifically for shrinking very very large images to small thumbnails. First it uses "-strip
" to remove all profile and other fluff from the image. It then uses "-sample
" to shrink the image down to 5 times the final height. Finally it does a normal "-resize
" to reduce the image to its final size. All this is to basically speed up thumbnail generation from very large files. However for thumbnails of JPEG images, you can limit the size of the image read in from disk using the special option "-define jpeg:size=
{size}" setting. See Reading JPEG Images for more details. As such this speed improvment is rarely needed for JPEG in thumbnail generation, though profile stripping is still very important. For other image formats, such as TIFF, both profile stripping and speed improvement is still vitally important. As such it is still the recommended way to resize images for thumbnail creation.
![]() ![]() |
Before IM v6.5.4-7 the "-thumbnail " would strip ALL profiles from the image, including the ICC color profiles. From this version onward the color profiles will be preserved. If the color profile is not wanted then "-strip " all profiles. |
Resample - Changing an image's resolution
Just as in the previous alternative resize operators, "-resample" is also a simple wrapper around the normal "-resize
" operator. Its purpose however is to adjust the number of pixels in an image so that when displayed at the given Resolution or Density the image will still look the same size in real world terms. That is, the given image is enlarged or shrunk, in terms of the number of pixels, while the image size in real world units will remain the same. It is meant to be used for images that were read in from, or will be written out to, a program or device of a particular resolution or density. This is especially important for adjusting an image to fit a specific hardware output device, whether it is a display, or printer, or a postscript or PDF image format of a specific resolution. Just remember the real world size of the image does not change, only its resolution and of course the number of pixels used to represent the image. For example, suppose you had an image that you scanned at a 300dpi (dots per inch). The image was saved with this resolution (density) or when you read it into IM, you specified it as a 300dpi image (using "-density
"). Now you decide to display it on a screen that has a resolution of 90dpi, so you do a "-resample 90
". IM will now resize the image by 90/300 or to 30% of the images original size and set the images new density to 90dpi. The image is now smaller in terms of the number of pixels used, but if displayed on a 90dpi display will appear at the same physical size as the original image you scanned. That is, it now has a resolution appropriate for a 90dpi display, so it will be displayed to the user at its original real world size. A "-units
" setting (with arguments 'PixelsPerInch
' or 'PixelsPerCentimeter
') may be required in some situations to get this operator to work correctly. This setting can also be important for output to Postscript and PDF image file formats. Note that only a small number of image file formats (such as JPEG, PNG, and TIFF) are capable of storing the image resolution or density with the image data. For formats which do not support an image resolution, or which are multi-resolution (vector based) image formats, the original resolution of the image must be specified via the "-density
" attribute (see Density Image Meta-data) before being read in. If no density attribute has been set IM will assume it has a default density of 72dpi. Setting the density AFTER reading such an image will only affect its output resolution, and not affect its final size in terms of pixels.
Scale - Minify with pixel averaging
The "-scale" resize operator is a simplified, faster form of the resize command. When enlarging an image, the pixels in the image are replicated to form a large rectangular blocks of color. Which is great for showing a clean unblurred magnification of an image. For example, here is a magnified view of one of the built-in tile patterns...
|
![]() |
-scale
" tends to look overlay sharp, with aliasing ('staircase') effects along sharp edges. The pixel averaging of "-scale
" allows it to generate 'pixelated' images. You basically reduce the size of the image to average the pixels, then enlarge again back to the image's original size.
You can use a mask to combine the above pixelated image with the original image, so as to 'hide' a much smaller 'naughty' bit present in the original image. See the Protect Someones Anonymity example for a demonstration of using this technique. The algorithm is also designed to loop over rows of pixels then columns, which is inverted to that of "-resize
". This may allow "-scale
" to handle a "mpc:
" disk cached image better.
![]() ![]() |
Up until IM v6.4.7 the "-scale " still contained the old Resize Halo Bug. |
Scale Internals (pixel mixing)...
In many ways the Scale Operator is similar to the normal Resize Operator, but using a 'Box
' Resampling Filter. However it actually uses a completely different algorithm that is slightly more accurate in its results than that produced by a Box Filter. The way the Box Filter works is to simply average any pixel (sample) that falls within the filters 'support window' (See Filter Support Expert Control. This means that when shrinking an image by a very small amount, a Box filtered Resize will only produce either exact pixel values, or an perfectly averaged pixel value. The Scale Operator however uses a different algorithm that is known (for want of a better name) Pixel Mixing. Rather than generate the colors based on a 'average of pixels', within the 'support window' it uses a more accurite 'area of pixels' within the support window. For example, here I take 'checker board' pixel pattern, and shrink it by 2 pixels, comparing result of Scale against resizing using very simple Box & Triangle Filters.
magick -size 10x10 pattern:gray50 checks.gif magick checks.gif -filter box -resize 8x8 checks_box.gif magick checks.gif -scale 8x8 checks_scale.gif magick checks.gif -filter triangle -resize 8x8 checks_triangle.gif |
![]() 10 pixel 'hash' |
![]() |
![]() Box Filtered Resize |
![]() Pixel Mixing Scale |
![]() Triangle Filter Resize |