Skip to main content

Imagemagick - convert (a tool to batch manipulate images)

Convert is an extremely powerful and versatile but "simple to use" tool to manipulate images. No need to open each file and operate on them in GIMP or Photoshop. I helps best when I know exactly what to do with my pics. For example, I want to resize all pics from a recent field trip and also give them a border along with a nice tag at the corner like "Goa trip" so that I can recall later in my life where are those pictures from and if I include the date too(which I always do) I have a nice picture diary. I think it i always a good idea to tag pictures with dates. That is where this tool comes in to be so handy. In Photoshop also you can do batch conversion of images. To do the tagging you have to save a group of transformations like adding text at the corners as "Actions" and open files one by one and keep applying that to each of them. So boring. In imagemagick this is accomplished with just one line command. See my shell script at the end for an example. But will only explain some features which are used regularly not all of them. There are many more. Interested users should refer to the original site http://www.imagemagick.org/Usage/ which contains extreme details.

convert command popular usages :-

1. Converting Color to Gray-Scale

convert test.png -colorspace Gray gray_colorspace.png

http://www.imagemagick.org/Usage/color/#grayscale

2. Resizing

convert dragon.gif -resize 64x64 resize_dragon.gif

this resizes dragon.gif into 64x64 pixels in size. Aspect ratio may or may not be maintained. Here aspect ratio would be maintained if the original file dragon.gif was 1:1.

or better still just give the width and forget all about aspect ratio,

convert dragon.jpg -resize 640 resize_dragon.jpg

this resizes dragon.gif into 640 pixels in size. Aspect ratio maintained.

3. Converting to other image formats

convert dragonold.bmp dragon.jpg

or in batch mode

convert *.jpg new-*.jpg

This is why I like this command. It can convert to almost anything. Previously I had such a bad time finding the right tools to do the converting and that too for a batch of files. I found that irfanview was the best tool in Windows. But for Linux .... Note that there is an issue about preserving transparencies. Find it in the source site http://www.imagemagick.org/Usage/color/
I found that in batch mode even if I mistype new-*.jpg as just new-8.jpg(missing the wildcard) it automatically renumbers the output images correctly. So cool.

4. Making a pdf out of a group of images

Yes you might guessed it right. It's just.

convert scanofnotes*.jpg notes.pdf

5. Rotating images
convert -rotate 90 image.jpg
This rotates the images CW 90 degress.

Issues :- In case of using wildcards (convert *.jpg new-*.jpg) Imagemagick loads all images to memory before acting on them. It can lead to slowdowns and memory locks. So it is better to do the conversion using shell script than wildcards.

Therefore, I wrote a simple script to do resize pics and then tag them with date and location where the pic was taken.

######### shell scipt #########
for img in `ls *.JPG`

# parse all JPG's in the directory
# note JPG and jpg are treated differently

do
# resize image from 1280 to 640 pixels and store in temp.jpg so that more transformations could be applied to it
convert $img -resize 640 temp.jpg

# add a border with border command
convert $img -resize 640 -bordercolor "#000000" -border 2x2 -bordercolor "#FFFFFF" -border 10x10 -bordercolor "#000000" -border 4x4 -bordercolor "#FFFFFF" -border 24x24 -bordercolor "#000000" -border 2x2 -font 'Helvetica' -fill white -box '#00770080' border-$img

# add a tag(text) at the lower right corner
convert temp.jpg -gravity southeast -stroke '#000C' -strokewidth 2 -annotate 0 'Party @ GOA (5 July 2008)' -stroke none -fill white -annotate 0 'Party @ GOA (5 July 2008)' tag-$img
rm -f temp.jpg
done
###########################


-border 2x2 : makes a border around a image not on it.
-border 2x2 -bordercolor "#FFFFFF" creates a white border 2 pixels thick around the image.
-gravity southeast : positions the text at the lower right corner
-strokewidth 2 : fix the width
-stroke '#000C' : the color of stroke


http://sun.cs.lsus.edu/~rmabry/magick/strokewidth/strokewidthtest.png

Please share your own "convert" recipes in the comments.

Source : http://imagemagick.org/Usage/

Comments

Popular posts from this blog

Fastest way to send multiple drafts from gmail

People claim that the fastest way to send multiple email drafts is to use Gmail IMAP with email client like Outlook or Evolution or Thunderbird. But I have found this is not true. Because Thunderbird and Evolution etc. email clients treats the drafts as emails still to be edited. So it is not just simple select all and hit send. Each email draft has to be opened and sent separately. That is a lot of clicks and mouse movements, wasting precious time and energy. I have a better solution which involves minimum keystrokes and mouse usage. Efficiency booster technique for sending emails. If someone is feeling adventurous and want to try it from the Gmail interface itself, here's how to do it in the fastest possible manner. It involves using the mouse once. Select the first draft. Gmail would open a new email box and put the cursor inside the box to write. Press TAB once to go the Send button. Press ENTER to send. Now Gmail sends it and the box is gone but the highlight goes to the last

LYRICS OF CHANDRABINDOO

___________________________________________________________________ SWEET HEART FROM AAR JAANI NAA(T-SERIES) -- SWEETHEART -- Pratham college-er din ta Aajo thik e mone poRey scene ta Dada didi haath dhorey siNRi tei bose poRey Aamar chokh ta ghorey bon bon bon bon Sweetheart, I am seating alone Sweetheart, for me there is none DhoNk gile chole gelo pratham maas Meye dekhlei feli deergho-shwash DhoNk gile chole gelo pratham maas Meye dekhlei othe nabhishwash Meyera bheeshan smart poRey chhoto mini-skirt Aamar e je sheet korey kon kon kon kon Sweetheart, I am seating alone Sweetheart, for me there is none Taarporey kete gelo maas chaar Fuse holo je kato future Bandhura purse khule eke oke taake tole Aamar pran ta korey chon mon chon mon Sweetheart, I am seating alone Sweetheart, for me there is none Ekdin lawn theke beriye Ek tanayaar dike taakiye Hawt korey ki je holo magaj ta ghurey gelo Taar kaaner saamne kori ghyan ghyan ghyan ghyan Sweetheart, I am seating alone Sweethea

Changing the font size of section headings in LaTex

You have several ways to do so: 1.- A direct redefinition of \section: \makeatletter \renewcommand\section{\@startsection{section}{1}{\z@}%                                   {-3.5ex \@plus -1ex \@minus -.2ex}%                                   {2.3ex \@plus.2ex}%                                   {\normalfont\large\bfseries}} \makeatother 2.- By means of the titlesec package: \usepackage{titlesec} \titleformat{\section}{\large\bfseries}{\thesection}{1em}{} 3.- By means of the sectsty package: \usepackage{sectsty} \sectionfont{\large} source : http://www.latex-community.org/forum/viewtopic.php?f=4&t=3245   Now, I would explain the titlesec package a bit more (because it seems easier to me and with more options) : To change the section fonts with this package put the following lines in the preamble - \usepackage{titlesec} \titleformat{\ section }{\ large \ bfseries }{\thesection}{1em}{} Options available are- a> Font size - \normals