Site icon Web Niraj

Batch Resize Images on Linux Using Mogrify (ImageMagick)

One problem I encountered recently was saving space on the web server which was being used to store user-uploaded photos. The current upload directory was approaching 8 GB in size – the limit for the Ubuntu Amazon AIM. So I had to find a quick solution to compressing the images (some of which had resolutions of 5 megapixels and higher).

I found that using mogrify (which somes with ImageMagick) was a quick and easy solution to resizing the images. I first had to Install ImageMagick on the server:

https://gist.github.com/niraj-shah/5327356

Then, I was ready get compressing. I wanted to resize all images larger than 1024 x 1014 pixels to that size.

https://gist.github.com/niraj-shah/5332600

The command explained:

  1. Resizes images larger than 1024×1024 to 1024×1024, while keeping the correct aspect ratio.
  2. Resize all images to 1024×1024. Images larger than this size will be scaled down, while smaller images will be scaled up.
  3. Resize all images with the ‘jpg’ extension

The -verbose flag will explain how the image was resized, giving the before and after dimensions.

https://gist.github.com/niraj-shah/5332686

mogrify can also be used for other manipulation operations, like converting a JPEG image to PNG. Typing in the commend without any flags will give you other operations you can perform, or you can read more about it here.

Exit mobile version