# Image::Magick jpeg compression - PERL Miscellaneous

Hi all, I've been banging my head against a wall trying to use Image::Magick to generate jpeg compressed thumbnails. Here's latest iteration of the code I've been using; my $image = Image::Magick->new;$image->Read(filename=>$source);$image->Resize(width=>400, height=>300); $image->Write(filename=>$output); $image->Resize(width=>100, height=>75);$image->Set(Quality=>10); $image->Write(filename=>$thumb, compression=>'JPEG'); undef $image; Basically, I'm trying to read a source image, resize to 400x300 and write that, then resize to 100x75 and then write that. I notice that the typical filesize I get for the 400x300 image is between 50-60k, however the filesize for the thumbnail is roughly 25k. Using Paintshop Pro I can produce compressed images of around 5k. ... 1. ## Image::Magick jpeg compression Hi all, I've been banging my head against a wall trying to use Image::Magick to generate jpeg compressed thumbnails. Here's latest iteration of the code I've been using; my$image = Image::Magick->new;
$image->Read(filename=>$source);
$image->Resize(width=>400, height=>300);$image->Write(filename=>$output);$image->Resize(width=>100, height=>75);
$image->Set(Quality=>10);$image->Write(filename=>$thumb, compression=>'JPEG'); undef$image;

Basically, I'm trying to read a source image, resize to 400x300 and
write that, then resize to 100x75 and then write that. I notice that
the typical filesize I get for the 400x300 image is between 50-60k,
however the filesize for the thumbnail is roughly 25k. Using
Paintshop Pro I can produce compressed images of around 5k.

Any pointers as to what I'm doing wrong would be great, as I find the
doentation on the Image::Magick website a bit short on detail.

thanks,
Andrew

Andrew Harton Guest

2. ## Re: Image::Magick jpeg compression

Andrew Harton wrote:
> Hi all,
>
> I've been banging my head against a wall trying to use Image::Magick
> to generate jpeg compressed thumbnails.
>
> Here's latest iteration of the code I've been using;
> my $image = Image::Magick->new; >$image->Read(filename=>$source); >$image->Resize(width=>400, height=>300);
> $image->Write(filename=>$output);
> $image->Resize(width=>100, height=>75); >$image->Set(Quality=>10);
> $image->Write(filename=>$thumb, compression=>'JPEG');
> undef $image; > > Basically, I'm trying to read a source image, resize to 400x300 and > write that, then resize to 100x75 and then write that. I notice that > the typical filesize I get for the 400x300 image is between 50-60k, > however the filesize for the thumbnail is roughly 25k. Using > Paintshop Pro I can produce compressed images of around 5k. > > Any pointers as to what I'm doing wrong would be great, as I find the > doentation on the Image::Magick website a bit short on detail. > > thanks, > Andrew I've since realised that 'Quality' should have been 'quality', and I've tried my$image = Image::Magick->new;
$image->Read(filename=>$source);
$image->Resize(width=>400, height=>300);$image->Write(filename=>$output);$image->Resize(width=>100, height=>75);
$image->Set(compression=>'JPEG');$image->Set(quality=>90);
$image->Write(filename=>$thumb);
undef $image; to see if that made a difference, but with no luck - the output filesize remains unchanged, no matter what value I put in for the quality. Andrew Andrew Harton Guest 3. ## Re: Image::Magick jpeg compression >>>>> "Andrew" == Andrew Harton <andrew_hartonagilent.com> writes: Andrew> I've since realised that 'Quality' should have been 'quality', Given the arcane interface of ImageMagick, I wouldn't be surprised if it wasn't "uality". :-) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! Randal L. Schwartz Guest 4. ## Re: Image::Magick jpeg compression "Andrew Harton" <andrew_hartonagilent.com> wrote: : I've been banging my head against a wall trying to use Image::Magick : to generate jpeg compressed thumbnails. : : Here's latest iteration of the code I've been using; : my$image = Image::Magick->new;
: $image->Read(filename=>$source);
: $image->Resize(width=>400, height=>300); :$image->Write(filename=>$output); :$image->Resize(width=>100, height=>75);
: $image->Set(Quality=>10); :$image->Write(filename=>$thumb, compression=>'JPEG'); : undef$image;
:
: Basically, I'm trying to read a source image, resize to 400x300 and
: write that, then resize to 100x75 and then write that. I notice that
: the typical filesize I get for the 400x300 image is between 50-60k,
: however the filesize for the thumbnail is roughly 25k. Using
: Paintshop Pro I can produce compressed images of around 5k.
:
: Any pointers as to what I'm doing wrong would be great, as I find the
: doentation on the Image::Magick website a bit short on detail.

