Wednesday, December 5, 2012

Resizing and Modifying Images through Linux terminal

This one is really interesting feature of Linux. We always had use some photo editing software like Photoshop for resizing, modifying etc to the images. But now this can be done quickly by just executing commands through the Linux terminal. Imagemagick is a suite of command-line utilities for modifying images. Imagemagick can quickly perform operations on images from terminal, also can perform batch processing of many images.
Installation:
This can be done by installing the Imagemagick by executing the command, as it is not included in the default installations of Ubuntu or any other Linux distributions. Get it through following command:
sudo  apt-get install imagemagick
Converting the Formats:
This is one of the basic things to do that is converting the images from one format to other formats. In this, the operation will be done on image according to the command given and saves the image with the name we specify in the command. The command is:
     convert filename.jpg filename.png


You can also specify a  compression level for JPEG images:
convert filename.jpg –quality 94 filename.jpg
The number must be between 1 and 100.
Resizing Images:
This is used to resize the images.The command is
convert filename.jpg –resize 180x100 filename.jpg


This command will resize the image to 180pixels in width and 100 pixels in hieght.
If we give the same name in the command it will overwrite the original image with the new size.
If we just want to resize the width of the image, this can be done by the command as follows:
     convert filename.jpg –resize 180 filename.jpg
Similarly , just hieght can be resize by the following command:
convert filename.jpg –resize x100 filename.jpg
Rotating Images:
This is used to rotate the images by an angle we specify in the command.This will take the image and rotate the image by the angle specified and save the image given by the name in the command.If same filename given then Imagemagick will overwrite and rotate the original image and save it.
The command is:
     convert filename.jpg –rotate 90 filename –rotated.jpg


This command will rotate the image to 90 degree.
Applying Effects:
Imagemagick can apply a variety of effects to the images.
For example the following command applies the “charcoal” effect to an image:
convert filename.jpg –charcoal 2 filename-charcoal.jpg



The charcaol command used to give the charcoal effect to the image and 2 is to indicate the strength of the effects.
Another one is like implode effect.This effect can be applied by the following command:
     convert filename.jpg –implode 1 filename-imploded.jpg


Combining Operations:
All the above commands seen can also be combined and can be applied to the images.
For example the following command resize the image, rotate it, apply an effect and then convert it to another format:
convert filename.jpg –resize 300x300 –rotate 180 –charcoal 4 –quality 94 filename.jpg



Batch Processing:
Imagemagick can also operate the batch processing to the images.
For example,the following command will take all the png files in the current directory, rotate them and save a new copy of each with “-rotated” added to the beginning of each filename
for file in *.png;do convert $file –rotate 90 rotated -$file;done


This is  just some of the features of Imagemagick done by the commands through the Linux terminal.Still,there are many more with Imagemagick which we can do and can apply lots of interesting features to the images.

10 comments:

  1. it is really nice. . .but can we merge two images through imagemagick. . . ?

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Amazing. I think I should try to modify the code and insert my Image Processing codes in it.

    ReplyDelete
  4. Kabita,
    this can be a nice 'tip'...
    Refer, the "Linux for You"s editions and submit your tip at the following link:
    http://www.linuxforu.com/submit-your-tips-tricks/

    ReplyDelete
  5. yes i will definately submit this one..

    ReplyDelete
  6. to merge the two images ,you can use the command
    composite -dissolve value% input1 input2 output

    ReplyDelete
  7. CAN WE CROP THE IMAGE USING IMAGEMAGICK..????

    ReplyDelete
    Replies
    1. you can crop the image by using the command
      convert imagename -crop 250x200(parameters) imagename

      Delete