I often work with images while preparing technical articles for 2DayGeek (this website).
I take lots of screenshots as part of the article preparation and 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.
In this guide, we will show you how to use ImageMagick tool to easily Resize, Convert and Modify Images from the Linux command-line.
What is ImageMagick?
ImageMagick is a free and open source, feature-rich, command-line based image manipulation tool.
It is 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, etc.
It can resize, mirror, rotate, transform images, adjust image colors, apply various special effects, etc.
It supports batch processing, which allow you to process several 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-deve
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 to install ImageMagick:
$ sudo apt update $ sudo apt 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 its size (in MBs or KBs) then specify a compression level while converting them. It won’t reduce the pixels. See below:
$ 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 verify the change in 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 (50%):
$ convert sample.jpg -resize 50% sample-2.jpg
You can verify the change in 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. To do that, use the following command. I have added a red color border, but you can change the color code as per your need.
$ 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. Some of them are listed below.
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
This example will show you how to resize multiple file’s from “.png” to “.jpg” at once:
for image in *.png ; do convert "$image" -quality 40 "${i%.}.jpg" ; done
Wrapping Up
In this article, we showed you several examples to Resize, Convert and Modify Images from the Linux command-line
If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!
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.
Hi James,
Yes, that’s right but you can do the same through CLI as well. We will write an article about GUI tools in the future.