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

The problem is pretty straight forward: I have a jpeg file and I want to convert it to bmp -- In Windows (ActiveState perl).
The candidates would seem to be ImageMagick and Imager. Of those, only Imager seems to be supported in PPM. After installing Imager on Windows, I discovered the only supported formats are:
bmp rgb w32 pnm tga ifs raw
(from keys %Imager::formats)
It won't read JPEGs. Gives an error if I try.

Can anyone point me to how to install a JPEG reading, BMP outputting version of ImageMagick or Imager on ActiveState Windows Perl 5.6? (Or any other suggestion for converting a JPEG -> 24 or 32 bit windows BMP). Thanks

Replies are listed 'Best First'.
Re: jpg to bmp in Windows
by BrowserUk (Patriarch) on Jul 15, 2005 at 20:27 UTC

    If you have GD this will work for 24-bit color jpgs:

    #! perl -slw use strict; use GD; my $img = GD::Image->newFromJpeg( $ARGV[ 0 ], 1 ) or die "$ARGV[ 0 ] : $!"; my( $width, $height ) = $img->getBounds(); ( my $bmpFile = $ARGV[ 0 ] ) =~ s[.jpg$][.bmp]i; open BMP, '>:raw', $bmpFile or die "$bmpFile : $!"; binmode BMP; printf BMP '%s', pack 'A2ISSI', 'BM', $width * $height +54, 0, 0, 54; printf BMP '%s', pack 'IiiSSIIiiII', 40, $width, $height, 1, 24, 0, $width * $height, 11811, 11811, 0, +0; for my $y ( reverse 0 .. $height-1 ) { for my $x ( 0 .. $width-1 ) { printf BMP '%s', pack 'CCC', reverse $img->rgb( $img->getPixel +( $x, $y ) ); } } close BMP;

    Other color depths take a little more effort and it could be sped up quite easily.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Why isn't this a member function of GD::Image?

      -Paul

        Probably because the underlying GD Library doesn't have support for Windows .BMPs.

        And that's probably because there is no existing open C library available for load/saving .BMPs as there are for .jpgs, .gifs etc.

        And that maybe, though I haven't checked, because MS have never freely published the spcs for .BMPs?

        Although my snippet works, for simple, uncompressed BMPs, the references I've seen suggest that 'the spec' has a multitude of extensions.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Why does this only work with 400x300pix images?
      I have a 600x240 image. The bmp will be created, but i can't look at it nor use it.
      Do i have to change some of these lines to work correctly?

        There are no size limitations with the code. I just ran it on the biggest .jpg I had kicking around and it ran fine. Slow, but fine:

        c:\test\img\tmp>imageinfo --verbose worldx2-2.jpg worldx2-2.jpg File format: JPEG MIME type: image/jpeg Width (pixels): 5400 Height (pixels): 2700 Bits per pixel: 24 Number of images: 1 Physical width (dpi): 72 Physical height (dpi): 72 Physical width (inches): 75 Physical height (inches): 37.500000 Number of textual comments: 0 c:\test\img\tmp>..\..\Jpg2bmp.pl worldx2-2.jpg c:\test\img\tmp>imageinfo --verbose worldx2-2.bmp worldx2-2.bmp File format: BMP MIME type: image/bmp Width (pixels): 5400 Height (pixels): 2700 Bits per pixel: 24 Number of images: 1 Physical width (dpi): 300 Physical height (dpi): 300 Physical width (inches): 18 Physical height (inches): 9 Number of textual comments: 0

        However, as noted above, it does only work for 24-bit images! I've updated the node with a slightly modified version that will abort if the image is not 24-bit.

        It could be modified to handle palette images but it is a fair amount of work and there are far better (faster & more comprehensive) tools out there for performing image conversions.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: jpg to bmp in Windows
by Mr. Muskrat (Canon) on Jul 16, 2005 at 00:39 UTC
      Thank You for all answers. I installed the Image::Magick ppm from the Bribes PPM repository and it worked perfectly.

      For future seekers of knowledge, the address of the repository is:
      http://www.bribes.org/perl/ppm
      Some useful repositories per CPAN
      http://cpan.uwinnipeg.ca/htdocs/faqs/ppm.html

      • http://crazyinsomniac.perlmonk.org/perl/ppm, for the crazyinsomniac Perl 5.6 repository
      • http://crazyinsomniac.perlmonk.org/perl/ppm/5.8, for the crazyinsomniac Perl 5.8 repository
      • http://www.bribes.org/perl/ppm, for the www.bribes.org Perl 5.6 and 5.8 repository
      • http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer, for the uwinnipeg Perl 5.6 repository
      • http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer58, for the uwinnipeg Perl 5.8 repository
Re: jpg to bmp in Windows
by socketdave (Curate) on Jul 15, 2005 at 19:11 UTC
    I've not installed it, but it looks like Apache-ImageMagick is available with PPM.

    UPDATE:

    This actually looks like a set of hooks to the ImageMagick library, but it do seem to indicate that ImageMagick should work...

    All you need to do is install ImageMagick on your machine. During the install, it asks if it should install perlmagick for activestate perl. This is a standard module for using ImageMagick (docs here):
    http://search.cpan.org/~jcristy/PerlMagick-6.20/Magick.pm

    UPDATE 2:

    This should get you going with ImageMagick, if you decide to use it:
    use Image::Magick; my($image, $x); $image = Image::Magick->new; $x = $image->Read('test.jpg'); warn "$x" if "$x"; $x = $image->Write('test.bmp'); warn "$x" if "$x";

    Hope this helps...
Re: jpg to bmp in Windows
by ww (Archbishop) on Jul 15, 2005 at 19:10 UTC
    ... tricky name spaces:
    ppm> s ImageMagick Searching in Active Repositories 1. Apache-ImageMagick [2.0b7] Convert and manipulate images on the f +ly 2. Apache-ImageMagick [2.0b7] Convert and manipulate images on the f +ly ppm>
Re: jpg to bmp in Windows
by astroboy (Chaplain) on Jul 16, 2005 at 12:01 UTC
    If you install Image Magick for Windows, it will ask if you want to install the Perl libraries too