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

I have been reducing the dimensions of a jpeg file using perlmagick but this does not get the file size down. Looking around on google I found the following command line but I don't understand how that converts to a perl script. Can anyone help?
convert -strip -scale 3%x3% -quality 70 bigimage.jpg thumbnail.jpg

Replies are listed 'Best First'.
Re: imagemagick file size reduction
by CountZero (Bishop) on Mar 22, 2008 at 09:42 UTC
    use strict; use Image::Magick; my $p = new Image::Magick; $p->Read('c:/data/eps/cookie.jpg'); $p->Resize(geometry=>'100x80'); $p->Write('c:/data/eps/cookie_new.jpg');
    This reduced a 40 Kbytes jpeg-image to a 4 Kbytes image.

    Update: I tried your command line and it does reduce the file size. Maybe we have different (versions of) ImageMagick programs running? I run "ImageMagick 6.3.4 06/14/07 Q16" on Windows XP Pro

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hi,

      Thanks for your reply. I have an 84kb image and just using Resize(width=>$x, height=>$y) reduced to around 62kb. using your geometry reduces further to 56kb. On further reading I have used $x = $image->Thumbnail (width=>160, height=>$r); and this has got it down to 23kb! (But a partial reason for the reduction is I have reduced the resulting file dimension from width 180 to 160. Still it's a pleasing result!)

      I am using rented server which doesn't have telnet so I need to write the command line stuff into an actual script to action it, and I don't know how to do that. I'd be interested to know if it further reduces file size.

        If you are desparate to get size down, you can add a quality option, it will start to make things grainy, if below .75.
        $image->Write($outfile.jpg, compression => 'JPEG', quality=>.5); # I think it goes 0 to 1 for quality, ImageMagick has minimal # perl docs, so groups.google.com search for examples

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        if you just wish to execute the command line, use system:

        system 'convert -strip -scale 3%x3% -quality 70 bigimage.jpg thumbnail.jpg'

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: imagemagick file size reduction
by zentara (Cardinal) on Mar 22, 2008 at 13:45 UTC
    Here is a way to do batch mode reducing, efficiently. ( code adapted from an example by Merlyn that reuses the IM object )
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $image = Image::Magick->new; umask 0022; my @pics= <*.jpg>; foreach my $pic (@pics){ my ($picbasename) = $pic =~ /^(.*).jpg$/; my $ok; $ok = $image->Read($pic) and warn ($ok); my $thumb = $picbasename . '-t.jpg'; $image->Scale(geometry => '100x100'); $ok = $image->Write($thumb) and warn ($ok); undef @$image; #needed if $image is created outside loop print "$pic -> $thumb\n"; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum