Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, Want to make a script that will resize pictures within the directory and all sub directories. I know it's pretty straight forward, to do within the directory itself ...
  1. #1
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958

    Bash Resize Pictures (directory & subdirectories)

    Hi All,

    Want to make a script that will resize pictures within the directory and all sub directories. I know it's pretty straight forward, to do within the directory itself it's just this:

    #!/bin/bash
    for i in `ls *.jpg`;
    do
    convert $i -resize 800×600 $i
    done


    Thanks all

    **batch, not bash
    Last edited by jmadero; 11-15-2011 at 09:05 PM.
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So you basically want to make this recursive. You could do something like this:
    Code:
    #!/bin/bash
    
    if [ -z "$1" ]; then
        # No directory given; start with .
        dir=.
    else
        dir=$1
    fi
    
    for file in "$dir/*"; do
        if [ -d "$file" ]; then
            # This is a directory
            "$0" "$file"
        elif echo "$file" | egrep -q '.*\.jpe?g$'; then
            # This is a JPEG file
            convert "$file" -resize 800x600 "$file"
        fi
    done
    I haven't actually run this, but it should give you a basic idea.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The utility find can discriminate between directories and plain files, and produces the pathname, so I would use that in the operating command. Here is a script that displays my running context and shows this way of doing something to each jpg file in a tree:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate process tree of files.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C find
    
    DIR=${1-d1}
    
    pl " Input directory $DIR:"
    tree $DIR
    
    pl " Results:"
    find $DIR -type f -name '*.jpg' |
    while read i
    do
      pe " Would have run \"convert $i -resize 800x600 $i\""
    done
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    GNU bash 3.2.39
    find (GNU findutils) 4.4.0
    
    -----
     Input directory d1:
    d1
    |-- a1
    |-- b1.jpg
    `-- d2
        |-- a2
        |-- b2.jpg
        `-- d3
            |-- a3
            |-- b3.jpg
            `-- jpg-something
    
    2 directories, 7 files
    
    -----
     Results:
     Would have run "convert d1/b1.jpg -resize 800x600 d1/b1.jpg"
     Would have run "convert d1/d2/d3/b3.jpg -resize 800x600 d1/d2/d3/b3.jpg"
     Would have run "convert d1/d2/b2.jpg -resize 800x600 d1/d2/b2.jpg"
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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