Find the answer to your Linux question:
Results 1 to 6 of 6
I can use the convert command to convert a single image from PNG to XPM, but cannot get (my first) script to work. I want it to convert a directory ...
  1. #1
    Just Joined! jadube's Avatar
    Join Date
    Jun 2008
    Location
    North Carolina usa
    Posts
    10

    Batch Script to Convert PNG to XPM

    I can use the convert command to convert a single image from PNG to XPM, but cannot get (my first) script to work. I want it to convert a directory of PNGs to XPM. Any advice appreciated. Thank you!

    Code:
    echo "Script Starting.."
    cd /home/user/Pictures/icons
    echo "start For loop"
    for pic in 'ls *.png'
    do
    	echo "converting $pic"
    	convert $pic $pic.xpm
    done
    echo "finished"
    ------------------------------------
    While looking for info I came across a 2nd script which might work if modified - this is fyi. Source: Imagemageic.org

    Code:
      # Use a simple shell loop, to process each of the images.
      mkdir thumbnails
      for $f in *.jpg
      do   convert $f -thumbnail 200x90 thumbnails/$f.gif
      done
      # Use find to substitute filenames into a 'convert' command
      # This also provides the ability to recurse though directories by removing
      # the -prune option, as well as doing other file checks (like image type,
      # or the disk space used by an image).
      find * -prune -name '*.jpg' \
             -exec  convert '{}' -thumbnail 200x90 thumbnails/'{}'.gif \;
      # Use xargs -- with a shell wrapper to put the argument into a variable
      # This can be combined with either "find" or "ls" to list filenames.
      ls *.jpg | xargs -n1 sh -c 'convert $0 -thumbnail 200x90 thumbnails/$0.gif'
      # An alternative method on linux (rather than plain unix)
      # This does not need a shell to handle the argument.
      ls *.jpg | xargs  -I FILE   convert FILE -thumbnail 200x90 th_FILE.gif

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You would have gotten farther with:
    Code:
    echo "Script Starting.."
    cd /home/user/Pictures/icons
    echo "start For loop"
    for pic in `ls *.png`
    do
    	echo "converting $pic"
    	convert $pic $pic.xpm
    done
    echo "finished"
    Do you see what I did there? Those two characters in red, which were single quotes in your code, have been converted to backticks. That's on the same key on the standard computer keyboard as the tilde ~ is, in the upper left corner.

    But I'd recommend that you make the difference more obvious, by doing this instead:
    Code:
    echo "Script Starting.."
    cd /home/user/Pictures/icons
    echo "start For loop"
    for pic in $(ls *.png)
    do
    	echo "converting $pic"
    	convert $pic $pic.xpm
    done
    echo "finished"
    That should fix things. But there will be one minor flaw. File aaa.png will be converted, for example, and the name of the new file will be aaa.png.xpm. I am guessing that you want, instead, aaa.xpm. If that is the case, do this instead:
    Code:
    echo "Script Starting.."
    cd /home/user/Pictures/icons
    echo "start For loop"
    for pic in $(ls *.png)
    do
    	echo "converting $pic"
    	convert $pic $(basename $pic.xpm .xpm)
    done
    echo "finished"
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined! jadube's Avatar
    Join Date
    Jun 2008
    Location
    North Carolina usa
    Posts
    10
    Bill, Thank you. I never would have understood the tick mark (vs) quote problem. The script runs and I get the "converting pic" messages as each file is processed. But I never find the output/xpm files. The script pemission is 755, and I have run it as root and user. But still no files. I'm re-reading documentation on permissions (I figure that is the problem) but if you suspect another problem please let me know. Thanks again.

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    There's no point in doing this:

    Code:
    for i in $(ls *.png)
    When this suffices:

    Code:
    for i in *.png
    Parsing the output of ls will screw you up if the file names contain spaces and can lead to some other potential problems.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    i92guboj has a point. Also, I had an error in the script I wrote. His change is in green, and mine is in red:
    Code:
    echo "Script Starting.."
    cd /home/user/Pictures/icons
    echo "start For loop"
    for pic in *.png
    do
            echo "converting $pic"
            convert $pic $(basename $pic .png).xpm
    done
    echo "finished"
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Aug 2010
    Posts
    1
    bash substitution FTW (see 'man bash' line 1238 or so):

    Code:
    for pic in *.png
    do
    	convert $pic ${pic/.xpm/.jpg}
    done

Posting Permissions

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