Results 1 to 2 of 2

Thread: Shell script to resize jpg and subfolders

  1. #1
    Join Date
    Dec 2010
    Beans
    18

    Shell script to resize jpg and subfolders

    I use the following to resize images in a folder

    Code:
    for file in `ls -l`
    do
    name=`echo $file | cut -f1 -d.`
    convert -strip  -quiet -geometry 1024x768 -quality 80 $file /path/to/pictures/{name}_email.jpg
    done
    Which works great. Every time I put a memory stick or camera card in my PC Shotwell imports them and saves them to my NAS in folders and subfolders which is also great. So the subfolder depth can vary. I would like to clone this structure but with all the images resized to 1024x768 with a cron job. Lazy eh.

    Would it be possible to do this, automatically duplicate the file structure and resize the images into a completely different folder? In this case Dropbox. Whilst not recreating images already resized.

    Best place to start?

    Thx
    Distributor ID: Ubuntu
    Description: Ubuntu 10.10
    Release: 10.10
    Codename: maverick

  2. #2
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Shell script to resize jpg and subfolders

    I would suggest a 2-step process ... (1) using rsync to mirror the directory structure, and (2) using find in a loop to do the converting ... you could combine both but that would complicate things a little.

    something like the below. (try it out on a test sample first before unleashing it on the real thing!).
    Code:
    #!/bin/bash 
    
    # set the source & dest directories -- (update to reflect your locations)
    src=/media/nas/pictures
    dest=/home/you/picturesclone
    
    # sync the directory structure using rsync ... this uses rsync filter rules to 
    # ignore all files and only work on directories
    cd "$src" || { echo "Failed to cd to $src, exiting..."; exit 1; }
    rsync -a -f "+ */" -f "- *" . "$dest"
    
    # find all jpg files, check if equivalent file exists in parallel directory, 
    # if not, create it
    find . -iname '*.jpg' | while read file
    do
        other_file="$dest/${file#./}" 
        if [[ ! -e "$other_file" ]]; then
            echo "Converting $file to $other_file" 
            convert -strip  -quiet -geometry 1024x768 -quality 80 "$file" "$other_fi
    le"
            [[ $? -ne 0 ]] && { echo "Failed to create file $other_file, exiting"; e
    xit 1; }
        else
            echo "Skipping $file as it already exists at $other_file"
        fi
    done

Bookmarks

Posting Permissions

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