I've been using Imagemagick, a really useful tool, a lot recently to convert images for my ebook collection. I use it to create smaller versions of the images that Calibre produces to run on my own ebook server. This mini-tutorial will show you how to install and use Imagemagick and the useful convert utility on your Raspberry Pi.

Firstly, you'll need to install Imagemagick by typing in the following at the command line on your Raspberry Pi:

sudo apt-get update

This will ensure all your the packages on your Raspberry Pi are up to date. Next type:

sudo apt-get install imagemagick

This will install Imagemagick.

Resizing images

The utility I use the most is convert. I use this to resize images. For example:

convert cover.jpg -resize 200×100 new-cover.jpg

The above command asks ImageMagick to resize an image to 200 pixels wide and 100 pixels high. If the aspect ratio is correct then this will work perfectly, but if it isn't, Imagemagick will try to fit the image into a 200x100 area although it may not be exactly 200x100.

If you want the image to convert to exactly the size you give, even though it might mess up the aspect ration, then add an exclamation point to the dimension:

convert cover.jpg -resize 200×100! new-cover.jpg

The following command resizes the image to exactly 200 wide and the height is resized accordingly:

convert cover.jpg -resize 200 cover.jpg

and the next command resizes the image to exactly 100 high and the width is resized accordingly:

convert example.jpg -resize x100 cover.jpg

Converting between image types

You can also convert from one image type to another.

convert cover.jpg cover.png

Batch processing

If you want to resize more than one image, you might want to batch process the images on your Raspberry Pi. The way to do this is to use the following bash command:

for file in *.png; do convert $file -resize 200x100 thumb-$file; done

This will take every image in the directory and resize them all to 200x100, appending their filename with the word thumb.

I hope you found this information useful. I used these commands to create a commandline php script to update my ebook web server.


comments powered by Disqus