Problem converting PNG to JPEG

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
mlorenz_ca

Problem converting PNG to JPEG

Post 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!
mlorenz_ca

Re: Problem converting PNG to JPEG

Post by mlorenz_ca »

Scratch that - the code I posted is fine after all. The problem was elsewhere.
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: Problem converting PNG to JPEG

Post 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 !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem converting PNG to JPEG

Post 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?
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: Problem converting PNG to JPEG

Post 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 :-/
Post Reply