Web Niraj
  • Facebook
  • Flickr
  • Github
  • Instagram
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Batch Resize Images on Linux Using Mogrify (ImageMagick)

Batch Resize Images on Linux Using Mogrify (ImageMagick)

4

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:

sudo apt-get install imagemagick
view raw install_imagemagick.sh hosted with ❤ by GitHub

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

# Resize images larger than 1024x1024
sudo mogrify -verbose -resize '1024x1024>' image.jpg
# Resize all images to 1024x1024
sudo mogrify -verbose -resize '1024x1024' image.jpg
# Resize all JPEG images larger than 1024x1024
sudo mogrify -verbose -resize '1024x1024>' *.jpg
view raw mogrify.sh hosted with ❤ by GitHub

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.

$> sudo mogrify -verbose -resize '1024x1024>' image.jpg
image.jpg JPEG 2592x1944 2592x1944+0+0 8-bit DirectClass 2.395MB 0.110u 0:00.109
image.jpg JPEG 2592x1944=>1024x768 1024x768+0+0 8-bit DirectClass 500KB 0.960u 0:00.950
view raw mogrify_verbose.sh hosted with ❤ by GitHub

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.

CLI, Command Line, Linux, Server

4 comments on “Batch Resize Images on Linux Using Mogrify (ImageMagick)”

  1. Iván Yossi says:
    April 13, 2013 at 12:56 AM

    Could you share the command?

    Reply
  2. Iván Yossi says:
    April 13, 2013 at 1:00 AM

    Oh i see them now. the didn’t appear the first time

    Might be a good idea to add a resampler filter to keep quality

    Reply
  3. ssk2 says:
    May 28, 2013 at 1:39 PM

    Why does ‘mogrify’ need to be run as sudo? Seems like bad practice to me.

    Reply
    • Niraj Shah says:
      May 28, 2013 at 3:44 PM

      It doesn’t need to be. It may be required depending on file permissions (since in my case, the files were uploaded by the www-data user, and I was logged in as another).

      Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Subscribe for updates

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Flat File Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux Mac OS MongoDB NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins

© 2011-2026 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy