Page 1 of 1

Problem converting PNG to JPEG

Posted: 2008-04-25T11:41:16-07:00
by mlorenz_ca
I'm trying to use PerlMagick to convert a PNG to a JPEG, in memory, and I want to return the resulting BLOB. Although I can do a command line conversion without any problems, in code the image always comes out as a PNG. Here's what the code looks like:

Code: Select all

    my $image = Image::Magick->new;

    # $imagebuffer is the BLOB of the image
    if( 1 != $image->BlobToImage( $imagebuffer ) ) {
        undef $imagebuffer;
        $log->logcroak("2:ImageMagick:BlobToImage() failed: $!");
    }

    # convert non jpeg images to jpeg
    unless ($imgmagick eq "JPEG") {
        if ($imgmagick eq "PNG" || $imgmagick eq "GIF") {
            # Going for lossless conversion
            $image->Set(quality=>100,compression=>'none',magick=>'jpeg');
            $image->Strip();
        }
    }

    $jpegRawImageBuffer = $image->ImageToBlob();
    return $jpegRawImageBuffer;
Is it possible to do this at all? If it is, is the above even close to correct, or am I missing something really simple?

If someone has working code, I'd appreciate a posting - thanks!

Re: Problem converting PNG to JPEG

Posted: 2008-04-25T13:18:53-07:00
by mlorenz_ca
Scratch that - the code I posted is fine after all. The problem was elsewhere.

Re: Problem converting PNG to JPEG

Posted: 2012-10-12T16:53:55-07:00
by Alexvb6
Hi,

I am experiencing the same issue // I'm simply using :
convert source.png destination.jpg

and the output .JPG file is still in PNG fileformat.
This file is not opening, but if I change extension to PNG, it opens ! So it's still PNG file format .. :-/

Do you have ANY clue to save my brain ? ;-)
Thank you very very much !

Re: Problem converting PNG to JPEG

Posted: 2012-10-12T17:39:08-07:00
by fmw42
Alexvb6 wrote:Hi,

I am experiencing the same issue // I'm simply using :
convert source.png destination.jpg

and the output .JPG file is still in PNG fileformat.
This file is not opening, but if I change extension to PNG, it opens ! So it's still PNG file format .. :-/

Do you have ANY clue to save my brain ? ;-)
Thank you very very much !
This is probably a different topic to be posted in the User's forum, since you are apparently not using Perlmagick but simply the command line.

Do you have libjpg installed? What version of IM and what platform?

Check your delegates,

convert -list configure | grep -i "delegates"

See if jpg is really there?

Do you get any error messages?

Re: Problem converting PNG to JPEG

Posted: 2012-10-15T06:08:08-07:00
by Alexvb6
Hi, 'Im using the last Imagemagick version available,
but the error was on my side, and so silly : I was specifiying .png in the Output filename , instead of .jpg

... It now works (obviously) !

fmw42, I thank you very much for your long and detailed answer,
... I feel so confuse :-/