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

Does anyone know how to reduce the image file size using image magick. It seems when I use the Scale method the image file size is reduced but does not reduce the file size enough on larger images (i.e. > 300k). I have tried using the Sample method but do not see any file size reduction that is different from the Scale method. I would like to take a 300k+ image and resample to 35 to 40k. Below is a snippet of how I am trying to sample:
$geometry = ($newwidth x $newheight); $im->Sample(geometry => $geometry); $im->Write("$magick_full");
Thank you for your input.

David K.

Replies are listed 'Best First'.
Re: Image Magick - Sample vs. Scale
by caedes (Pilgrim) on Feb 28, 2003 at 07:54 UTC
    There is only a loose correlation between an image's spacial size (in pixels) and it's filesize when encoded. It is mainly dependant on the image's content whether you'd be better off using JPEG, GIF, or PNG compression (among others). Also keep in mind that ImageMagick's Sample method yields best quality when you specify the filter attribute (this makes the interpolation more accurate).

    -caedes

Re: Image Magick - Sample vs. Scale
by crouchingpenguin (Priest) on Feb 28, 2003 at 12:12 UTC

    You can use Image::Thumbnail (which uses Image Magick) like so:

    #!/usr/bin/perl use strict; use warnings; use Image::Thumbnail; + my $image_name = 'picture.jpg'; my $output_image_name = 'picture_thumb.jpg'; + my $t = new Image::Thumbnail( size => 80, create => 1, inputpath => $image_name, outputpath => $output_image_name );

    Update: Oops, after drinking my morning cup I noticed the question wasn't concerning the actual image size.


    cp
    ---
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Image Magick - Sample vs. Scale
by PodMaster (Abbot) on Feb 28, 2003 at 07:06 UTC
    What image format are you using? That's where the file size lies (in the format). I suggest you use PNG.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      I'd have to disagree about PNG if this is something that will reside on a website. It's an odd thing but I had a problem where any NT 4.x computer running IE 4.* would totally freeze solid and requirie a hard reset when trying to view a client site. It turned out is was becasue of a PNG on the site. Granted they are both old software but if appeasing consumers is the goal, giving them a hard boot is not the way to do it.

      -Lee

      "To be civilized is to deny one's nature."

        So because of a few people who refuse to upgrade their ancient software, we shouldn't use new things that save time and bandwidth for the other 99.999% of browsers? Guess it's back to gopher then!

      The format is JPG which should have no bearing in the ultimate file size. I guess what I'm trying to determine is the difference between Sample and Scale. The Image Magick website says that Sample scales an image with pixel sampling. This leads me to believe there should be a reduction in the number of pixels and ultimately the file size. Is this true? I've resampled images in photo editors before (i.e. Image Expert) and successfully reduced a file size from 300k to 40k without compromizing web photo quality. Again, I am not reducing the image size just the file size.

      Any help would be appreciated. Thank you.

      David K.

        Did you have a look at the quality option? From the docs it looks like this might be the way to go.

        -- Hofmator