Zac B
Zac B

Reputation: 4232

How can I configure ImageMagick to not use any temporary files?

I'm trying to run ImageMagick without touching the filesystem: image data is read from memory/the network, and written back to a socket as a blob.

However, ImageMagick continually tries to write temp files, which either fill up my test system due to aborted/failed tests, or cause problems on systems with extremely slow disks: it's a weird constraint, but many of my conversion hosts are embedded-like systems with block devices that are extremely slow to respond to any operations, even stat()s.

Questions:

  1. Is there a way to configure ImageMagick to not touch the disk during image processing?Assume that all required modules that ImageMagick will use have already been loaded, and that none of the ImageMagick functionality that farms processing out to subprocesses that talk to the filesystem will be used.

  2. What are the side effects of doing this? I'm OK with processing that won't fit in memory failing, rather than falling back to the disks.

I'm converting using the C++ or Perl ImageMagick APIs, not the convert utility. If another binding has support for this, though, I'm fine switching platforms.

What I've Tried

Upvotes: 8

Views: 3001

Answers (2)

Janne
Janne

Reputation: 113

I had to increase the memory in the config file, when using the command line tools:

In /etc/ImageMagick-6/policy.xml:

<!-- DEFAULT: <policy domain="resource" name="memory" value="256MiB"/> -->
<policy domain="resource" name="memory" value="1024MiB"/> 

There was a discrepancy with versions: 8:6.9.7.4+dfsg-16ubuntu6.9 (Ubuntu 18.04) worked default without writing temporary files and with 256MiB, but with 8:6.9.10.23+dfsg-2.1ubuntu11.2 (Ubuntu 20.04) I had to increase the memory as above.

Upvotes: 1

Finbar Crago
Finbar Crago

Reputation: 444

The trick is to set MAGICK_DISK_LIMIT=0 and MAGICK_AREA_LIMIT/MAGICK_AREA_LIMIT to a value larger than the maximum memory required by the program.

#!/usr/bin/perl
BEGIN {
    $ENV{'MAGICK_DISK_LIMIT'  }='0';
    $ENV{'MAGICK_AREA_LIMIT'  }='1Mb';
    $ENV{'MAGICK_MEMORY_LIMIT'}='1Mb';
}

use Image::Magick;    
my $img = Image::Magick->new();
my $err = $img->Read('in.png');
die $err if $err;

If ImageMagick exceeds this memory limit it will die with a message like:

Exception 450: Memory allocation failed `in.png' @ error/png.c/MagickPNGErrorHandler/1645 at ./imagemem.pl line 11.

You can view ImageMagick's default limits by running identify -list resource

Upvotes: 2

Related Questions