leo charre


editing images in the command line with convert and mogrify

One of the dumbest things I used to do in making web pages was to resize images and make thumbnails in ‘photoshop’.

The next less dumb thing I did was to script thumbnailing. To allow a server to make the thumbnails instantly.
Then I got comfortable with things like convert and mogrify.

Both of these are interfaces to image magick..

These are used via the command line, the terminal. You feed them a list of one or more images (by path) and what to do with them.

Here’s a big thing to not forget:

convert makes copies of the original images

mogrify changes the original images

Resizing images

What if you want an image to be no more then 800 pixels tall and wide?

convert ./original.jpg -resize 800x800 ./copy.jpg

Do it to the original:

mogrify -resize 800x800 ./original.jpg

So let’s have a more useful example. I downloaded images from my camera. And I want to resize them and make thumbnails. My originals reside in ~/images/set1. (Whenever you see a ~ symbol it means ‘home’ to the computer, usually /home/yourlogin)

cp -R  /home/myself/images/set1 /home/myself/images/set1post # 1
cd /home/myself/images/set1post #2
mogrify -resize 800x800 ./*jpg #3
find ./*jpg -exec convert '{}' -resize 100x100 '{}'small \; #4
rename '.jpgsmall' '_small.jpg' ./* #5 

First of all, it’s safe to leave the # marks, these are interpreted as comments, thus ignored.
1) Let’s replicate the directory set of images first, recursively.
2) Change into the new directory
3) Change the originals to be no more wide or tall then 800px, for the web.
4) Use find to convert the files from image.jpg to image.jpgsmall, for all of them, this way we ahve copies.
5) Name the files properly, anywhere we see something like ‘.jpgsmall’, change it to ‘_small.jpg’

converting between image formats

Now, as ImageMagick tells it, primarity, it is a package for converting images from one format to another.
What if you have a freaking tif file that you need to be something else?

convert ./original_image.tif ./converted_image.jpg

Leave a Reply


Linux User