Sep 02 2009

Editing and manipulating images from Linux command line

Published by amjad at 11:35 am under Centos, Debian, Fedora, Red Hat, Ubuntu

Imagemagick includes a number of useful tools that allows us to manipulate, edit and get information about image files from command line

First we have to install the package. In Debian/Ubuntu, do

apt-get install imagemagick

In Red Hat/Centos/Suse, we do

yum install imagemagick

Getting information about an image

To see short information about the image, use the following command

identify image.jpg

image.jpg JPEG 400×266 DirectClass 68kb

To get detailed information, use -verbose

identify -verbose image.jpg
image.jpg JPEG 400x266 DirectClass 68kb
Image: image.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Geometry: 400x266
  Class: DirectClass
  Type: TrueColor
  Endianess: Undefined
  Colorspace: RGB
  Channel depth:
    Red: 8-bits
    Green: 8-bits
    Blue: 8-bits
   ....

Converting images

Use convert command to convert from one image format to another. For example to convert from image.jpg to image.png, use the following

convert image.jpg image.png

To convert giff to bmp, use

convert image.giff icon.bmp

Images of types .jpg, .bmp, .pcx, .gif, .png, .tiff, .xpm, and .xwd can be converted from one type to another

Resizing images

Let’s first find out current geometry of our image file

identify -verbose image.jpg

image.jpg JPEG 400×266 DirectClass 68kb

To resize it to 600×399, issue the following command

convert -resize 600x399 image.jpg image_big.jpg

Now check the new dimension

identify image_big.jpg

image_big.jpg JPEG 600×399 DirectClass 107kb

We can also reduce the size of the image if we specify smaller geometry. For example

convert -resize 301x200 image.jpg image_small.jpg

Rotating an image

Using the convert command, we can rotate an image by any degree we wish. To rotate our test image by 35 degree, issue the following command

convert -rotate 35 image.jpg image_45.jpg

The original image looked like

image

The new image looks like the following

image_35

Having fun? Keep reading.

Adding text to an image

Now we are going to add some text to the image. The following will add ‘Our test text’ to the image

convert -fill white -pointsize 20 -font helvetica -draw 'text 5,15 "Our test text."' image.jpg image_text.jpg

-fill is the fill color to use the text
-pointsize is the size of text.
-font is the font used to draw the text
text 10,80 shows the vertical and horizontal distance from the upper left corner. If you increase the first number, the text will be placed farther to the right and increasing the second number places the text farther down.

Our new image like below

image_text

To add a stroke to the text as well

convert -fill white -pointsize 30 -font helvetica -stroke  purple -draw 'text 5,23 "Our test text."' image.jpg image_text_mag.jpg

Now the text has purple color stroke as shown below

image_text_mag

Creating thumbnails

Imagemagick’s convert utility can be used to create thumbnails from an image. For example to create 120×120 thumbnail from image.png

convert -thumbnail 120x120 image.png image_thumbnail.png

It results into this image

image_thumbnail

To create a thumbnail and add border around it as well, use

convert -thumbnail 120x120 -border 8 image.png image_thumbnail_bd.png

The resultant thumbnail image has a border as shown below

image_thumbnail_bd

To create a thumbnail, add border and rotate the image by 15 degrees we use

convert -thumbnail 120x120 -border 8 -rotate 15 image.png image_thumbnail_bd_rt.png

See the image below

image_thumbnail_bd_rt

Swirling an image

To swirl the poor guy by 150 degrees

convert -swirl 150  image.jpg image_swirl.jpg

The swirled image looks like this

image_swirl

Some artistic touches

To simulate a charcoal drawing, issue the following command

convert -charcoal 5 image.jpg image_char.jpg

we get this image

image_char

To colorize (blending the color of each pixel with a specified color)

convert -colorize 10,40,50 image.jpg image_col.jpg

We our new colorized image looks like this

image_col

where 10 is for red, 40 is for green and 50 is blue

Imploding is sucking the image into a hole at the center of the image. To implode

convert -implode .5 image.jpg image_imp.jpg

The imploded image now looks like below

image_imp

To give sepia-tone effect to the image

convert -sepia-tone 60% image.jpg image_sep.jpg

and this gives us this image as a result

image_sep

The following will solarize the image

convert -solarize 42 image.jpg image_sol.jpg

This is our solarized image

image_sol

To spread the pixels

convert -spread 10 image.jpg image_spread.jpg

Look at the result

image_spread

To blur the image

convert -blur 150 image.jpg image_blrr.jpg

image_blrr

To radial blur it by 30 degrees

convert -radial-blur 30 image.jpg image_blrr_rad.jpg

image_blrr_rad

 

 

The image used in the demonstration was taken from FreeDigitalPhotos.net

Share The Knowledge:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • blogmarks
  • Diigo
  • E-mail this story to a friend!
  • LinkedIn
  • Live
  • Reddit
  • StumbleUpon
  • Twitter
  • Blogosphere News
  • Identi.ca
  • Slashdot
  • Technorati

2 responses so far

2 Responses to “Editing and manipulating images from Linux command line”

  1. McIvor says:

    That Red Hat/CentOS command isn’t quite right. Apt-get is only for Debian, I think you want
    yum install imagemagick
    instead.

  2. amjad says:

    Thanks McIvor. was a typo. Fixed.

Leave a Reply