Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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( http://www.coldwellbanker.com/imgs/property2002/205/14692047_1.jpg.266 +x200 http://www.coldwellbanker.com/imgs/property2002/78/14692047_2.jpg.266x +200 http://www.coldwellbanker.com/imgs/property2002/463/14692047_3.jpg.266 +x200 http://www.coldwellbanker.com/imgs/property2002/336/14692047_4.jpg.266 +x200 http://www.coldwellbanker.com/imgs/property2002/209/14692047_5.jpg.266 +x200 http://www.coldwellbanker.com/imgs/property2002/82/14692047_6.jpg.266x +200 http://www.coldwellbanker.com/imgs/property2002/467/14692047_7.jpg.266 +x200 http://www.coldwellbanker.com/imgs/property2002/340/14692047_8.jpg.266 +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="http://www.mysite.com/$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.

Comment on Image::Magick not printing all images
Download Code
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 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="http://www.mysite.com/$photo"><br>~; }
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 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://733357]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-25 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls