I've written before about my obsession with Highway 395, but this time I have photos, many of which were taken out the car window (w/my wife's camera...someday I'll get my own), though one of my favorites was taken from inside the car, and one was actually from Lake Tahoe, but I included in the set anyway just because it was so awesome :-)
I did find that the pictures coming out of the camera were at 2560x1920, and since I have the free flickr account, there is a limit on how many MB you can upload. Earlier, I had quickly used up the limit on my wife's account uploading full-size pictures, only to find that they get scaled to 1280x960 when you view the largest version of the image after it's uploaded, but you get "charged" for the actual uploaded size. So this time, I installed Image::Magick, found the documentation, and spent 5 minutes reading it to come up with this:
use Image::Magick; my @files = <*.jpg>; for my $file (@files) { my $img = Image::Magick->new; $img->Read($file); $img->Scale(width=>1280, height=>960); # Yes, I'm overwriting the file, but the whole # directory is a copy $img->Write($file); }
I'm sure there must be other ways to do this, but this is what I came up with first, and it worked just fine, though I'd love to hear about alternatives, or anything wrong with the above :-)
I would do:
Shell rules.find . -name '*.jpg' -exec convert -resize '1280x960>' {} {} \;
Re:Shell rules.
Aristotle on 2006-09-30T23:15:33
It always amazes me that people use ImageMagick.
:-) What does the greater-than in your size specification mean? Why does ImageMagick have to have such an arcane interface that it shines through even in such a simple example? When I try to do multiple operations, I am always bamboozled by the precedence of the various options. Would that at least the image quality made it all worthwhile… but other tools usually do a much better job. NetPBM usually loses out on brevity vs. ImageMagick, but pipes are very intuitive. I find my trips to the manual much less frequent with it and I almost never need any dry runs to be sure a command line or script will do what I intended.
Re:Shell rules.
Abigail on 2006-10-01T00:24:17
The greater than sign says: scale only if the image is larger (it prevents a picture to be enlarged). Which is, IMO, a very useful feature. A feature that I haven't seen in NetPBM. (It might be there, but the manual page of pnmscale doesn't mention it).Re:Shell rules.
Aristotle on 2006-10-01T01:01:28
Yeah, you’re right, pnmscale doesn’t have that. You can code around it using pnmfile and explicit checks, but it’s awkward to say the least. Adding -enlargeonly and -reduceonly switches should be easy… (Another advantage of the small-tools philosophy: they all have short and mostly simple sources.) Maybe I’ll submit a patch.
Re:Shell rules.
runrig on 2006-10-01T18:22:37
It always amazes me if people post a multiline program in Perl, when it can be done with a shell one-liner.I normally don't do that, but in this case, I was not aware of the command line tools (I've heard of ImageMagick for years now, but have only just now actually used it), and I only installed the PerlMagick package (on Windows w/ppm). If I do install the whole ImageMagick package, I'll have to do something about that 'convert' command, as there is already a 'convert' on Windows, which does something entirely different (an unfortunately generic name for a command IMHO). I agree that shell does rule (I do have unix utilities installed), but usually perl rules over Window's "cmd.exe". Either way, almost anything rules over manually opening the pictures in MSPaint, and scaling them there.
I hate ImageMagick. It’s slow and its algorithms often produce low-quality results. I don’t like trotting out Perl for small tasks involving fiddling with a lot of files either, the command line interface of ImageMagick is execrable. I use ImageMagick only when I am desperate.
So I recommend NetPBM instead, which consists of a big bag of little programs that you connect together using pipes – a highly intuitive metaphor for image manipulation, I find. A shell script using NetPBM for your task might look like the following:
for f in *.jpg ; do
jpegtopnm "$f" -exif "$f.exif" \
| pnmscale -xysize 1280 960 \
| pnmtojpeg -exif "$f.exif" \
| reservoir -o "$f"
rm "$f.exif"
done
(reservoir is a tiny tool by Simon Tatham of PuTTY fame that makes overwriting the input file of a pipe a little more convenient. Note also that the -xysize option defines a bounding box. pnmscale will scale up images to the largest size that fits into the given dimensions with respect to original aspect ratio – an option that I find infinitely useful.)
Essentially, NetPBM adds a domain-specific language for image manipulation to shell. I love it.
Re:NetPBM!
petdance on 2006-10-01T06:33:00
There's also a tool called sponge that does what you describe w/reservoir.Re:NetPBM!
Aristotle on 2006-10-01T07:13:21
For the benefit of anyone who reads this: the one in moreutils, I suppose. Thanks for the tip.