Find the answer to your Linux question:
Results 1 to 4 of 4
Hi everybody, I'm not sure if it is the right place to write this post, but here we go... I have a lot of images into the server and I've ...
  1. #1
    Just Joined!
    Join Date
    Jul 2006
    Posts
    51

    [SOLVED] Shell script, image reducing problem

    Hi everybody,

    I'm not sure if it is the right place to write this post, but here we go...

    I have a lot of images into the server and I've write a script to reduce all these images recursively inside all the subdirectories, here is the code:

    Code:
    #!/bin/bash
    
    for img in `find /home/aleix/photos/ -iname "*.JPG"`
    do
           convert -sample 640x480 $img $img
    done
    It works well except when find command finds a subdirectory or a file name with a space in his name, for example:

    /home/aleix/photos/2007/photos april/...

    or

    /home/aleix/photos/2007/april 009.jpg

    In these cases script returns an error telling me that it can't find:

    Code:
    convert: unable to open file `/home/aleix/photos/2007/photos': The file or directory doesn't exist.
    convert: unable to open file `april': The file or directory doesn't exist.
    convert: unable to open image `009.jpg': The file or directory doesn't exist.
    Anybody has any idea to resolve it?

    Thanks!

    Aleix

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Try something like this:
    Code:
    find /home/aleix/photos/ -iname "*.JPG" | xargs -ix convert -sample 640x480 \"x\" \"x\"
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    #!/bin/bash

    for img in `find /home/aleix/photos/ -iname "*.JPG"`
    do
    convert -sample 640x480 $img $img
    done
    This snippet has some problems.

    First, you need to quote filenames unless you are sure that no spaces or special characters are contained within the names:

    Code:
    convert -sample 640x480 "$img" "$img"
    Another big prlblem is that, if there are too many files, bash will not be able to handle the long list. There are two nice ways to handle that correctly, if you want to use a loop, use while:

    #!/bin/bash

    find /home/aleix/photos/ -iname "*.JPG" | while read img
    do
    convert -sample 640x480 $img $img
    done
    Or just use the -exec feature:

    Code:
    find /home/aleix/photos/ -iname "*.JPG" -exec convert -sample 640x480 '{}' '{}' \;

  4. #4
    Just Joined!
    Join Date
    Jul 2006
    Posts
    51
    wje_lf, i92guboj

    Thanks for your replies...

    the 2nd and the 3th optionts worked perfectly!

    thanks a lot!

    see you.
    Aleix

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...