How to Easily Resize, Convert and Modify Images from the Linux Command Line

I often worked with images while preparing a technical article for 2DayGeek.

I took a lot of screen shots as part of the article preparation and will edit them before adding them to my blog article.

Mostly i use the compression option to reduce the actual size of the image to load them quickly on the web.

There are many tools for this purpose, and we are going to discuss about ImageMagick tool.

What’s ImageMagick?

ImageMagick is a free and open source, feature-rich, command-line based image manipulation tool.

It used to create, edit, compose, or convert bitmap images.

It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, PDF, SVG and etc,.

It can resize, mirror, rotate, transform images, adjust image colors, apply various special effects, etc,.

It supports batch process, which allow you to processes all images at once.

How to Install ImageMagick?

The ImageMagick package is included in the official repository of most Linux distributions. Use the distribution package manager to install it.

Make a note: Make sure you already have “Development Tools” installed on your Linux system as a prerequisite for this.

For RHEL/CentOS 6/7 systems, use the yum command to install ImageMagick.

$ sudo yum install -y ImageMagick ImageMagick-devel

For RHEL/CentOS 8 and Fedora systems, use the dnf command to install ImageMagick.

$ sudo dnf install -y ImageMagick ImageMagick-devel

For Debian/Ubuntu systems, use the apt command or apt-get command to install ImageMagick.

$ sudo apt-get update
$ sudo apt-get install imagemagick

For openSUSE systems, use the zypper command to install ImageMagick.

$ sudo zypper install -y ImageMagick

Run the following commands to find out the installed version of ImageMagick.

$ identify -version
or
$ convert -version

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib

Convert Between Image Formats

The convert command is one of the most used command. It converts images from one image format to another.

The below command converts the image “sample.jpg” from JPEG format to PNG.

$ convert sample.jpg sample.png

If the image size is too large and you want to reduce the image size (in MBs or KBs) then specify a compression level when converting them. It doesn’t reduce the pixels.

$ convert sample.jpg -quality 55 sample-1.jpg

This Conversion has reduced the size of the image, but not the image pixels.

Before Conversion:

$ identify sample.jpg
sample.jpg JPEG 3138x2012 3138x2012+0+0 8-bit sRGB 864429B 0.000u 0:00.000

After Conversion:

$ identify sample-1.jpg
sample-1.jpg JPEG 3138x2012 3138x2012+0+0 8-bit sRGB 244985B 0.000u 0:00.000

How to Resize Images

The convert command can quickly resize the given image. This can be done by specifying the new width and height of the image in pixels.

To do so, you need to know the dimensions of your original image. The following command can print the dimensions of a given image file.

$ identify sample.jpg
sample.jpg JPEG 3138x2012 3138x2012+0+0 8-bit sRGB 864429B 0.000u 0:00.000
or
$ identify -format "%wx%h" sample.jpg
3138x2012

Now, run the following command to resize the image. In this example, I will reduce the size of my image from 3138×2012 pixels to 800×600 pixels.

$ convert -size 3138x2012 sample.jpg -resize 800x600 sample-2.jpg

Make a Note: If you use this command, ImageMagick will try to preserve the aspect ratio. This will change the image to fit within the 800×600 area, but the image will not be exactly 800×600.

You can check the change of size by running the following command.

$ identify sample-2.jpg
sample-2.jpg JPEG 800x513 800x513+0+0 8-bit sRGB 74062B 0.000u 0:00.009

Alternatively, you can use “%” to resize images. In this example, we will resize the image to half the size of the original.

$ convert sample.jpg -resize 50% sample-2.jpg

You can check the change of size by running the following command.

$ identify sample-2.jpg
sample-2.jpg JJPEG 1569x1006 1569x1006+0+0 8-bit sRGB 236156B 0.000u 0:00.000

How to Add a Border

Sometimes you may need to add a border to an image. If yes, use the following command. I have added a red color border, but you can change the color code to suit your needs.

$ convert -border 1x1 -bordercolor "#FF0000" sample.jpg sample-3.jpg

The above command adds a one pixel wide red border to the output image.

How to Apply Effects

You can apply a variety of effects to an image using ImageMagick.

Run the following command to apply the “charcoal” effect to an image.

$ convert sample.jpg -charcoal 1 sample-charcoal.jpg

Run the following command to apply the “monochrome” effect to an image.

$ convert sample.jpg -monochrome sample-monochrome.png

Batch conversion

ImageMagick allows you to process multiple files at once. Create a small bash script to achieve this. In this example, we are going to convert all images in a folder from JPEG format to PNG format.

$ for image in *.jpg; do convert "$image" "batch-process/${image%.jpg}.png"; done

About Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

View all posts by Prakash Subramanian →

4 Comments on “How to Easily Resize, Convert and Modify Images from the Linux Command Line”

  1. Personally, I find nothing easy from the command line, yet I love and use Linux. Luckily there are a lot of good programs that do convert easily, like Krita or Pinta.

Leave a Reply