Resizing Images with ImageMagick with Height Precedence

Whenever I need to process images I quickly turn to ImageMagick. ImageMagick is a set of tools for image processing. The cool thing about it is that you can process many images, even recursively over a tree of directories, at the same time because everything is done from the command line. You can write scripts, or chain commands. One of the things that is often required is resizing images. As this post explains, it can be done in many different ways.

Yesterday I had to do such a task, but I had to make the images the same height. At first I didn’t know how to do it, but then I though of the simplest solution. To go thru a set of images, all on the same directory, and re-size them to be the same height you only need to execute the following on the command line:

find . -iname "*.jpg" | xargs -l -i convert -resize 10000x100\> {} out/{}

It is quite simple. Since we are specifying a huge width (10000), most images will just re-size to 100px tall. The \> tells convert to only re-size images that are greater than the measurements specified. This way we avoid making smaller images bigger to fit in the 10000 by 100 area.

The reason this works is because “Resize will fit the image into the requested size. It does NOT fill, the requested box size.”

This is a very simple solution, but I took me at least 10 minutes to get to it. I had 3 images, one was in landscape orientation, and the other two in portrait orientation. I wanted to re-size them to be 75px tall, so I originally did this:

find . -iname "*.jpg" | xargs -l -i convert -resize 75x75 {} out/{}

The portrait-oriented images would re-size well, and the height would be effectively 75, but the landscape would get a width of 75, and a height of less than 75. So, initially I thought I could write a scrip to detect the orientation of the image based on its width and height, then depending on that run the convert command setting different argument for the resize parameter so that all images would get a height of 75, but then I realized how stupid I was being by trying to solve a simple problem in a complex manner. That is when I realized that setting a very big width, all images would be forced to fit into the same height, thus rendering each as tall as each other.

In the process of arriving at the right solution I wanted to find out the difference between the resize and the scale parameters. That led me to this nice documentation on ImageMagick. I would no doubt recommend it to anyone interested on learning more about ImageMagick.

2 thoughts on “Resizing Images with ImageMagick with Height Precedence

  1. Yeah, I feel that ImageMagick is a great command line utility to create, edit, or convert images that is really very powerful especially for bitmap images. ImageMagick is very simple and powerful!

    • Yes, in fact, I just finished another project where image magic saved me a lot of time. I had to crop a bunch of images by size so they would fit on pre-stablished containers. Image magic made the job a piece of cake.

Comments are closed.