Thursday, April 3, 2008

A Better Way to Convert ppm to jpeg in Linux

So two months ago I posted the scripts I used to convert ppm to jpegs in my Ubuntu setup. I knew it was poorly done because it renamed every file picture.ppm.jpeg, and it needed two seperate files to work. Thanks to commenter Manuel, I finally have a much better way to do it.

ppmconv:
for pic in *.ppm
do
pnmtojpeg "${pic}" > "${pic/%ppm/jpg}"
done

Then just put that in your path somewhere, chmod +x ppmconv, and you're good to go.

To add it to your path, add this line to your ~/.bashrc file:
PATH=$PATH:"~/bin"
Then add whatever scripts you want into ~/bin to have them run. Right now the only other thing I have in there is my twitter script.

My dad and I were actually talking about this exact same solution to my problem on the way up to Davis last week, so I at least knew it was bad (Not to mention unstable...). Thank Manuel!

3 comments:

  1. hi

    thanks for posting the script - however when i try this i get the following error msg :

    line 4: {pic/%ppm/jpg}: No such file or directory

    can you please help me out with this?

    Thanks.

    ReplyDelete
  2. Way late, but for future readers:

    Make sure you don't forget the "$" before the {pic/%ppm/jpg}.

    ReplyDelete
  3. Thanks, very handy. The digicam I got stuck with gives .x-portable-pixmap as the file extension. Changing ppm to x-portable-pixmap in your script worked perfectly.

    ReplyDelete