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

howdy monks

I am using perlmagick to save jpg images, but seem to lose a lot of quality by default.

the original image size is 90k. When I use:
$background->Write(filename=>"jpeg:$filename");
the size comes out as 13k and the image has lost a lot of detail

I then try:
$background->Write(filename=>"jpeg:$filename", quality=>'100');
comes out same size - 13k

tried:
$background->Write(filename=>"jpeg:$filename", compression=>'None');
comes out same size - 13k

how do I stop it compressing like this?

thanks!

Replies are listed 'Best First'.
Re: permagick jpg compression
by rhesa (Vicar) on Mar 19, 2007 at 20:05 UTC
    What operations did you perform on $background preceding the Write call? It's likely your quality loss occurs there instead of in the Write.
      nothing

      I have cut out all the code between the read and write:
      $background->Read("$filename"); $background->Write(filename=>"$newfilename", quality=>100);
      whatever the quality setting is set at, or if it is not there at all, I get the same result - the image has gone from 95k to 13k and is clearly less sharp and detailed.
        What's your version of ImageMagick?

        I can't confirm such a big reduction in size. I tried exactly your code on an image of 704104 bytes, and the output was 704085 bytes. The quality reduction wasn't appreciable either.

        FWIW, we use ImageMagick 6.1.5 compiled from source, with the latest libjpeg.

Re: permagick jpg compression
by kyle (Abbot) on Mar 19, 2007 at 20:08 UTC

    I wonder if your image is just getting compressed better but at the same quality. If you want to test this, convert the original image and a resulting 13K image both to a lossless format and compare them. Comparisons would be easy in Portable_pixmap format.

      I second this. If i execute following code on a file, i get a small reduction in file size, but no loss in quality. If i execute the same code with quality '10', i get a much smaller file, but also a great loss in quality.
      use Image::Magick; my $image = Image::Magick->new; my $x = $image->Read('test.jpg'); $image->Set(compression => 'JPEG'); $image->Set(quality => '100'); $x = $image->Write('out.jpg');
      So it is normal to see a small decrease in file size.
Re: permagick jpg compression
by hangon (Deacon) on Mar 19, 2007 at 20:26 UTC

    It's not clear exactly what you're doing, but maybe this will help clarify things for you. JPEG is a lossy format. Any time you save a jpeg file it loses image quality. The effect is cumulative. If you reopen the file, then save it again as jpeg the image gets worse (even if you don't try to compress it further).

    The best way to deal with jpeg files is to save a master copy of the original file in a lossless format such as PNG, TIFF or even BMP. Use the master copy to edit and save your changes. When you're finished working with the image, save a final copy in JPEG format as a new file. Never edit the original jpeg file directly, you will never be able to recover the lost image quality.

Re: permagick jpg compression
by dmorgo (Pilgrim) on Mar 19, 2007 at 19:48 UTC
    Try not putting '100' in quotes:

    $background->Write(filename=>"jpeg:$filename", quality=>100);

      I tried that - same result - 13k
Re: permagick jpg compression
by Rhandom (Curate) on Mar 19, 2007 at 20:27 UTC
    If I were to guess I think you should try quality => 50. If I remember right, I would guess that quality is the percentage of the original image - so a value of 80 would mean compress to 80% of the quality of the uncompressed version. That would mean that specifying a quality of 100 is leaving compression off entirely -- which is the same as the original -- which is the same as compression => 'None'.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      but compression=>'None' does not seem to make any difference - the image is still reduced in size from 95k to 13k, and it looks much poorer
Re: permagick jpg compression
by sgifford (Prior) on Mar 20, 2007 at 03:24 UTC
    Try setting the quality in the Image::Magick constuctor, like this:
    use constant IMAGE_QUALITY => 75; my $img = Image::Magick->new(size => $size, quality => IMAGE_QUALITY); $img->Read($image->{sourcedir}."/".$image_in); $img->Write($tmp);

    I have a program written like this that seems to work. :-)