| Converting Images with ImageMagick |
|
|
|
| Written by Fr. Robert Bower |
| Monday, 03 January 2011 12:16 |
|
When you take a photo with your digital camera most often that image is too large for your website. Today you can buy a 10 megapixel camera for $80 and that camera will produce a picture with an average file size of 2.2 Megabytes. You certainly don't want to load your website with pictures that large. The smaller the file size the better as far as the web is concerned. The maximium size in most cases should be no larger then 100 kilobytes. The question becomes how do you shrink the picture down in size? I use two different programs to manupulate images. The first is GIMP which is an Opensource alternative to Photoshop. Gimp is a great program and if you need to do image manipulation you should have this program. Gimp is great working with a few photos but if I have 50 pictures I need to shrink down for a website I prefer ImageMagick. ImageMagick is a command-line program that allows you to convert or manipulate images and works great for converting a large number of pictures. Once the right command is entered and the return key is entered, ImageMagick happily processes the photos with no more input from the user. ImageMagick is fairly quick but quick can be a relative term. Imaging processing is a heavy cpu process so it may take some time so once the process starts go get a cup of coffee. There are two commands you will use with ImageMagick one is convert and the other is mogrify. Both commands change images convert is usually considered safer because it takes the old image, changes the image, and saves the new image with a new name but be warned you can destroy the old image if you make the new name the same as the old. mogrify takes the images and converts it so the original is gone and the new image takes it place. There is a path option that prevents this behavior but you are new to the program so do you really trust yourself. The first time I used the path option I did it wrong and mogrify happily ate the original image. So what is the point of this paragraph? Always, Always, did I say Always work on a copy of your pictures, ImageMagick will happily do what you tell it and if you tell it to eat your originals it will. Here is a mogrify batch command to change the image resolution to 72 dpi and set the quality to 80% on all jpeg files in the current directory and save them in the directory /home/frrobert/Pictures/testmagik Warning if the specified directory does not exist ImageMagick will happily do it in the current directory. mogrify -resample 72 -quality 80% -path /home/frrobert/Pictures/testmagik *.jpg What! Lets dissect that command mogrifyIs the command that will change the image. -resample 72Changes the resolution to 72 dpi. -quality 80%Changes the the quality to 80% which will allow the image to be compressed but on the screen the image to appear very similar to the original. -path /home/frrobert/Pictures/testmagikSaves the file in the indicated directory. If the directory is different then the current directory and is valid the original image will remain intact. *.jpgAll files in the current directory ending with the extension jpg will modified by the command. Here is a convert batch command to change the image resolution to 72 dpi and set the quality to 80% on all jpeg files in the current directory and save them in the current directory with a new file name with the prefix SM plus the original file name Example: picture.jpg become SMpicture.jpg
convert *.jpg -resample 72 -quality 80% -set filename:orig %t SM%[filename:orig].jpg Lets dissect this command. convertIs the command that will change the image. *.jpgAll files in the current directory ending with the extension jpg will modified by the command. -resample 72Changes the resolution to 72 dpi. -quality 80%Changes the the quality to 80% which will allow the image to be compressed but on the screen the image to appear very similar to the original. -set filename:orig %tSets the variable filename:orig to the top part of the name of the current file SM%[filename:orig].jpgTakes the variable filename:orig adds the letters SM to the front of it and saves the new file with that name. To find out more about ImageMagick or to download the program go to http://www.imagemagick.org/ |
|
|
| Tweet |
| Last Updated on Monday, 03 January 2011 16:15 |