Convert ppm to jpeg

EDIT: So this solution is really really bad. Thanks to commenter Manuel, I've posted a much better solution here.

So last summer I bought a crazy cheapo camera at Rite Aid for $20 to take with me to high risk locations like gallivanting down a creek or hanging out at the beach. The pictures from it are along the lines of low quality camera phone, but that's really about what I was looking for.

Long story short, support for it in Windows sucks. You have to install its own crappy little import utility and it doesn't work all right in Windows Vista, so I gave up on it for awhile. About a month ago, just for kicks I plugged it into my computer running Ubuntu Linux, and would you believe the standard photo manager program recognized it and imported all the pictures for me? Yay Linux.

The downside is that it saves pictures as .ppm files, which don't do me any good when trying to upload to Facebook since FB only takes .jpg, .png, and .gif. I found a utility pnmtojpeg that converts a .ppm file feed into its stdin into a jpeg put out its stdout. Example usage:
cat foo.ppm | pnmtojpeg > foo.jpg

That's all good and well, except I want to handle a dozen or two of these at a time. So I hacked together a pair of scripts to do it for me.
convert.sh:
#!/bin/bash
find $1 -name *.ppm | xargs ./helper.sh

helper.sh:
#!/bin/bash
cat $1 | pnmtojpeg > $1.jpg

Usage:
./convert.sh /path/to/folder/of/pictures/

How this works is the find command in the first script lists every file in the path (given as argument number 1 to the script ($1)) that matches the patter *.ppm. * is a wildcard which means any number of any characters. Which makes sense since any filename which ends with .ppm is going to be a .ppm, regardless of what the filename is before that. The xargs command reads in this list of .ppm files and calls helper.sh for each one.

The one thing that I didn't see an easy solution to was that the filenames came out as foo.ppm.jpg, which works, but isn't pretty.
Does anyone have any input on a more elegant way to do this? Both for doing it in one script and for trimming off the .ppm?

Popular Posts