I agree.

I recall seeing what you describe with a thumbnail generator of my
own. The files shrank dramatically when I inserted a call to the
Profile() method, which removed a huge chunk of data that was being
carried forward from the full-size image to the thumbnail.

$magick->Read($ifile );
$magick->Profile();$magick->Resize( width=>$x, height=>$y );
$magick->Write($ofile );

I'm not sure anymore what an "ICC or IPTC image profile" is or why it
has to be removed manually, but it made sense back when I wrote the
program.

IOW, I don't know what I'm doing, but it did the trick.
Cargo cult programming is bad, mmkay?

Jay Tilton Guest

5. ## Re: Image::Magick jpeg compression

Andrew Harton wrote:
> Andrew Harton wrote:
>
>>Hi all,
>>
>>I've been banging my head against a wall trying to use Image::Magick
>>to generate jpeg compressed thumbnails.
I had the same problem a while back, and likewise found the docs a bit
sp. Guessing you're on a M$platform (message headers??), which should have added the ImageMagick *system* executables to your path. Its a kludge, using convert, which in the doentation ([url]http://studio.imagemagick.org/www/convert.html):[/url] "converts an input file using one image format to an output file with a differing image format." Anyway, tested on W2K, ImageMagick 5.5.7, Activestate 635: use strict; use warnings; use File::Basename; use Image::Magick; resize_jpgs(ARGV); #pass in full pathnames sub resize_jpgs { my (files) = _; my sizes = qw[400 100]; foreach my$file(files) {
foreach my $size(sizes) { my ($name,$path) = filep($file);
my $new_jpg = ($size == 400) ? "$path/s-$name" : "$path/t-$name";
my args =
('convert','-size',$size,$file,'-resize',$size,'+profile','"*"',$new_jpg);
# system(args) == 0 or warn "Couldn't resize $file\n"; system(args); } } } You *will* get warnings to the effect that the file couldn't be opened, but it works nonetheless. System returns 1...anyone have an idea why return value isn't zero, despite the fact that the conversions succeeed? HTH - keith ko Guest 6. ## Re: Image::Magick jpeg compression On Thu, 21 Aug 2003 21:35:51 GMT, Jay Tilton <tiltonjerols.com> wrote: > "Andrew Harton" <andrew_hartonagilent.com> wrote: >: Basically, I'm trying to read a source image, resize to 400x300 and >: write that, then resize to 100x75 and then write that. I notice that >: the typical filesize I get for the 400x300 image is between 50-60k, >: however the filesize for the thumbnail is roughly 25k. Using >: Paintshop Pro I can produce compressed images of around 5k. > I recall seeing what you describe with a thumbnail generator of my > own. The files shrank dramatically when I inserted a call to the > Profile() method, which removed a huge chunk of data that was being > carried forward from the full-size image to the thumbnail. > >$magick->Read( $ifile ); >$magick->Profile();
It's probably clearer to use Strip() when your intention is stripping
the image of its profiling information. Strip() also removes comments
and such, so if you want to retain those, using Profile() is the right
thing to do.

The identify tool that comes with the ImageMagick distribution can tell
you whether your original image has a profile, and how large it is.

It is also probably not a bad idea to make sure that you adjust the
image compression quality to something lower, or at least check that
it's low enough. Thumbnails generally can get away with low quality
compression.
> I'm not sure anymore what an "ICC or IPTC image profile" is or why it
> has to be removed manually, but it made sense back when I wrote the
> program.
Image::Magick tends to carry all information in an image forward, if
possible, even non-visible information. The Strip() method is there to
get rid of all non-visible data.

\begin{offtopic}

ICC and IPTC are just some standardised ways for adding extra
information to images.

ICC = International Color Consortium color profile
IPTC = IPTC Newsphoto profile ([url]www.iptc.org[/url])

This is not the only profile information that ImageMagick can carry
around in an image (and besides profile information, there is other
stuff as well), but I think that JPEG only has these two as
possibilities, but I could be wrong.

\end{offtopic}

Martien
--
|
Martien Verbruggen | Useful Statistic: 75% of the people make up
| 3/4 of the population.
|
Martien Verbruggen Guest

7. ## Re: Image::Magick jpeg compression

Jay & Martien,

Thanks for your input - comments below..

Martien Verbruggen wrote:
> On Thu, 21 Aug 2003 21:35:51 GMT,
> Jay Tilton <tiltonjerols.com> wrote:
>> "Andrew Harton" <andrew_hartonagilent.com> wrote:
>
>>> Basically, I'm trying to read a source image, resize to 400x300 and
>>> write that, then resize to 100x75 and then write that. I notice
>>> that the typical filesize I get for the 400x300 image is between
>>> 50-60k, however the filesize for the thumbnail is roughly 25k.
>>> Using Paintshop Pro I can produce compressed images of around 5k.
>
>> I recall seeing what you describe with a thumbnail generator of my
>> own. The files shrank dramatically when I inserted a call to the
>> Profile() method, which removed a huge chunk of data that was being
>> carried forward from the full-size image to the thumbnail.
>>
>> $magick->Read($ifile );
>> $magick->Profile(); > > It's probably clearer to use Strip() when your intention is stripping > the image of its profiling information. Strip() also removes comments > and such, so if you want to retain those, using Profile() is the right > thing to do. > > The identify tool that comes with the ImageMagick distribution can > tell you whether your original image has a profile, and how large it > is. I've tried using both Strip() and Profile(), and neither has any effect on output filesize. In both cases, I called the method immediately after reading the image, and before doing any resizing. I've set compression->'JPEG', and tried various quality settings, with no difference in results, and I've tried setting the compression and quality separately, and alternatively as part of the image write, in case that would make a difference (it didn't). Attached below is the last iteration of my attempts, still unsuccessful. The original images are 1600x1200 Jpeg files, and the identify tool didn't show them as having any particular profile information. By the way, I'm using ImageMagick 5.5.7-Q16 binary download for Windows (dlls, not static), in case that makes a difference. > It is also probably not a bad idea to make sure that you adjust the > image compression quality to something lower, or at least check that > it's low enough. Thumbnails generally can get away with low quality > compression. That's exactly why I'm trying to get this sorted out - I don't want 24kb full-quality thumbnails when they'd be just as good with lower quality and 3-5k in size. Andrew -- # This is the entire sub that I call, with the destination directory, # followed by a list of the files to be converted. sub copy_images { my$destdir = shift;
my images = _;
foreach my $i (0..$#images)
{
my $source =$images[$i].".jpg"; print "copy$source";
my $output =$destdir.$images[$i].".jpg";
my $thumb =$destdir.$images[$i]."_thumb.jpg";

my $image = Image::Magick->new;$image->Read(filename=>$source);$image->Profile();
$image->Resize(width=>400, height=>300);$image->Write(filename=>$output);$image->Resize(width=>100, height=>75);
$image->Write(filename=>$thumb, compression=>'JPEG', quality=>10);
undef $image; print "\tDone\n"; } } Andrew Harton Guest 8. ## Re: Image::Magick jpeg compression On Tue, 26 Aug 2003 15:16:45 +0100, Andrew Harton <andrew_hartonagilent.com> wrote: > Jay & Martien, >> The identify tool that comes with the ImageMagick distribution can >> tell you whether your original image has a profile, and how large it >> is. > > I've tried using both Strip() and Profile(), and neither has any effect > on output filesize. In both cases, I called the method immediately after > reading the image, and before doing any resizing. > The original images are 1600x1200 Jpeg files, and the identify tool didn't > show them as having any particular profile information. Could you maybe put one of those images in a spot where we can get to it? A web server or something? I'd be interested to see why ImageMagick does such a bad job at them. You said that PhotoShop does compress them nicely? > By the way, I'm using ImageMagick 5.5.7-Q16 binary download for Windows > (dlls, > not static), in case that makes a difference. I don't know whether it would make a difference. Unless it uses an external JPEG library (it does on my platform, a linux distribution) and yours happens to be broken, it shouldn't make a difference. Martien -- | Martien Verbruggen | If at first you don't succeed, destroy all Trading Post Australia | evidence that you tried. | Martien Verbruggen Guest 9. ## Re: Image::Magick jpeg compression Martien Verbruggen wrote: > On Tue, 26 Aug 2003 15:16:45 +0100, > Andrew Harton <andrew_hartonagilent.com> wrote: >> Jay & Martien, > >>> The identify tool that comes with the ImageMagick distribution can >>> tell you whether your original image has a profile, and how large it >>> is. >> >> I've tried using both Strip() and Profile(), and neither has any >> effect on output filesize. In both cases, I called the method >> immediately after reading the image, and before doing any resizing. > >> The original images are 1600x1200 Jpeg files, and the identify tool >> didn't show them as having any particular profile information. > > Could you maybe put one of those images in a spot where we can get to > it? A web server or something? I'd be interested to see why > ImageMagick does such a bad job at them. You said that PhotoShop does > compress them nicely? I've put two of the original images, along with my entire script here : [url]http://www.clri.net/image_prob[/url] The script has a number of hard-coded items in it, and there should be /event_details/ and /images/events/ in place below the base directory before it'll run. Irrespective of the quality setting I use within the script, the thumbnails come out at around 25kb, whereas Paintshop Pro will bring them down to around 2kb. I've had to run all of the thumbnails through Paintshop Pro, because we don't have the space to leave them uncompressed. thanks & regards, Andrew --$_="8fe403bc47120d12a0635912";$#=" eratcHhJklnoPsu"; split//;for(0..$#_){print substr($#,hex$_[$_],1);} Andrew Harton Guest 10. ## Re: Image::Magick jpeg compression On Thu, 28 Aug 2003 12:06:29 +0100, Andrew Harton <andrew_hartonagilent.com> wrote: > Martien Verbruggen wrote: >> On Tue, 26 Aug 2003 15:16:45 +0100, >> Andrew Harton <andrew_hartonagilent.com> wrote: >>> Jay & Martien, >> >>>> The identify tool that comes with the ImageMagick distribution can >>>> tell you whether your original image has a profile, and how large it >>>> is. >>> >>> I've tried using both Strip() and Profile(), and neither has any >>> effect on output filesize. In both cases, I called the method >>> immediately after reading the image, and before doing any resizing. >> >>> The original images are 1600x1200 Jpeg files, and the identify tool >>> didn't show them as having any particular profile information. It didn't? Which version of ImageMagick are you using again? If it's really old, you should consider upgrading. >> Could you maybe put one of those images in a spot where we can get to >> it? A web server or something? I'd be interested to see why >> ImageMagick does such a bad job at them. You said that PhotoShop does >> compress them nicely? > > I've put two of the original images, along with my entire script here : > [url]http://www.clri.net/image_prob[/url] > The script has a number of hard-coded items in it, and > there should be /event_details/ and /images/events/ in place below the > base directory before it'll run. Both of your images indeed have 22 kB of profile data embedded:$ identify -verbose IM000001.JPG
Format: JPEG (Joint Photographic Experts Group JFIF format)
Geometry: 1600x1200
[snip]
Profile-APP1: 22468 bytes
[snip]

$convert -geometry "100x75" +profile "" IM000001.JPG tn1p.jpg$ ls -l IM000001.JPG tn1p.jpg tn1g.jpg
-rw-r--r-- 1 mgjv users 571286 Aug 29 15:31 IM000001.JPG
-rw-r--r-- 1 mgjv users 2037 Aug 29 15:40 tn1p.jpg
-rw-r--r-- 1 mgjv users 2035 Aug 29 15:33 tn1g.jpg

[tn1g.jpg is the result of a resize in the GIMP, tn1p.jpg the result
of ImageMagick]

In other words: ImageMagick compresses these images to the same size
as the GIMP, and comparable numbers to your Paint Shop Pro numbers.
All you need to do is remove the profiling info.

The following Perl program does the same work as the abovequoted
convert command:

$cat foo #!/usr/local/bin/perl5 -w use strict; use Image::Magick; ARGV == 2 or die; my$im = Image::Magick->new;
my $rc =$im->Read($ARGV[0]); die$rc if $rc;$im->Profile();
$rc =$im->Resize("100x75");
die $rc if$rc;
$im->Write($ARGV[1]);

$./foo.pl IM000001.JPG tn2.jpg$ ls -l tn2.jpg
-rw-r--r-- 1 mgjv users 2037 Aug 29 15:50 tn2.jpg

HTH,
Martien
--
|
Martien Verbruggen | True seekers can always find something to
Trading Post Australia | believe in.
|
Martien Verbruggen Guest

11. ## Re: Image::Magick jpeg compression

Martien Verbruggen wrote:
> The following Perl program does the same work as the abovequoted
> convert command:
>
>
> $cat foo > #!/usr/local/bin/perl5 -w > use strict; > use Image::Magick; > > ARGV == 2 or die; > > my$im = Image::Magick->new;
> my $rc =$im->Read($ARGV[0]); > die$rc if $rc; >$im->Profile();
> $rc =$im->Resize("100x75");
> die $rc if$rc;
> $im->Write($ARGV[1]);
>
>
> $./foo.pl IM000001.JPG tn2.jpg The program *doesn't* work on one of my boxes running Activestate Perl 5.6.1 W2K. Read() failed. If you comment out the die for its return value, the image is resized, but the profile information is not removed, even though the return value from Profile() is 0. When I first installed ImageMagick I basically tried to do the same thing with the Perl API to no avail, that's why my earlier post uses system() instead of the Image:Magick module. (I *did* neglect to take out 'use Image::Magick;' from my post, that's leftover from my failed attempts to use the module) Tested the program on another box running Activestate Perl 5.8.0. It does *exactly* what its supposed to, and the thumb is 1.98KB. keith ko Guest 12. ## Re: Image::Magick jpeg compression On Fri, 29 Aug 2003 22:40:54 +0900, ko <kuujinbohotmail.com> wrote: > Martien Verbruggen wrote: >> The following Perl program does the same work as the abovequoted >> convert command: >> >> >>$ cat foo
>> #!/usr/local/bin/perl5 -w
>> use strict;
>> use Image::Magick;
>>
>> ARGV == 2 or die;
>>
>> my $im = Image::Magick->new; >> my$rc = $im->Read($ARGV[0]);
>> die $rc if$rc;
>> $im->Profile(); >>$rc = $im->Resize("100x75"); >> die$rc if $rc; >>$im->Write($ARGV[1]); >> >> >>$ ./foo.pl IM000001.JPG tn2.jpg
>
> The program *doesn't* work on one of my boxes running Activestate Perl
> 5.6.1 W2K. Read() failed. If you comment out the die for its return
> value, the image is resized, but the profile information is not removed,
> even though the return value from Profile() is 0.
That sounds to me like there is a problem with that installation then.
Either the ImageMagick is broken, or there is some mismatch between the
ImegaMagick module and the libraries, maybe. I don't know that much
about using or installing IM on windows, so I can't suggest anything
more than remove the current installations, completely, and reinstall.
> Tested the program on another box running Activestate Perl 5.8.0. It
> does *exactly* what its supposed to, and the thumb is 1.98KB.
That, to me, at least indicates that there is no fundamental problem
with the ImageMagick distributions for Windows (I would have been very,
very surprised if there had been), and that the problem more likely is
related to the specific installation you used before.

Martien
--
|
Martien Verbruggen | Think of the average person. Half of the
| people out there are dumber.
|
Martien Verbruggen Guest

13. ## Re: Image::Magick jpeg compression

ko wrote:
> Martien Verbruggen wrote:
>> The following Perl program does the same work as the abovequoted
>> convert command:
>>
>>
>> $cat foo >> #!/usr/local/bin/perl5 -w >> use strict; >> use Image::Magick; >> >> ARGV == 2 or die; >> >> my$im = Image::Magick->new;
>> my $rc =$im->Read($ARGV[0]); >> die$rc if $rc; >>$im->Profile();
>> $rc =$im->Resize("100x75");
>> die $rc if$rc;
>> $im->Write($ARGV[1]);
>>
>>
>> $./foo.pl IM000001.JPG tn2.jpg > > The program *doesn't* work on one of my boxes running Activestate Perl > 5.6.1 W2K. Read() failed. If you comment out the die for its return > value, the image is resized, but the profile information is not > removed, even though the return value from Profile() is 0. When I > first installed ImageMagick I basically tried to do the same thing > with the Perl API to no avail, that's why my earlier post uses > system() instead of the Image:Magick module. (I *did* neglect to take > out 'use Image::Magick;' from my post, that's leftover from my failed > attempts to use the module) > > Tested the program on another box running Activestate Perl 5.8.0. It > does *exactly* what its supposed to, and the thumb is 1.98KB. > > keith I had the same problem & results when trying to run the above program. I got an error 330, with a message that a delegate couldn't be found, unless the die.. line was commented out. I tried a number of things, such as modifying the path to also explicitly point at the directory in question (it's a subdirectory of the ImageMagick install directory), but all with no luck. I tried reinstalling ImageMagick with static libraries, rather than dlls, and also reinstalled Image::Magick using PPM. Nothing changed the situation - my thumbnails were all still too big. I upgraded to Perl 5.8.0, but when I tried to install the Image::Magick module, I got an error message that there was no install source for Image-Magick-Thumbnail. I've now successfully used a slightly modified version of what you proposed in your other post, using a system call to 'convert', and this works perfectly - generated files are a quality and size that are perfect for my needs. Thanks very much for all the help. regards, Andrew --$_="8fe403bc47120d12a0635912";$#=" eratcHhJklnoPsu"; split//;for(0..$#_){print substr($#,hex$_[\$_],1);}

Andrew Harton Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•