rjahrman has asked for the wisdom of the Perl Monks concerning the following question:

Basically, I'm going to be converting several thousand (maybe MORE) 24-bit and 32-bit BMPs to JPG format. Rather than doing it by hand, does anyone know of a cool Perl way to do this? If not, that's OK--I could always do this in C++ (but Perl's better for little things like this).

Replies are listed 'Best First'.
Re: Convert BMP to JPG?
by hawtin (Prior) on Sep 14, 2003 at 08:02 UTC

    There are so many ways, you need to give more info about your system and constraints before a best one can be suggested. Just for starters:

    • Lots of CPAN modules, for example ImageMagik, IPG's JPEG library, NetPBM, libpng (and that's just the older ones)
    • Scipting in GIMP (in Perl of course) via the Gimp::Fu
    • On Unix like boxes you can install the pbm/pnm software and control it with Perl
    • On Windows boxes there are lots of graphics manipulation program that support scripting (I have an old version of CorelPaint that I sometimes use)

    Do you just want BMP to JPG conversion? Do you need to resample images (change size or colour depth)? Do you need to control the output quality? What platform are you running on? Are you the sys admin?

    It is certainly better to use Perl for this type of stuff but exactly how you use it depends on what you are trying to achieve

      domm pointed me to
      Imager - Perl extension for Generating 24 bit Images
      definitely also worth a try.
      the main advantage over imagemagick and gd is that it doesn't need any additional programs installed to run (just libpng, libjpeg and libungif). compiling imagemagick on a distribution without Xfree-RPM (like trustix linux can be a pain in the ass, since it needs lots of X-headers ...
Re: Convert BMP to JPG?
by Anonymous Monk on Sep 14, 2003 at 10:48 UTC
    Why not just use "find" and "convert" (from ImageMagick)?

    find . -type f -name '*.bmp' -exec convert {} {}.jpg \;

Re: Convert BMP to JPG?
by Anonymous Monk on Sep 14, 2003 at 09:05 UTC

    Do you really have to write a script?

    If you dont and want to do it the 'lazy' way use 'bmptoppm' to 'ppmtojpeg'


    xluther
Re: Convert BMP to JPG?
by surrealistfashion (Acolyte) on Sep 14, 2003 at 10:09 UTC
    with win XP, they convert format when you rename the file if 'show known file types' is set in the folder option. I converted about 400 bmp's with something simliar to the code on the bottom, my attempts were to change the names to numbers, and change files types.
    opendir (PICS, "path/to/pics") || die ""; while ($name = readdir PICS) { if ($name =~ m/bmp$/i) { $numb++; rename $name, $numb . "jpg"; } }
    JPL
      This doesn't convert the file! It just renames it, and windows displays it as the new type because of the extension.
      oh sweet baby Jesus, that's some scary stuff ( foo.SueMe .... owe micro$oft millions of dollars instantaneously, scary ).
Re: Convert BMP to JPG?
by zakzebrowski (Curate) on Sep 14, 2003 at 15:24 UTC
    As a previous node says, it all depends on what you are trying to do. A great freeware windows solution is Ifranview. Simply choose File -> batch -> choose input directory -> add all ; select new output directory -> batch coversion -> jpeg -> (possibly advanced options, where you can crop, resize, color manip, etc.) -> start. It has support for many different types of images (including some very odd ones) and is very powerfull.

    ----
    Zak
Re: Convert BMP to JPG?
by bart (Canon) on Sep 15, 2003 at 07:57 UTC
    The IJG JPEG library source, which is, BTW , the JPEG library all the mentioned open source image manipulation uses, comes with some command line tools, including cjpeg. The quality is excellent, I can testify for that.

    If you look around, you can find binaries for many platforms, for example, here are the 32-bit DOS binaries, and I'm sure Cygwin binary versions are available for download somewhere, too.

Re: Convert BMP to JPG?
by rjahrman (Scribe) on Sep 14, 2003 at 15:53 UTC
    Thanks all! I decided to go with IFranView. That really saved me a headache. :)