Ubuntu: Creating thumbnails with the same name as the pictures


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.


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;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »