Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Image::Magick not printing all images

by jonnyfolk (Vicar)
on Dec 30, 2008 at 23:47 UTC ( #733357=perlquestion: print w/replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I've been messing around with this for ages now without finding a solution - I am taking a set of images from the internet and printing a thumbnail to my server. However when I run the script only a haphazard subset of the full set is written to file - I have to run the script a few times to get all the images

Could anyone test this to see if it an error in the code or otherwise what might be going wrong? Thanks.

#!/usr/bin/perl -w use strict; use Image::Magick; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); print "Content-type: text/html\n\n"; my @names = qw( +x200 +200 +x200 +x200 +x200 +200 +x200 +x200 ); my $count; foreach my $item (@names) { $count++; my $photo = '../photo' . $count . '.jpg'; my $image = Image::Magick->new(magick=>'JPEG'); my $x = $image->Read($item); $x = $image->Scale(width=>'50', height=> '50'); $x = $image->Write($photo); print qq~<img src="$photo"><br>~; } exit();
Additional note: All of the lines are printed, 1 to 8, but some of the 'Write' are ignored, as mentioned previously, in haphazard manner.

Replies are listed 'Best First'.
Re: Image::Magick not printing all images
by almut (Canon) on Dec 31, 2008 at 00:39 UTC

    Do you get any error messages if you print out the three $xes? (see Handling Exceptions)

    Update: in particular, note that context (i.e. string/numeric) matters here. E.g.

    $x = $image->Read(...); warn "$x" if "$x"; # string context: the error/warning message ( +if any) print 0+$x; # numeric context: the number of images read
Re: Image::Magick not printing all images
by oko1 (Deacon) on Dec 31, 2008 at 02:15 UTC

    You may also be Suffering from Buffering. For myself, whenever I'm dealing with any external resource - e.g., a database or a web server - I disable buffering by habit. I find that it prevents a number of arbitrary-seeming and difficult to troubleshoot problems.

    #!/usr/bin/perl -w use strict; # Ask Perl to pull the chain after every 'write' or 'print' $|++; # Rest of code follows

    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: Image::Magick not printing all images
by leocharre (Priest) on Dec 31, 2008 at 01:13 UTC

    Yes, as almut mentioned very wisely..

    I know Image::Magick is funny- some of the methods only return if there's an error. Ala unix.

    This is a little of the frustration of mixing c coders with perl code :-)

    So, those $x hold the error, if there is one.. so..

    my $count; foreach my $item (@names) { $count++; my $photo = '../photo' . $count . '.jpg'; my $image = Image::Magick->new(magick=>'JPEG'); my $x = $image->Read($item); $x = $image->Scale(width=>'50', height=> '50'); $x and die("Error scaling: $x"); $x = $image->Write($photo); $x and die("Error writing: $x"); print qq~<img src="$photo"><br>~; }
Re: Image::Magick not printing all images
by zentara (Archbishop) on Dec 31, 2008 at 10:46 UTC
    In addition to what has been said, I will point out that IM has a way of retaining images in an internal stack, so when you read an image in, with the same IM object, it gets pushed into an array of images. You might try to clean out the IM object after each loop run to be sure
    ...... $x = $image->Write($photo); undef @$image; ......
    Even though you are calling a new IM object each time thru the loop, it may be that the object is not "loop-safe" for some reason....possibly a bug or an old version that needs updating? But your symptoms point to this, and it's worth a try.

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://733357]
Approved by Perlbotics
[Discipulus]: good morning monks and nuns!

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-02-22 08:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.