Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Please, here is my little code for creating thumbnails of pictures saved in a folder named 'pictures', and saving them in another folder named 'thumbs'.

#!/bin/bash
for i in *.jpg
do
convert -thumbnail 100 $i thumbs/$i
done

However, there two things the program doesn't do:

1) It does not retain the name of the pictures in the thumbnail. For instance, I would want it to generate a thumbnail with the name pic.jpg for a picture named pic.jpg

2) Also, when I run the program, i don't want it to generate the thumbnail for a picture it has already generated its thumbnail, unless that picture has been modified.

Any help will be very much appreciated.

Thank you

share|improve this question
add comment

1 Answer

According to the ImageMagick documentation,

The ImageMagick command line consists of

  1. one or more required input filenames.
  2. zero, one, or more image settings.
  3. zero, one, or more image operators.
  4. zero, one, or more image sequence operators.
  5. zero, one, or more image stacks.
  6. zero or one output image filenames (required by convert, composite, montage, compare, import, conjure).

So you need to have the original image parameter before the -thumbnail 100 command.

To only run convert if the file exists, you need to add an if test.

Also, in the for loop you have for i in *.jpg but then in the convert command you have pictures/$i. Unless I misunderstand what you're doing, these should both be the same, as I've done in my example script. If you want the thumbs directory inside the pictures directory, remove the ../ part from the convert command and if test. You will need to run this from the pictures directory, or if you don't want to, you can add cd pictures to the start of the script.

So, the script should be something like this:

#! /bin/bash

for i in *.jpg; do
    if [ "$i" -nt "../thumbs/$i" ]; then
        convert "$i" -thumbnail 100 "../thumbs/$i";
    fi
done;
share|improve this answer
    
This looks like it would work. –  coteyr Nov 28 '12 at 3:09
    
Thank very much mspencer. Your solution is working perfectly –  Duby Nov 28 '12 at 4:31
    
@Duby Great! If this is the correct solution, please mark my answer as your accepted answer. –  iBelieve Nov 28 '12 at 15:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.