Creating thumbnail images with convert

January 1, 2011

A little while ago, I found I had a bunch of images that needed thumbnails that were 100×75. This isn’t hard to do – I used convert and a pair of bash for loops.

The core was a call to convert, which is part of ImageMagick.

convert -thumbnail 100x75 input.jpg thumbnail.jpg

All of my images happened to be 4:3 – if they hadn’t, I might have used 100x75! or rotated/resized them.

Next, all my files had numbers of the form file01..file09 file10 file11… etc. If you’re nuts, you try to figure out how to do this in a single for-loop with a condition for the first 9 elements that start with 0.

If you’re lazy like me, you use two loops, with a cursor-up in bash so you don’t have to type as much:

export INFILEPREFIX=file
export OUTFILEPREFIX=file
for i in {1..9}; do convert -thumbnail 100x75 ${INFILEPREFIX}0${i}.jpg ${OUTFILEPREFIX}0${i}t.jpg; done
for i in {10..25}; do convert -thumbnail 100x75 ${INFILEPREFIX}${i}.jpg ${OUTFILEPREFIX}${i}t.jpg; done

For the non-thumbnails, I resized as well, using -resize 800×600.

Advertisements

Batch-converting images

June 29, 2010

I had a wad of images that I needed to resize from 300dpi and high-resolution jpg to 4.5×6 (or 6×4.5) low resolution at 72 dpi.

First, I installed ImageMagick:
sudo aptitude install imagemagick

Then I opened GIMP and determined the pixel width and height of one image. (Image -> Canvas Size). In my case, it was 3264×2448.

Next, I resized the image to the size I wanted. That turned out to be 432×324.

In other words, I wanted my new images to be 0.132352941 of the original image. A quick test told me I was on the right track:

convert P4250049.JPG -resize 13.2352941% test.jpg

The first time I did this I forgot the % sign and got a 13×10 pixel image – not exactly what I wanted.

So, on to batch-converting everything in a directory:

mkdir ./resized
for i in *.JPG; do
echo $i
convert $i -resize 13.235941% -quality 20 ./resized/$i
done

And all my resized images were in the resized directory below the image directory. (Did I mention I wanted to reduce the quality so my images would be smaller too? Yep, that’s what the -quality line does.)