How to Use Terminal for Image Converting and Filtering

Viran Malaka
6 min readApr 8, 2018
Photo by João Silas on Unsplash

Hi all, Let’s do some MAGIC with ImageMagick

We all love very much to opensource software, ImageMagick is one of a best open source image processing tool which is running in Windows, Mac, and Linux. This has not only a terminal version but also a number of GUI tools that are written in the various language. But Here we are focusing on Terminal version because this is for the terminal Lovers… ;-)

Image from : https://itsfoss.com/ubuntu-terminal-train/

Installation

$ sudo apt-get install imagemagick

This is only for Ubuntu users. Others can refer here [https://www.imagemagick.org/script/download.php] for other OS installations.

Let’s talks about the ImageMagick features one by one.

Change Image Type

You will not believe ImageMagick can handle more than 200 image type including PNG, JPEG, GIF, TIFF, DPX, EXR, WebP, Postscript, PDF, and SVG. Now I am going to tell you how to convert the image between those types.

$ convert input.jpg output.png

You just have to put the image file type as the output file. The place where you should consider is bold.

Simple Image Transform

1. Resize

It is easy, you should have to use only -resize flag when converting. Let’s check some examples.

// if you want to set the image into 100px height and width.
$ convert input.jpg -resize 100x100 resized_output.jpg
// what if the original image is not exactly a square. you can
// ignore the aspect ratio of the original image.
$ convert input.jpg -resize 100x100! resized_output.jpg ---> (a)
$ convert input.jpg -resize 100x100\! resized_output.jpg ---> (b)
// resize in a ratio like 50%(smaller) or 120%(larger)
$ convert input.jpg -resize 50% resized_output.jpg

Let me explain the above magic. Your original image can have a different aspect ratio. It can have a shape of square or rectangle. If you want to change that shape while you converting the image you can use ‘!’ mark. Here both code segments (a) and (b) will do the same but some Unix operating system will recognize the ‘!’ as special character. That is why we use backslash (‘\’) to ignore it. You can resize the image with keeping the current aspect ratio if you use percentage sign(‘%’).

Hey… Can I make the image into specific width or height ?

Yes.... Here you go.
$ convert input.jpg -resize 100 resized_output.jpg
$ convert input.jpg -resize x200 resized_output.jpg

First one makes 100px wide image and the second one makes a 200px height image. If you want to limit the final image within 5000 pixels, use ‘@’ sign.

$ convert input.jpg -resize 5000@ resized_output.jpg

2. Change Quality

Some images have more quality but the issue is they have large file size. We can reduce the image quality with ImageMagick.

$ convert input.jpg -quality 50 low_quality_output.jpg

3. Rotate, Flip, Flop

$ convert input.jpg -rotate 90 90_rotate_output.jpg    

use -rotate flag and then put the degree you want to rotate. When you think about the rotation of angle except a multiply of 90. (ex: 30 degree). Then the image should have background right!. Yes you can decorate that background as well.

$ convert input.jpg -rotate 30 -background lightskyblue rotate_output.jpg

The output image will have light blue color background. use -alpha flag to make that background transparent.

$ convert input.jpg -rotate 30 -alpha rotate_output.jpg

Here is some tips to have fun with convert. Use -flip ,-flop ,-transpose , -transverse to see what happened to the image.

4. Crop

Here we are using -crop flag with 4 number parameter. Lets see what are those <width>x<height>+<x-point>+<y-point> . It is pretty much confusing right? Here are some examples.

$ convert input.jpg -crop 100x200+350+150 cropped_output.jpg

You will get 100px wide and 200px high image starting from point where x coordinate is 350 and y coordinate is 150 in original image.

5. Special Effects

Let’s consider how to add some simple effects like blur and border in to your images.

$ convert input.jpg -blur 2x3 blurred_output.jpg

There are two parameters for this operation. First one is called the radius and other is sigma. I am not going to talk more about those. you can play with different value inputs and see the magic. For more information refer the documentation (https://www.imagemagick.org/Usage/blur/#blur)

Now decorate your image with a nice border. As our first attempt, add a blank border.

$ convert input.jpg -border 10x20 border_1_output.jpg   ---> (1)$ convert input.jpg -border 10%x10% border_2_output.jpg ---> (2)

Script (1) will make your image with blank border which has 10px left and right, 20px top and bottom. The second one will make your border according to the width and height of your input image.

$ convert input.jpg -frame 10x10+2+4 border_3_output.jpg  ---> (3)$ convert input.jpg -frame 10x10+2+4 -mattecolor SkyBlue \
border_4_output.jpg --- (4)

Here you will see the nice 3D effected border in your output. Don’t worry about the tag -frame is the same as the border but has more powerful effects. Let me explain you the parameter passes. First two is same as above, the size of the border. The extra two argument specifies the ‘outside’ and ‘inside’ bevels of the frame. The tag -mattecolor specifies the color of the frame.

Tip: use two -frame tag to see the magic

$ convert input.jpg  -mattecolor grey  -background grey \  
-frame 3x3+0+3 -gravity South \
-splice 0x15 -annotate 0x0 'My Beautiful Image' \
-frame 6x6+3+0 \
output_annotated.jpg

Here I introduce a nice effected border with a text. Following script will create a beautiful colored border to you image.

$ convert input.jpg -matte -mattecolor '#CCC6' 
-frame 10x10+3+4 \
-size 100x100 plasma:fractal \
-normalize -blur 0x1 \
-compose DstOver -composite \
output_frame_plasma.jpg

6. Combined Operations

We have discussed lots of magic from ImageMagick. I am sure you saw the beauty and the power of this tool. Another useful feature comes with ImageMagick is the ability to combine the operations.

$ convert input.jpg -resize 50% -rotate 90 output.jpg

This example has only two operations. But you can use more. Above, in the frame section, I put some code segments that have more operations.

7. Batch Processing

You may have a DSLR or a high pixel camera in your mobile phone, then you know how much capacity an image take for the store. In a typical DSLR camera an average image will be more than 10MB, for a smartphone it will be more than 3MB. Let’s say you have a folder having about 500 images to reduce the quality and file size. Here you have the better and easiest solution. You can combine both powers from bash and ImageMagick.

What is bash?

Yes, Bash is the scripting language using in the terminal. We are going to use the power of bash to increase the true put of ImageMagic. Bash language has ‘for’ loop.

// cd into your image folder
$ for i in *.jpg; do
\
convert $i -resize 50% -quality 50 ${i%.jpg}_new.jpg \
; done

It seems more complex, doesn’t it? No worries, It is very much easy if you understand how the script build.

First, I explain the ‘For’ loop syntax. Here i is the variable that is iterated inside the for a loop. You can see *.jpg code segment will filter out all JPG images for processing. If you have any other file type please change it.

Next line will call the convert tool. You know this syntax already but the use of some $ code segment will confuse you. $i is the current file name. It is just the input file name. Nothing more. Another interesting thing is how we define the output file name. It is OK if you make another new directory and put all new files into it. But you know we cannot have two files having the same file name inside the same directory. We have to add some change into the output file. Let's see how we make the output filename. ${i%.jpg} will take only the filename without file extension. Then append ‘_new.jpg’ tag to the output name. Finally, all converted images will have ‘_new’ tag in its’ file name.

END

Hope you learn a lot about ImageMagick free and opensource tool. You can read more from their website (https://www.imagemagick.org/script/index.php). I just want to make you more familiar with this tool. I am sure you will love this and it will make your life easier when you doing some small tasks with one image file or a bunch of files.

Thank you. Have a nice day.

--

--