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

(Version: ImageMagick 6.2.9 09/24/07)

I'm scanning a directory of .pic files (school student photos), converting them to .jpg files and creating reduced thumbnail images. Initially I was executing 'convert' to do the image work, but I though Image::Magick might be a more elegant solution. So I re-wrote my code to use Image::Magick but I've hit a wall with file handle limits. It appears that Image::Magick is creating temporary file handles and not releasing them. Eventually, the file handle pool runs out and the script dies.

To illustrate I've reduced my code to a simple example the reads/converts/writes the same image file over and over again. (you'll have to provide your own images to test). When I run this example it fails at 1016:
Image Magick(1016) Read failed: Exception 430: unable to create tempor +ary file `/home/user/tmp/magick-XX376BF8': Too many open files
Is this an Image::Magick bug? Can I force Image::Magick to use ram instead of creating tmp file handles? What if anything can I do to get this example to stop failing?

example follows:
use strict; use warnings; use Image::Magick; my $image_count = 0; # provide your own test paths for these! my $src_dir = '/some/source/directory/containing/a/.pic/file'; my $dest_dir = '/some/temp/directory'; my $file = 'somepicfilename'; while (1) { convert("$src_dir/$file.pic", $dest_dir, "$file.jpg"); } sub convert { my ($src, $dest, $filename) = @_; my $image = Image::Magick->new; $image_count++; my $x = $image->Read($src); die "Image Magick($image_count) Read failed: $x\n" if $x; $x = $image->Set(quality=>'85') and die "Image Magick($image_count) Set1 failed: $x\n"; $x = $image->Write("$dest/$filename") and die "Image Magick($image_count) Write1 failed: $x\n"; #172x228 $x = $image->Crop(geometry=>'120x159+26+15') and die "Image Magick($image_count) Crop1 failed: $x\n"; $x = $image->Thumbnail(width=>86, height=>114) and die "Image Magick($image_count) Thumbnail1 failed: $x\n"; $x = $image->Set(quality=>'75') and die "Image Magick($image_count) Set2 failed: $x\n"; $x = $image->Write("$dest/thumb.$filename") and die "Image Magick($image_count) Write2 failed: $x\n"; $x = $image->Crop(geometry=>'75x90+27+25') and die "Image Magick($image_count) Crop2 failed: $x\n"; $x = $image->Thumbnail(width=>29, height=>38) and die "Image Magick($image_count) Thumbnail2 failed: $x\n"; $x = $image->Write("$dest/stamp.$filename") and die "Image Magick($image_count) Write3 failed: $x\n"; }

Replies are listed 'Best First'.
Re: Image::Magick Exception 430
by snoopy (Curate) on Nov 05, 2007 at 00:24 UTC
    Hi ruzam,

    I ran your script, using Image::Magick 6.3.4, and substituting pdfs for pic files. I am getting the same error!

    I agree that Image::Magick doesn't seem to be cleaning up file handles properly.

    You might want to examine Image Magick bugs. Consider submitting a bug report, against the Perl bindings, using your test case.

    Note that the current version of Image::Magick is 6.3.6

      Thanks snoopy,

      At least now I know there's nothing more I can do with it (getting a newer version of Image::Magick is not an option).

      Before I revert my code back to running 'convert' as a shell command, is there a way to get at the handles created by Image::Magick and force close them outside of Image::Magick?
        This smells like a serious bug. It has the potential to destabilise other parts of your application.

        As a work-around, your best bet could be to convert your sub to a stand-alone command-script and run it as a once per image from your main program.

        Submit this as a bug report, if you've got a few minutes, Otherwise I will!

        Update:The bug went away on my system after I upgraded to 6.3.6

        From the change log:

        2007-05-03  6.3.4-1 Cristy  <quetzlzacatenango@image...>
          * Add support for PFM images.
          * Check for corrupt EXIF image profiles.
          * Writing JPEG YCbCr TIFF images no longer faults (reference
            http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=8896).
        
        
Re: Image::Magick Exception 430
by ruzam (Curate) on Nov 04, 2007 at 16:08 UTC
    Anyone?