2

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.

muru
  • 197,895
  • 55
  • 485
  • 740
Duby
  • 41

1 Answers1

5

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;
muru
  • 197,895
  • 55
  • 485
  • 740
iBelieve
  • 5,384